aboutsummaryrefslogtreecommitdiffstats
path: root/datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/model/logging/MappedDiagnosticContext.java
blob: 72d18437767ebb32899bd2362b6f7fd82c3f0266 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
 * ============LICENSE_START======================================================================
 * Copyright (C) 2018 NOKIA Intellectual Property, 2019 Nordix Foundation. All rights reserved.
 * ===============================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 * ============LICENSE_END========================================================================
 */

package org.onap.dcaegen2.collectors.datafile.model.logging;

import java.util.Map;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.methods.HttpRequestBase;
import org.onap.dcaegen2.services.sdk.rest.services.model.logging.MdcVariables;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.slf4j.Marker;
import org.slf4j.MarkerFactory;
import org.springframework.http.HttpHeaders;

/**
 * Support functions for MDC.
 */
public final class MappedDiagnosticContext {

    public static final Marker ENTRY = MarkerFactory.getMarker("ENTRY");
    public static final Marker EXIT = MarkerFactory.getMarker("EXIT");
    private static final Marker INVOKE = MarkerFactory.getMarker("INVOKE");

    private static final Logger logger = LoggerFactory.getLogger(MappedDiagnosticContext.class);

    private MappedDiagnosticContext() {
    }

    /**
     * Inserts the relevant trace information in the HTTP header.
     *
     * @param httpRequest a request
     */
    public static void appendTraceInfo(HttpRequestBase httpRequest) {
        String requestId = MDC.get(MdcVariables.REQUEST_ID);
        httpRequest.addHeader(MdcVariables.X_ONAP_REQUEST_ID, requestId);
        httpRequest.addHeader("X-RequestID", requestId); // deprecated
        httpRequest.addHeader("X-TransactionID", requestId); // deprecated

        String invocationId = UUID.randomUUID().toString();
        httpRequest.addHeader(MdcVariables.X_INVOCATION_ID, invocationId);
        logger.info(INVOKE, "Invoking request with invocation ID {}", invocationId);
    }

    /**
     * Initialize MDC from relevant information in a received HTTP header.
     *
     * @param headers a received HTPP header
     */
    public static void initializeTraceContext(HttpHeaders headers) {
        String requestId = headers.getFirst(MdcVariables.X_ONAP_REQUEST_ID);
        if (StringUtils.isBlank(requestId)) {
            requestId = UUID.randomUUID().toString();
        }
        String invocationId = headers.getFirst(MdcVariables.X_INVOCATION_ID);
        if (StringUtils.isBlank(invocationId)) {
            invocationId = UUID.randomUUID().toString();
        }
        MDC.put(MdcVariables.REQUEST_ID, requestId);
        MDC.put(MdcVariables.INVOCATION_ID, invocationId);
    }

    /**
     * Initialize the MDC when a new context is started.
     * 
     * @return a copy of the new trace context
     */
    public static Map<String, String> initializeTraceContext() {
        MDC.clear();
        MDC.put(MdcVariables.REQUEST_ID, UUID.randomUUID().toString());
        return MDC.getCopyOfContextMap();
    }

    /**
     * Updates the request ID in the current context.
     * 
     * @param newRequestId the new value of the request ID
     * @return a copy the updated context
     */
    public static Map<String, String> setRequestId(String newRequestId) {
        Map<String, String> context = MDC.getCopyOfContextMap();
        context.put(MdcVariables.REQUEST_ID, newRequestId);
        MDC.setContextMap(context);
        return context;
    }

}