0c4a4b1f6957a2e9b5e756c95bc5e9a90ba256b7
[dcaegen2/services/sdk.git] /
1 /*
2  * ============LICENSE_START====================================
3  * DCAEGEN2-SERVICES-SDK
4  * =========================================================
5  * Copyright (C) 2019 Nokia. All rights reserved.
6  * =========================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *       http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=====================================
19  */
20
21 package org.onap.dcaegen2.services.sdk.rest.services.model.logging;
22
23 import io.vavr.collection.HashMap;
24 import io.vavr.collection.Map;
25 import java.util.UUID;
26 import org.immutables.value.Value;
27 import org.jetbrains.annotations.NotNull;
28 import org.jetbrains.annotations.Nullable;
29 import org.slf4j.MDC;
30
31 /**
32  * @author <a href="mailto:piotr.jaszczyk@nokia.com">Piotr Jaszczyk</a>
33  * @since 1.1.2
34  */
35 @Value.Immutable
36 public interface RequestDiagnosticContext {
37
38     UUID requestId();
39
40     @Nullable UUID invocationId();
41
42     @Value.Default
43     default GlobalDiagnosticContext global() {
44         return GlobalDiagnosticContext.instance();
45     }
46
47     @Value.Derived
48     default Map<String, String> remoteCallHttpHeaders() {
49         java.util.Map<String, String> result = new java.util.HashMap<>();
50
51         result.put(MdcVariables.httpHeader(MdcVariables.REQUEST_ID), requestId().toString());
52
53         if (invocationId() != null) {
54             result.put(MdcVariables.httpHeader(MdcVariables.INVOCATION_ID), invocationId().toString());
55         }
56
57         return HashMap.ofAll(result);
58     }
59
60     @Value.Derived
61     default Map<String, String> asMap() {
62         java.util.Map<String, String> result = new java.util.HashMap<>();
63
64         if (requestId() != null) {
65             result.put(MdcVariables.REQUEST_ID, requestId().toString());
66         }
67
68         if (invocationId() != null) {
69             result.put(MdcVariables.INVOCATION_ID, invocationId().toString());
70         }
71
72         return global().asMap().merge(HashMap.ofAll(result));
73     }
74
75     default void withSlf4jMdc(Runnable runnable) {
76         withSlf4jMdc(true, runnable);
77     }
78
79     default void withSlf4jMdc(boolean loglevelEnabled, Runnable runnable) {
80         if (loglevelEnabled) {
81             final java.util.Map<String, String> ctxBefore = MDC.getCopyOfContextMap();
82             try {
83                 MDC.setContextMap(asMap().toJavaMap());
84                 runnable.run();
85             } finally {
86                 if (ctxBefore == null) {
87                     MDC.clear();
88                 } else {
89                     MDC.setContextMap(ctxBefore);
90                 }
91             }
92         }
93     }
94
95     default @NotNull RequestDiagnosticContext withNewInvocationId() {
96         return ImmutableRequestDiagnosticContext.copyOf(this)
97                 .withInvocationId(UUID.randomUUID());
98     }
99
100     static ImmutableRequestDiagnosticContext create() {
101         return ImmutableRequestDiagnosticContext.builder()
102                 .requestId(UUID.randomUUID())
103                 .invocationId(UUID.randomUUID())
104                 .build();
105     }
106 }
107
108