From 0c4750650a84caa64c6df79c5d4042916d529799 Mon Sep 17 00:00:00 2001 From: Piotr Jaszczyk Date: Wed, 27 Feb 2019 11:43:39 +0100 Subject: Add MDC support to CBS client Change-Id: Id1f1f9016b03658eca0afd0bd3bd724afc0bea96 Issue-ID: DCAEGEN2-1233 Signed-off-by: Piotr Jaszczyk --- rest-services/common-dependency/pom.xml | 8 ++ .../model/logging/GlobalDiagnosticContext.java | 71 ++++++++++++++ .../rest/services/model/logging/MdcVariables.java | 24 ++++- .../model/logging/RequestDiagnosticContext.java | 102 +++++++++++++++++++++ 4 files changed, 203 insertions(+), 2 deletions(-) create mode 100644 rest-services/common-dependency/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/model/logging/GlobalDiagnosticContext.java create mode 100644 rest-services/common-dependency/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/model/logging/RequestDiagnosticContext.java (limited to 'rest-services/common-dependency') 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 @@ -33,6 +33,14 @@ org.immutables value + + io.vavr + vavr + + + org.jetbrains + annotations + ch.qos.logback logback-classic 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 Piotr Jaszczyk + * @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 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 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 Piotr Jaszczyk + * @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 remoteCallHttpHeaders() { + java.util.Map 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 asMap() { + java.util.Map 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 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(); + } +} + + -- cgit 1.2.3-korg