|
| 1 | +/* |
| 2 | + * Copyright (C) 2020 Temporal Technologies, Inc. All Rights Reserved. |
| 3 | + * |
| 4 | + * Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 5 | + * |
| 6 | + * Modifications copyright (C) 2017 Uber Technologies, Inc. |
| 7 | + * |
| 8 | + * Licensed under the Apache License, Version 2.0 (the "License"). You may not |
| 9 | + * use this file except in compliance with the License. A copy of the License is |
| 10 | + * located at |
| 11 | + * |
| 12 | + * http://aws.amazon.com/apache2.0 |
| 13 | + * |
| 14 | + * or in the "license" file accompanying this file. This file is distributed on |
| 15 | + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 16 | + * express or implied. See the License for the specific language governing |
| 17 | + * permissions and limitations under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package io.temporal.common.context; |
| 21 | + |
| 22 | +import io.opentracing.Scope; |
| 23 | +import io.opentracing.Span; |
| 24 | +import io.opentracing.SpanContext; |
| 25 | +import io.opentracing.Tracer; |
| 26 | +import io.opentracing.log.Fields; |
| 27 | +import io.opentracing.propagation.Format; |
| 28 | +import io.opentracing.propagation.TextMap; |
| 29 | +import io.opentracing.tag.Tags; |
| 30 | +import io.opentracing.util.GlobalTracer; |
| 31 | +import io.temporal.api.common.v1.Payload; |
| 32 | +import io.temporal.common.converter.DataConverter; |
| 33 | +import io.temporal.internal.logging.LoggerTag; |
| 34 | +import java.util.HashMap; |
| 35 | +import java.util.Iterator; |
| 36 | +import java.util.Map; |
| 37 | +import org.slf4j.Logger; |
| 38 | +import org.slf4j.LoggerFactory; |
| 39 | +import org.slf4j.MDC; |
| 40 | + |
| 41 | +/** Support for OpenTracing spans */ |
| 42 | +public class OpenTracingContextPropagator implements ContextPropagator { |
| 43 | + |
| 44 | + private static final Logger log = LoggerFactory.getLogger(OpenTracingContextPropagator.class); |
| 45 | + |
| 46 | + private static final ThreadLocal<SpanContext> currentOpenTracingSpanContext = new ThreadLocal<>(); |
| 47 | + private static final ThreadLocal<Span> currentOpenTracingSpan = new ThreadLocal<>(); |
| 48 | + private static final ThreadLocal<Scope> currentOpenTracingScope = new ThreadLocal<>(); |
| 49 | + |
| 50 | + public static SpanContext getCurrentOpenTracingSpanContext() { |
| 51 | + return currentOpenTracingSpanContext.get(); |
| 52 | + } |
| 53 | + |
| 54 | + public static void setCurrentOpenTracingSpanContext(SpanContext ctx) { |
| 55 | + if (ctx != null) { |
| 56 | + currentOpenTracingSpanContext.set(ctx); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public String getName() { |
| 62 | + return "OpenTracing"; |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public Map<String, Payload> serializeContext(Object context) { |
| 67 | + Map<String, Payload> serializedContext = new HashMap<>(); |
| 68 | + Map<String, String> contextMap = (Map<String, String>) context; |
| 69 | + if (contextMap != null) { |
| 70 | + for (Map.Entry<String, String> entry : contextMap.entrySet()) { |
| 71 | + serializedContext.put( |
| 72 | + entry.getKey(), DataConverter.getDefaultInstance().toPayload(entry.getValue()).get()); |
| 73 | + } |
| 74 | + } |
| 75 | + return serializedContext; |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public Object deserializeContext(Map<String, Payload> context) { |
| 80 | + Map<String, String> contextMap = new HashMap<>(); |
| 81 | + for (Map.Entry<String, Payload> entry : context.entrySet()) { |
| 82 | + contextMap.put( |
| 83 | + entry.getKey(), |
| 84 | + DataConverter.getDefaultInstance() |
| 85 | + .fromPayload(entry.getValue(), String.class, String.class)); |
| 86 | + } |
| 87 | + return contextMap; |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public Object getCurrentContext() { |
| 92 | + Tracer currentTracer = GlobalTracer.get(); |
| 93 | + Span currentSpan = currentTracer.scopeManager().activeSpan(); |
| 94 | + if (currentSpan != null) { |
| 95 | + HashMapTextMap contextTextMap = new HashMapTextMap(); |
| 96 | + currentTracer.inject(currentSpan.context(), Format.Builtin.TEXT_MAP, contextTextMap); |
| 97 | + return contextTextMap.getBackingMap(); |
| 98 | + } else { |
| 99 | + return null; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public void setCurrentContext(Object context) { |
| 105 | + Tracer currentTracer = GlobalTracer.get(); |
| 106 | + Map<String, String> contextAsMap = (Map<String, String>) context; |
| 107 | + if (contextAsMap != null) { |
| 108 | + HashMapTextMap contextTextMap = new HashMapTextMap(contextAsMap); |
| 109 | + setCurrentOpenTracingSpanContext( |
| 110 | + currentTracer.extract(Format.Builtin.TEXT_MAP, contextTextMap)); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public void setUp() { |
| 116 | + Tracer openTracingTracer = GlobalTracer.get(); |
| 117 | + Tracer.SpanBuilder builder = |
| 118 | + openTracingTracer |
| 119 | + .buildSpan("cadence.workflow") |
| 120 | + .withTag("resource.name", MDC.get(LoggerTag.WORKFLOW_TYPE)); |
| 121 | + if (getCurrentOpenTracingSpanContext() != null) { |
| 122 | + builder.asChildOf(getCurrentOpenTracingSpanContext()); |
| 123 | + } |
| 124 | + Span span = builder.start(); |
| 125 | + openTracingTracer.activateSpan(span); |
| 126 | + currentOpenTracingSpan.set(span); |
| 127 | + Scope scope = openTracingTracer.activateSpan(span); |
| 128 | + currentOpenTracingScope.set(scope); |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public void onError(Throwable t) { |
| 133 | + Span span = currentOpenTracingSpan.get(); |
| 134 | + if (span != null) { |
| 135 | + Tags.ERROR.set(span, true); |
| 136 | + Map<String, Object> errorData = new HashMap<>(); |
| 137 | + errorData.put(Fields.EVENT, "error"); |
| 138 | + if (t != null) { |
| 139 | + errorData.put(Fields.ERROR_OBJECT, t); |
| 140 | + errorData.put(Fields.MESSAGE, t.getMessage()); |
| 141 | + } |
| 142 | + span.log(errorData); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + @Override |
| 147 | + public void finish() { |
| 148 | + Scope currentScope = currentOpenTracingScope.get(); |
| 149 | + Span currentSpan = currentOpenTracingSpan.get(); |
| 150 | + if (currentScope != null) { |
| 151 | + currentScope.close(); |
| 152 | + } |
| 153 | + if (currentSpan != null) { |
| 154 | + currentSpan.finish(); |
| 155 | + } |
| 156 | + currentOpenTracingScope.remove(); |
| 157 | + currentOpenTracingSpan.remove(); |
| 158 | + currentOpenTracingSpanContext.remove(); |
| 159 | + } |
| 160 | + |
| 161 | + /** Just check for other instances of the same class */ |
| 162 | + @Override |
| 163 | + public boolean equals(Object obj) { |
| 164 | + if (obj == null) { |
| 165 | + return false; |
| 166 | + } |
| 167 | + if (this == obj) { |
| 168 | + return true; |
| 169 | + } |
| 170 | + return this.getClass().equals(obj.getClass()); |
| 171 | + } |
| 172 | + |
| 173 | + @Override |
| 174 | + public int hashCode() { |
| 175 | + return this.getClass().hashCode(); |
| 176 | + } |
| 177 | + |
| 178 | + private class HashMapTextMap implements TextMap { |
| 179 | + private final HashMap<String, String> backingMap = new HashMap<>(); |
| 180 | + |
| 181 | + public HashMapTextMap() { |
| 182 | + // Noop |
| 183 | + } |
| 184 | + |
| 185 | + public HashMapTextMap(Map<String, String> spanData) { |
| 186 | + backingMap.putAll(spanData); |
| 187 | + } |
| 188 | + |
| 189 | + @Override |
| 190 | + public Iterator<Map.Entry<String, String>> iterator() { |
| 191 | + return backingMap.entrySet().iterator(); |
| 192 | + } |
| 193 | + |
| 194 | + @Override |
| 195 | + public void put(String key, String value) { |
| 196 | + backingMap.put(key, value); |
| 197 | + } |
| 198 | + |
| 199 | + public HashMap<String, String> getBackingMap() { |
| 200 | + return backingMap; |
| 201 | + } |
| 202 | + } |
| 203 | +} |
0 commit comments