|
| 1 | +/* Copyright 2019 Google Inc. |
| 2 | + * |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package com.google.cloud.pubsub.v1; |
| 17 | + |
| 18 | +import com.google.api.core.ApiFunction; |
| 19 | +import com.google.common.annotations.VisibleForTesting; |
| 20 | +import com.google.errorprone.annotations.MustBeClosed; |
| 21 | +import com.google.pubsub.v1.PubsubMessage; |
| 22 | +import io.opencensus.common.Scope; |
| 23 | +import io.opencensus.tags.TagContext; |
| 24 | +import io.opencensus.tags.Tagger; |
| 25 | +import io.opencensus.tags.Tags; |
| 26 | +import io.opencensus.tags.propagation.TagContextBinarySerializer; |
| 27 | +import io.opencensus.trace.Link; |
| 28 | +import io.opencensus.trace.SpanContext; |
| 29 | +import io.opencensus.trace.Tracer; |
| 30 | +import io.opencensus.trace.Tracing; |
| 31 | +import io.opencensus.trace.propagation.SpanContextParseException; |
| 32 | +import io.opencensus.trace.propagation.TextFormat; |
| 33 | +import io.opencensus.trace.propagation.TextFormat.Getter; |
| 34 | +import io.opencensus.trace.propagation.TextFormat.Setter; |
| 35 | +import io.opencensus.trace.samplers.Samplers; |
| 36 | +import java.util.logging.Level; |
| 37 | +import java.util.logging.Logger; |
| 38 | + |
| 39 | +/** |
| 40 | + * Utilities for propagating OpenCensus {@link TagContext} and {@link SpanContext} from publishers |
| 41 | + * to subscribers. |
| 42 | + */ |
| 43 | +public class OpenCensusUtil { |
| 44 | + private static final Logger logger = Logger.getLogger(OpenCensusUtil.class.getName()); |
| 45 | + |
| 46 | + public static final String TAG_CONTEXT_KEY = "googclient_OpenCensusTagContextKey"; |
| 47 | + public static final String TRACE_CONTEXT_KEY = "googclient_OpenCensusTraceContextKey"; |
| 48 | + @VisibleForTesting static final String MESSAGE_RECEIVER_SPAN_NAME = "OpenCensusMessageReceiver"; |
| 49 | + private static final String TRACEPARENT_KEY = "traceparent"; |
| 50 | + |
| 51 | + private static final Tagger tagger = Tags.getTagger(); |
| 52 | + private static final TagContextBinarySerializer serializer = |
| 53 | + Tags.getTagPropagationComponent().getBinarySerializer(); |
| 54 | + |
| 55 | + private static final Tracer tracer = Tracing.getTracer(); |
| 56 | + private static final TextFormat traceContextTextFormat = |
| 57 | + Tracing.getPropagationComponent().getTraceContextFormat(); |
| 58 | + |
| 59 | + /** |
| 60 | + * Propagates active OpenCensus trace and tag contexts from the Publisher by adding them as |
| 61 | + * attributes to the {@link PubsubMessage}. |
| 62 | + */ |
| 63 | + public static final ApiFunction<PubsubMessage, PubsubMessage> OPEN_CENSUS_MESSAGE_TRANSFORM = |
| 64 | + new ApiFunction<PubsubMessage, PubsubMessage>() { |
| 65 | + @Override |
| 66 | + public PubsubMessage apply(PubsubMessage message) { |
| 67 | + PubsubMessage.Builder builder = PubsubMessage.newBuilder(message); |
| 68 | + String encodedSpanContext = encodeSpanContext(tracer.getCurrentSpan().getContext()); |
| 69 | + String encodedTagContext = encodeTagContext(tagger.getCurrentTagContext()); |
| 70 | + if (encodedSpanContext.isEmpty() && encodedTagContext.isEmpty()) { |
| 71 | + return message; |
| 72 | + } |
| 73 | + if (!encodedSpanContext.isEmpty()) { |
| 74 | + builder.putAttributes(TRACE_CONTEXT_KEY, encodedSpanContext); |
| 75 | + } |
| 76 | + if (!encodedTagContext.isEmpty()) { |
| 77 | + builder.putAttributes(TAG_CONTEXT_KEY, encodedTagContext); |
| 78 | + } |
| 79 | + return builder.build(); |
| 80 | + } |
| 81 | + }; |
| 82 | + |
| 83 | + private static final Setter<StringBuilder> setter = |
| 84 | + new Setter<StringBuilder>() { |
| 85 | + @Override |
| 86 | + public void put(StringBuilder carrier, String key, String value) { |
| 87 | + if (key.equals(TRACEPARENT_KEY)) { |
| 88 | + carrier.append(value); |
| 89 | + } |
| 90 | + } |
| 91 | + }; |
| 92 | + |
| 93 | + private static final Getter<String> getter = |
| 94 | + new Getter<String>() { |
| 95 | + @Override |
| 96 | + public String get(String carrier, String key) { |
| 97 | + return key.equals(TRACEPARENT_KEY) ? carrier : null; |
| 98 | + } |
| 99 | + }; |
| 100 | + |
| 101 | + @VisibleForTesting |
| 102 | + static String encodeSpanContext(SpanContext ctxt) { |
| 103 | + StringBuilder builder = new StringBuilder(); |
| 104 | + traceContextTextFormat.inject(ctxt, builder, setter); |
| 105 | + return builder.toString(); |
| 106 | + } |
| 107 | + |
| 108 | + // TODO: update this code once the text encoding of tags has been resolved |
| 109 | + // (https://github.com/census-instrumentation/opencensus-specs/issues/65). |
| 110 | + private static String encodeTagContext(TagContext tags) { |
| 111 | + return ""; |
| 112 | + } |
| 113 | + |
| 114 | + // TODO: update this code once the text encoding of tags has been resolved |
| 115 | + // (https://github.com/census-instrumentation/opencensus-specs/issues/65). |
| 116 | + private static Scope createScopedTagContext(String encodedTags) { |
| 117 | + return tagger.withTagContext(tagger.getCurrentTagContext()); |
| 118 | + } |
| 119 | + |
| 120 | + @VisibleForTesting |
| 121 | + @MustBeClosed |
| 122 | + static Scope createScopedSpan(String name) { |
| 123 | + return tracer |
| 124 | + .spanBuilderWithExplicitParent(name, tracer.getCurrentSpan()) |
| 125 | + .setRecordEvents(true) |
| 126 | + // Note: we preserve the sampling decision from the publisher. |
| 127 | + .setSampler(Samplers.alwaysSample()) |
| 128 | + .startScopedSpan(); |
| 129 | + } |
| 130 | + |
| 131 | + private static void addParentLink(String encodedParentSpanContext) { |
| 132 | + try { |
| 133 | + SpanContext ctxt = traceContextTextFormat.extract(encodedParentSpanContext, getter); |
| 134 | + tracer.getCurrentSpan().addLink(Link.fromSpanContext(ctxt, Link.Type.PARENT_LINKED_SPAN)); |
| 135 | + } catch (SpanContextParseException exn) { |
| 136 | + logger.log(Level.INFO, "OpenCensus: Trace Context Deserialization Exception: " + exn); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Wrapper class for {@link MessageReceiver} that decodes any received trace and tag contexts and |
| 142 | + * puts them in scope. |
| 143 | + */ |
| 144 | + public static class OpenCensusMessageReceiver implements MessageReceiver { |
| 145 | + private final MessageReceiver receiver; |
| 146 | + |
| 147 | + public OpenCensusMessageReceiver(MessageReceiver receiver) { |
| 148 | + this.receiver = receiver; |
| 149 | + } |
| 150 | + |
| 151 | + @Override |
| 152 | + public void receiveMessage(PubsubMessage message, AckReplyConsumer consumer) { |
| 153 | + String encodedTagContext = message.getAttributesOrDefault(TAG_CONTEXT_KEY, ""); |
| 154 | + if (encodedTagContext.isEmpty()) { |
| 155 | + addTraceScope(message, consumer); |
| 156 | + return; |
| 157 | + } |
| 158 | + try (Scope statsScope = createScopedTagContext(encodedTagContext)) { |
| 159 | + addTraceScope(message, consumer); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + private void addTraceScope(PubsubMessage message, AckReplyConsumer consumer) { |
| 164 | + String encodedSpanContext = message.getAttributesOrDefault(TRACE_CONTEXT_KEY, ""); |
| 165 | + if (encodedSpanContext.isEmpty()) { |
| 166 | + receiver.receiveMessage(message, consumer); |
| 167 | + return; |
| 168 | + } |
| 169 | + try (Scope spanScope = createScopedSpan(MESSAGE_RECEIVER_SPAN_NAME)) { |
| 170 | + addParentLink(encodedSpanContext); |
| 171 | + receiver.receiveMessage(message, consumer); |
| 172 | + } |
| 173 | + } |
| 174 | + } |
| 175 | +} |
0 commit comments