|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package org.apache.beam.runners.dataflow.worker; |
| 19 | + |
| 20 | +import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator; |
| 21 | +import io.opentelemetry.context.Context; |
| 22 | +import io.opentelemetry.context.propagation.TextMapGetter; |
| 23 | +import io.opentelemetry.context.propagation.TextMapSetter; |
| 24 | +import org.apache.beam.model.fnexecution.v1.BeamFnApi; |
| 25 | +import org.apache.beam.sdk.annotations.Internal; |
| 26 | +import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.collect.Lists; |
| 27 | +import org.checkerframework.checker.nullness.qual.Nullable; |
| 28 | + |
| 29 | +@Internal |
| 30 | +public class WindmillOpenTelemetryContextPropagator { |
| 31 | + |
| 32 | + public static final String TRACEPARENT = "traceparent"; |
| 33 | + public static final String TRACESTATE = "tracestate"; |
| 34 | + private static final TextMapSetter<BeamFnApi.Elements.ElementMetadata.Builder> SETTER = |
| 35 | + (carrier, key, value) -> { |
| 36 | + if (carrier == null) { |
| 37 | + return; |
| 38 | + } |
| 39 | + if (TRACEPARENT.equals(key)) { |
| 40 | + carrier.setTraceparent(value); |
| 41 | + } else if (TRACESTATE.equals(key)) { |
| 42 | + carrier.setTracestate(value); |
| 43 | + } |
| 44 | + }; |
| 45 | + |
| 46 | + private static final TextMapGetter<BeamFnApi.Elements.ElementMetadata> GETTER = |
| 47 | + new TextMapGetter<BeamFnApi.Elements.ElementMetadata>() { |
| 48 | + @Override |
| 49 | + public Iterable<String> keys(BeamFnApi.Elements.ElementMetadata carrier) { |
| 50 | + return Lists.newArrayList(TRACEPARENT, TRACESTATE); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public @Nullable String get( |
| 55 | + BeamFnApi.Elements.@Nullable ElementMetadata carrier, String key) { |
| 56 | + if (carrier == null) { |
| 57 | + return null; |
| 58 | + } |
| 59 | + if (TRACEPARENT.equalsIgnoreCase(key)) { |
| 60 | + return carrier.getTraceparent(); |
| 61 | + } else if (TRACESTATE.equalsIgnoreCase(key)) { |
| 62 | + return carrier.getTracestate(); |
| 63 | + } |
| 64 | + return null; |
| 65 | + } |
| 66 | + }; |
| 67 | + |
| 68 | + public static void set(Context from, BeamFnApi.Elements.ElementMetadata.Builder builder) { |
| 69 | + W3CTraceContextPropagator.getInstance().inject(from, builder, SETTER); |
| 70 | + } |
| 71 | + |
| 72 | + public static Context read(BeamFnApi.Elements.ElementMetadata from) { |
| 73 | + return W3CTraceContextPropagator.getInstance().extract(Context.root(), from, GETTER); |
| 74 | + } |
| 75 | +} |
0 commit comments