bda889c2757742e6da181ed9e98c48a58d728c84
[dcaegen2/collectors/datafile.git] /
1 /*
2  * ============LICENSE_START======================================================================
3  * Copyright (C) 2018 NOKIA Intellectual Property, 2019 Nordix Foundation. All rights reserved.
4  * ===============================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
6  * in compliance with the License. You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software distributed under the License
11  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12  * or implied. See the License for the specific language governing permissions and limitations under
13  * the License.
14  * ============LICENSE_END========================================================================
15  */
16
17 package org.onap.dcaegen2.collectors.datafile.model.logging;
18
19 import java.util.Map;
20 import java.util.UUID;
21
22 import org.apache.commons.lang3.StringUtils;
23 import org.apache.http.client.methods.HttpRequestBase;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26 import org.slf4j.MDC;
27 import org.slf4j.Marker;
28 import org.slf4j.MarkerFactory;
29 import org.springframework.http.HttpHeaders;
30
31 /**
32  * Support functions for MDC
33  */
34 public final class MappedDiagnosticContext {
35
36     private static final String X_ONAP_REQUEST_ID = "X-ONAP-RequestID";
37     private static final String X_INVOCATION_ID = "X-InvocationID";
38     private static final String REQUEST_ID = "RequestID";
39     private static final String INVOCATION_ID = "InvocationID";
40     public static final String RESPONSE_CODE = "ResponseCode";
41     public static final String SERVICE_NAME = "ServiceName";
42
43     public static final Marker ENTRY = MarkerFactory.getMarker("ENTRY");
44     public static final Marker EXIT = MarkerFactory.getMarker("EXIT");
45     private static final Marker INVOKE = MarkerFactory.getMarker("INVOKE");
46
47     private static final Logger logger = LoggerFactory.getLogger(MappedDiagnosticContext.class);
48
49     private MappedDiagnosticContext() {}
50
51     /**
52      * Inserts the relevant trace information in the HTTP header
53      * @param httpRequest a request
54      */
55     public static void appendTraceInfo(HttpRequestBase httpRequest) {
56         String requestId = MDC.get(REQUEST_ID);
57         httpRequest.addHeader(X_ONAP_REQUEST_ID, requestId);
58         httpRequest.addHeader("X-RequestID", requestId); // deprecated
59         httpRequest.addHeader("X-TransactionID", requestId); // deprecated
60
61         String invocationId = UUID.randomUUID().toString();
62         httpRequest.addHeader(X_INVOCATION_ID, invocationId);
63         logger.info(INVOKE, "Invoking request with invocation ID {}", invocationId);
64     }
65
66     /**
67      * Initialize MDC from relevant information in a received HTTP header
68      * @param headers a received HTPP header
69      */
70     public static void initializeTraceContext(HttpHeaders headers) {
71         String requestId = headers.getFirst(X_ONAP_REQUEST_ID);
72         if (StringUtils.isBlank(requestId)) {
73             requestId = UUID.randomUUID().toString();
74         }
75         String invocationId = headers.getFirst(X_INVOCATION_ID);
76         if (StringUtils.isBlank(invocationId)) {
77             invocationId = UUID.randomUUID().toString();
78         }
79         MDC.put(REQUEST_ID, requestId);
80         MDC.put(INVOCATION_ID, invocationId);
81     }
82
83     /**
84      * Initialize the MDC when a new context is started.
85      * @return a copy of the new trace context
86      */
87     public static Map<String, String> initializeTraceContext() {
88         MDC.clear();
89         MDC.put(REQUEST_ID, UUID.randomUUID().toString());
90         return MDC.getCopyOfContextMap();
91     }
92 }