72d18437767ebb32899bd2362b6f7fd82c3f0266
[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.onap.dcaegen2.services.sdk.rest.services.model.logging.MdcVariables;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27 import org.slf4j.MDC;
28 import org.slf4j.Marker;
29 import org.slf4j.MarkerFactory;
30 import org.springframework.http.HttpHeaders;
31
32 /**
33  * Support functions for MDC.
34  */
35 public final class MappedDiagnosticContext {
36
37     public static final Marker ENTRY = MarkerFactory.getMarker("ENTRY");
38     public static final Marker EXIT = MarkerFactory.getMarker("EXIT");
39     private static final Marker INVOKE = MarkerFactory.getMarker("INVOKE");
40
41     private static final Logger logger = LoggerFactory.getLogger(MappedDiagnosticContext.class);
42
43     private MappedDiagnosticContext() {
44     }
45
46     /**
47      * Inserts the relevant trace information in the HTTP header.
48      *
49      * @param httpRequest a request
50      */
51     public static void appendTraceInfo(HttpRequestBase httpRequest) {
52         String requestId = MDC.get(MdcVariables.REQUEST_ID);
53         httpRequest.addHeader(MdcVariables.X_ONAP_REQUEST_ID, requestId);
54         httpRequest.addHeader("X-RequestID", requestId); // deprecated
55         httpRequest.addHeader("X-TransactionID", requestId); // deprecated
56
57         String invocationId = UUID.randomUUID().toString();
58         httpRequest.addHeader(MdcVariables.X_INVOCATION_ID, invocationId);
59         logger.info(INVOKE, "Invoking request with invocation ID {}", invocationId);
60     }
61
62     /**
63      * Initialize MDC from relevant information in a received HTTP header.
64      *
65      * @param headers a received HTPP header
66      */
67     public static void initializeTraceContext(HttpHeaders headers) {
68         String requestId = headers.getFirst(MdcVariables.X_ONAP_REQUEST_ID);
69         if (StringUtils.isBlank(requestId)) {
70             requestId = UUID.randomUUID().toString();
71         }
72         String invocationId = headers.getFirst(MdcVariables.X_INVOCATION_ID);
73         if (StringUtils.isBlank(invocationId)) {
74             invocationId = UUID.randomUUID().toString();
75         }
76         MDC.put(MdcVariables.REQUEST_ID, requestId);
77         MDC.put(MdcVariables.INVOCATION_ID, invocationId);
78     }
79
80     /**
81      * Initialize the MDC when a new context is started.
82      * 
83      * @return a copy of the new trace context
84      */
85     public static Map<String, String> initializeTraceContext() {
86         MDC.clear();
87         MDC.put(MdcVariables.REQUEST_ID, UUID.randomUUID().toString());
88         return MDC.getCopyOfContextMap();
89     }
90
91     /**
92      * Updates the request ID in the current context.
93      * 
94      * @param newRequestId the new value of the request ID
95      * @return a copy the updated context
96      */
97     public static Map<String, String> setRequestId(String newRequestId) {
98         Map<String, String> context = MDC.getCopyOfContextMap();
99         context.put(MdcVariables.REQUEST_ID, newRequestId);
100         MDC.setContextMap(context);
101         return context;
102     }
103
104 }