|
| 1 | +/* |
| 2 | + * Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Modifications copyright (C) 2017 Uber Technologies, Inc. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"). You may not |
| 7 | + * use this file except in compliance with the License. A copy of the License is |
| 8 | + * located at |
| 9 | + * |
| 10 | + * http://aws.amazon.com/apache2.0 |
| 11 | + * |
| 12 | + * or in the "license" file accompanying this file. This file is distributed on |
| 13 | + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 14 | + * express or implied. See the License for the specific language governing |
| 15 | + * permissions and limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package com.uber.cadence.context; |
| 19 | + |
| 20 | +import io.opentelemetry.api.GlobalOpenTelemetry; |
| 21 | +import io.opentelemetry.api.baggage.Baggage; |
| 22 | +import io.opentelemetry.api.baggage.propagation.W3CBaggagePropagator; |
| 23 | +import io.opentelemetry.api.trace.Span; |
| 24 | +import io.opentelemetry.api.trace.SpanKind; |
| 25 | +import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator; |
| 26 | +import io.opentelemetry.context.Context; |
| 27 | +import io.opentelemetry.context.Scope; |
| 28 | +import io.opentelemetry.context.propagation.TextMapGetter; |
| 29 | +import io.opentelemetry.context.propagation.TextMapPropagator; |
| 30 | +import io.opentelemetry.context.propagation.TextMapSetter; |
| 31 | +import java.nio.charset.Charset; |
| 32 | +import java.util.HashMap; |
| 33 | +import java.util.Map; |
| 34 | +import javax.annotation.Nullable; |
| 35 | +import org.slf4j.MDC; |
| 36 | + |
| 37 | +public class OpenTelemetryContextPropagator implements ContextPropagator { |
| 38 | + |
| 39 | + private static final TextMapPropagator w3cTraceContextPropagator = |
| 40 | + W3CTraceContextPropagator.getInstance(); |
| 41 | + private static final TextMapPropagator w3cBaggagePropagator = W3CBaggagePropagator.getInstance(); |
| 42 | + private static ThreadLocal<Scope> currentContextOtelScope = new ThreadLocal<>(); |
| 43 | + private static ThreadLocal<Span> currentOtelSpan = new ThreadLocal<>(); |
| 44 | + private static ThreadLocal<Scope> currentOtelScope = new ThreadLocal<>(); |
| 45 | + private static ThreadLocal<Iterable<String>> otelKeySet = new ThreadLocal<>(); |
| 46 | + private static final TextMapSetter<Map<String, String>> setter = Map::put; |
| 47 | + private static final TextMapGetter<Map<String, String>> getter = |
| 48 | + new TextMapGetter<Map<String, String>>() { |
| 49 | + @Override |
| 50 | + public Iterable<String> keys(Map<String, String> carrier) { |
| 51 | + return otelKeySet.get(); |
| 52 | + } |
| 53 | + |
| 54 | + @Nullable |
| 55 | + @Override |
| 56 | + public String get(Map<String, String> carrier, String key) { |
| 57 | + return MDC.get(key); |
| 58 | + } |
| 59 | + }; |
| 60 | + |
| 61 | + @Override |
| 62 | + public String getName() { |
| 63 | + return "OpenTelemetry"; |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public Map<String, byte[]> serializeContext(Object context) { |
| 68 | + Map<String, byte[]> serializedContext = new HashMap<>(); |
| 69 | + Map<String, String> contextMap = (Map<String, String>) context; |
| 70 | + if (contextMap != null) { |
| 71 | + for (Map.Entry<String, String> entry : contextMap.entrySet()) { |
| 72 | + serializedContext.put(entry.getKey(), entry.getValue().getBytes(Charset.defaultCharset())); |
| 73 | + } |
| 74 | + } |
| 75 | + return serializedContext; |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public Object deserializeContext(Map<String, byte[]> context) { |
| 80 | + Map<String, String> contextMap = new HashMap<>(); |
| 81 | + for (Map.Entry<String, byte[]> entry : context.entrySet()) { |
| 82 | + contextMap.put(entry.getKey(), new String(entry.getValue(), Charset.defaultCharset())); |
| 83 | + } |
| 84 | + return contextMap; |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public Object getCurrentContext() { |
| 89 | + Map<String, String> carrier = new HashMap<>(); |
| 90 | + w3cTraceContextPropagator.inject(Context.current(), carrier, setter); |
| 91 | + w3cBaggagePropagator.inject(Context.current(), carrier, setter); |
| 92 | + return carrier; |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public void setCurrentContext(Object context) { |
| 97 | + Map<String, String> contextMap = (Map<String, String>) context; |
| 98 | + if (contextMap != null) { |
| 99 | + for (Map.Entry<String, String> entry : contextMap.entrySet()) { |
| 100 | + MDC.put(entry.getKey(), entry.getValue()); |
| 101 | + } |
| 102 | + otelKeySet.set(contextMap.keySet()); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + @SuppressWarnings("MustBeClosedChecker") |
| 108 | + public void setUp() { |
| 109 | + Context context = |
| 110 | + Baggage.fromContext(w3cBaggagePropagator.extract(Context.current(), null, getter)) |
| 111 | + .toBuilder() |
| 112 | + .build() |
| 113 | + .storeInContext(w3cTraceContextPropagator.extract(Context.current(), null, getter)); |
| 114 | + |
| 115 | + currentContextOtelScope.set(context.makeCurrent()); |
| 116 | + |
| 117 | + Span span = |
| 118 | + GlobalOpenTelemetry.getTracer("cadence-client") |
| 119 | + .spanBuilder("cadence.workflow") |
| 120 | + .setParent(context) |
| 121 | + .setSpanKind(SpanKind.CLIENT) |
| 122 | + .startSpan(); |
| 123 | + |
| 124 | + Scope scope = span.makeCurrent(); |
| 125 | + currentOtelSpan.set(span); |
| 126 | + currentOtelScope.set(scope); |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + public void finish() { |
| 131 | + Scope scope = currentOtelScope.get(); |
| 132 | + if (scope != null) { |
| 133 | + scope.close(); |
| 134 | + } |
| 135 | + Span span = currentOtelSpan.get(); |
| 136 | + if (span != null) { |
| 137 | + span.end(); |
| 138 | + } |
| 139 | + Scope contextScope = currentContextOtelScope.get(); |
| 140 | + if (contextScope != null) { |
| 141 | + contextScope.close(); |
| 142 | + } |
| 143 | + } |
| 144 | +} |
0 commit comments