aboutsummaryrefslogtreecommitdiffstats
path: root/rest-services/common-dependency
diff options
context:
space:
mode:
authorPiotr Jaszczyk <piotr.jaszczyk@nokia.com>2019-02-27 11:43:39 +0100
committerPiotr Jaszczyk <piotr.jaszczyk@nokia.com>2019-02-27 14:43:42 +0100
commit0c4750650a84caa64c6df79c5d4042916d529799 (patch)
treea36ff229edee0855a008c9a727aa5caaf6ecb938 /rest-services/common-dependency
parentbc386bcf22a1078a9e29db3faff63667a3b5c99d (diff)
Add MDC support to CBS client
Change-Id: Id1f1f9016b03658eca0afd0bd3bd724afc0bea96 Issue-ID: DCAEGEN2-1233 Signed-off-by: Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
Diffstat (limited to 'rest-services/common-dependency')
-rw-r--r--rest-services/common-dependency/pom.xml8
-rw-r--r--rest-services/common-dependency/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/model/logging/GlobalDiagnosticContext.java71
-rw-r--r--rest-services/common-dependency/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/model/logging/MdcVariables.java24
-rw-r--r--rest-services/common-dependency/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/model/logging/RequestDiagnosticContext.java102
4 files changed, 203 insertions, 2 deletions
diff --git a/rest-services/common-dependency/pom.xml b/rest-services/common-dependency/pom.xml
index 335881d7..ef74c694 100644
--- a/rest-services/common-dependency/pom.xml
+++ b/rest-services/common-dependency/pom.xml
@@ -34,6 +34,14 @@
<artifactId>value</artifactId>
</dependency>
<dependency>
+ <groupId>io.vavr</groupId>
+ <artifactId>vavr</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.jetbrains</groupId>
+ <artifactId>annotations</artifactId>
+ </dependency>
+ <dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
diff --git a/rest-services/common-dependency/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/model/logging/GlobalDiagnosticContext.java b/rest-services/common-dependency/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/model/logging/GlobalDiagnosticContext.java
new file mode 100644
index 00000000..db4a0fd6
--- /dev/null
+++ b/rest-services/common-dependency/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/model/logging/GlobalDiagnosticContext.java
@@ -0,0 +1,71 @@
+/*
+ * ============LICENSE_START====================================
+ * DCAEGEN2-SERVICES-SDK
+ * =========================================================
+ * Copyright (C) 2019 Nokia. 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.services.sdk.rest.services.model.logging;
+
+import io.vavr.collection.HashMap;
+import io.vavr.collection.Map;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+import java.util.UUID;
+import org.immutables.value.Value;
+import org.jetbrains.annotations.Nullable;
+import org.slf4j.MDC;
+
+/**
+ * @author <a href="mailto:piotr.jaszczyk@nokia.com">Piotr Jaszczyk</a>
+ * @since 1.1.2
+ */
+@Value.Immutable(singleton = true)
+public interface GlobalDiagnosticContext {
+
+ @Value.Default
+ default String instanceId() {
+ return UUID.randomUUID().toString();
+ }
+
+ @Value.Default
+ default String serverFqdn() {
+ try {
+ return InetAddress.getLocalHost().toString();
+ } catch (UnknownHostException ex) {
+ return InetAddress.getLoopbackAddress().toString();
+ }
+ }
+
+ @Value.Default
+ default String serviceName() {
+ return System.getenv().getOrDefault("HOSTNAME", "unknown_service");
+ }
+
+ @Value.Derived
+ default Map<String, String> asMap() {
+ return HashMap.of(
+ MdcVariables.INSTANCE_ID, instanceId(),
+ MdcVariables.SERVER_FQDN, serverFqdn(),
+ MdcVariables.SERVICE_NAME, serviceName());
+ }
+
+ static GlobalDiagnosticContext instance() {
+ return ImmutableGlobalDiagnosticContext.of();
+ }
+}
+
+
diff --git a/rest-services/common-dependency/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/model/logging/MdcVariables.java b/rest-services/common-dependency/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/model/logging/MdcVariables.java
index 22090e9f..652e3541 100644
--- a/rest-services/common-dependency/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/model/logging/MdcVariables.java
+++ b/rest-services/common-dependency/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/model/logging/MdcVariables.java
@@ -26,17 +26,37 @@ import java.util.Map;
public final class MdcVariables {
+ @Deprecated
public static final String X_ONAP_REQUEST_ID = "X-ONAP-RequestID";
+ @Deprecated
public static final String X_INVOCATION_ID = "X-InvocationID";
- public static final String REQUEST_ID = "RequestID";
- public static final String INVOCATION_ID = "InvocationID";
+
public static final String INSTANCE_UUID = "InstanceUUID";
public static final String RESPONSE_CODE = "ResponseCode";
+ public static final String REQUEST_ID = "RequestID";
+ public static final String CLIENT_NAME = "PartnerName";
+ public static final String CLIENT_IP = "ClientIPAddress";
+ public static final String INVOCATION_ID = "InvocationID";
+ public static final String INVOCATION_TIMESTAMP = "InvokeTimestamp";
+ public static final String STATUS_CODE = "StatusCode";
+ public static final String INSTANCE_ID = "InstanceID";
+ public static final String SERVER_FQDN = "ServerFQDN";
public static final String SERVICE_NAME = "ServiceName";
+ private static final String HTTP_HEADER_PREFIX = "X-";
+
private MdcVariables() {
}
+ public static String httpHeader(String mdcName) {
+ return HTTP_HEADER_PREFIX + mdcName;
+ }
+
+ /**
+ * @deprecated use {@link RequestDiagnosticContext#withSlf4jMdc(Runnable)}.
+ * @param mdcContextMap
+ */
+ @Deprecated
public static void setMdcContextMap(Map<String, String> mdcContextMap) {
if (mdcContextMap != null) {
MDC.setContextMap(mdcContextMap);
diff --git a/rest-services/common-dependency/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/model/logging/RequestDiagnosticContext.java b/rest-services/common-dependency/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/model/logging/RequestDiagnosticContext.java
new file mode 100644
index 00000000..97269064
--- /dev/null
+++ b/rest-services/common-dependency/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/model/logging/RequestDiagnosticContext.java
@@ -0,0 +1,102 @@
+/*
+ * ============LICENSE_START====================================
+ * DCAEGEN2-SERVICES-SDK
+ * =========================================================
+ * Copyright (C) 2019 Nokia. 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.services.sdk.rest.services.model.logging;
+
+import io.vavr.collection.HashMap;
+import io.vavr.collection.Map;
+import java.util.UUID;
+import org.immutables.value.Value;
+import org.jetbrains.annotations.Nullable;
+import org.slf4j.MDC;
+
+/**
+ * @author <a href="mailto:piotr.jaszczyk@nokia.com">Piotr Jaszczyk</a>
+ * @since 1.1.2
+ */
+@Value.Immutable
+public interface RequestDiagnosticContext {
+
+ UUID requestId();
+
+ @Nullable UUID invocationId();
+
+ @Value.Default
+ default GlobalDiagnosticContext global() {
+ return GlobalDiagnosticContext.instance();
+ }
+
+ @Value.Derived
+ default Map<String, String> remoteCallHttpHeaders() {
+ java.util.Map<String, String> result = new java.util.HashMap<>();
+
+ result.put(MdcVariables.httpHeader(MdcVariables.REQUEST_ID), requestId().toString());
+
+ if (invocationId() != null) {
+ result.put(MdcVariables.httpHeader(MdcVariables.INVOCATION_ID), invocationId().toString());
+ }
+
+ return HashMap.ofAll(result);
+ }
+
+ @Value.Derived
+ default Map<String, String> asMap() {
+ java.util.Map<String, String> result = new java.util.HashMap<>();
+
+ if (requestId() != null) {
+ result.put(MdcVariables.REQUEST_ID, requestId().toString());
+ }
+
+ if (invocationId() != null) {
+ result.put(MdcVariables.INVOCATION_ID, invocationId().toString());
+ }
+
+ return global().asMap().merge(HashMap.ofAll(result));
+ }
+
+ default void withSlf4jMdc(Runnable runnable) {
+ withSlf4jMdc(true, runnable);
+ }
+
+ default void withSlf4jMdc(boolean loglevelEnabled, Runnable runnable) {
+ if (loglevelEnabled) {
+ final java.util.Map<String, String> ctxBefore = MDC.getCopyOfContextMap();
+ try {
+ MDC.setContextMap(asMap().toJavaMap());
+ runnable.run();
+ } finally {
+ if (ctxBefore == null) {
+ MDC.clear();
+ } else {
+ MDC.setContextMap(ctxBefore);
+ }
+ }
+ }
+ }
+
+ static ImmutableRequestDiagnosticContext create() {
+ return ImmutableRequestDiagnosticContext.builder()
+ .requestId(UUID.randomUUID())
+ .invocationId(UUID.randomUUID())
+ .build();
+ }
+}
+
+