2643eea53b8e2bb8834dabf9515e58f0d86be226
[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 import org.apache.commons.lang3.StringUtils;
22 import org.apache.http.client.methods.HttpRequestBase;
23 import org.onap.dcaegen2.services.sdk.rest.services.model.logging.MdcVariables;
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     public static final Marker ENTRY = MarkerFactory.getMarker("ENTRY");
37     public static final Marker EXIT = MarkerFactory.getMarker("EXIT");
38     private static final Marker INVOKE = MarkerFactory.getMarker("INVOKE");
39
40     private static final Logger logger = LoggerFactory.getLogger(MappedDiagnosticContext.class);
41
42     private MappedDiagnosticContext() {}
43
44     /**
45      * Inserts the relevant trace information in the HTTP header.
46      *
47      * @param httpRequest a request
48      */
49     public static void appendTraceInfo(HttpRequestBase httpRequest) {
50         String requestId = MDC.get(MdcVariables.REQUEST_ID);
51         httpRequest.addHeader(MdcVariables.X_ONAP_REQUEST_ID, requestId);
52         httpRequest.addHeader("X-RequestID", requestId); // deprecated
53         httpRequest.addHeader("X-TransactionID", requestId); // deprecated
54
55         String invocationId = UUID.randomUUID().toString();
56         httpRequest.addHeader(MdcVariables.X_INVOCATION_ID, invocationId);
57         logger.info(INVOKE, "Invoking request with invocation ID {}", invocationId);
58     }
59
60     /**
61      * Initialize MDC from relevant information in a received HTTP header.
62      *
63      * @param headers a received HTPP header
64      */
65     public static void initializeTraceContext(HttpHeaders headers) {
66         String requestId = headers.getFirst(MdcVariables.X_ONAP_REQUEST_ID);
67         if (StringUtils.isBlank(requestId)) {
68             requestId = UUID.randomUUID().toString();
69         }
70         String invocationId = headers.getFirst(MdcVariables.X_INVOCATION_ID);
71         if (StringUtils.isBlank(invocationId)) {
72             invocationId = UUID.randomUUID().toString();
73         }
74         MDC.put(MdcVariables.REQUEST_ID, requestId);
75         MDC.put(MdcVariables.INVOCATION_ID, invocationId);
76     }
77
78     /**
79      * Initialize the MDC when a new context is started.
80      * @return a copy of the new trace context
81      */
82     public static Map<String, String> initializeTraceContext() {
83         MDC.clear();
84         MDC.put(MdcVariables.REQUEST_ID, UUID.randomUUID().toString());
85         return MDC.getCopyOfContextMap();
86     }
87 }