reuvenlax commented on code in PR #41739:
URL: https://github.com/apache/beam/pull/41739#discussion_r4773569066


##########
sdks/java/core/src/main/java/org/apache/beam/sdk/values/WindowedValues.java:
##########
@@ -1114,6 +1117,168 @@ public <NewT> WindowedValueCoder<NewT> 
withValueCoder(Coder<NewT> valueCoder) {
       return new FullWindowedValueCoder<>(valueCoder, windowCoder);
     }
 
+    private static final int VALUE_KIND_COUNT = ValueKind.values().length;
+
+    private static final byte[] @Nullable [] 
NON_TRACED_LENGTH_PREFIXED_METADATA =
+        buildNonTracedLengthPrefixedMetadata();
+
+    private static byte[] @Nullable [] buildNonTracedLengthPrefixedMetadata() {
+      ValueKind[] valueKinds = ValueKind.values();
+      byte[] @Nullable [] table = new byte[VALUE_KIND_COUNT * 2][];
+      for (int drainBit = 0; drainBit <= 1; drainBit++) {
+        CausedByDrain drain = drainBit == 1 ? CausedByDrain.CAUSED_BY_DRAIN : 
CausedByDrain.NORMAL;
+        for (ValueKind vk : valueKinds) {
+          if (drain == CausedByDrain.NORMAL && vk == ValueKind.INSERT) {
+            continue;
+          }
+          BeamFnApi.Elements.ElementMetadata.Builder builder =
+              BeamFnApi.Elements.ElementMetadata.newBuilder();
+          if (drain == CausedByDrain.CAUSED_BY_DRAIN) {
+            builder.setDrain(BeamFnApi.Elements.DrainMode.Enum.DRAINING);
+          }
+          if (vk != ValueKind.INSERT) {
+            builder.setValueKind(ValueKindUtil.toProto(vk));
+          }
+          ByteArrayOutputStream baos = new ByteArrayOutputStream(8);
+          try {
+            ByteArrayCoder.of().encode(builder.buildPartial().toByteArray(), 
baos);
+          } catch (IOException e) {
+            throw new ExceptionInInitializerError(e);
+          }
+          table[nonTracedMetadataIndex(drain, vk)] = baos.toByteArray();
+        }
+      }
+      return table;
+    }
+
+    private static int nonTracedMetadataIndex(CausedByDrain causedByDrain, 
ValueKind valueKind) {
+      return (causedByDrain == CausedByDrain.CAUSED_BY_DRAIN ? 
VALUE_KIND_COUNT : 0)
+          + valueKind.ordinal();
+    }
+
+    /**
+     * Returns whether {@code openTelemetryContext} carries a valid W3C trace 
{@link Span} that
+     * {@link OpenTelemetryContextPropagator} will serialize.
+     */
+    @Internal
+    public static boolean hasValidTraceContext(
+        io.opentelemetry.context.@Nullable Context openTelemetryContext) {
+      return openTelemetryContext != null
+          && openTelemetryContext != io.opentelemetry.context.Context.root()
+          && Span.fromContext(openTelemetryContext).getSpanContext().isValid();
+    }
+
+    /**
+     * Returns whether any {@code ElementMetadata} field differs from its wire 
default ({@link
+     * CausedByDrain#NORMAL}, no valid OpenTelemetry trace {@link Span}, 
{@link ValueKind#INSERT}).
+     * When {@code false}, encoders omit the {@code ElementMetadata} payload 
and clear the {@code
+     * 0x80} metadata bit in {@link PaneInfo} per {@code 
beam:coder:windowed_value:v2}.
+     */
+    @Internal
+    public static boolean hasNonDefaultElementMetadata(
+        CausedByDrain causedByDrain,
+        io.opentelemetry.context.@Nullable Context openTelemetryContext,
+        ValueKind valueKind) {
+      return causedByDrain == CausedByDrain.CAUSED_BY_DRAIN
+          || valueKind != ValueKind.INSERT
+          || hasValidTraceContext(openTelemetryContext);
+    }
+
+    static void encodeElementMetadata(
+        CausedByDrain causedByDrain,
+        io.opentelemetry.context.@Nullable Context openTelemetryContext,
+        ValueKind valueKind,
+        OutputStream outStream)
+        throws IOException {
+      boolean hasNonDefaultDrainOrKind =
+          causedByDrain == CausedByDrain.CAUSED_BY_DRAIN || valueKind != 
ValueKind.INSERT;
+      // Callers already verified hasNonDefaultElementMetadata(...), so if 
drain and valueKind are
+      // both default, openTelemetryContext is guaranteed to carry a valid 
trace context.
+      boolean hasValidTrace =
+          !hasNonDefaultDrainOrKind || 
hasValidTraceContext(openTelemetryContext);
+      if (!hasValidTrace) {
+        byte @Nullable [] precomputed =
+            
NON_TRACED_LENGTH_PREFIXED_METADATA[nonTracedMetadataIndex(causedByDrain, 
valueKind)];
+        if (precomputed != null) {
+          outStream.write(precomputed);
+          return;
+        }
+      }
+      BeamFnApi.Elements.ElementMetadata.Builder builder =
+          BeamFnApi.Elements.ElementMetadata.newBuilder();
+      if (hasValidTrace && openTelemetryContext != null) {
+        OpenTelemetryContextPropagator.set(openTelemetryContext, builder);
+      }
+      if (causedByDrain == CausedByDrain.CAUSED_BY_DRAIN) {
+        builder.setDrain(BeamFnApi.Elements.DrainMode.Enum.DRAINING);
+      }
+      if (valueKind != ValueKind.INSERT) {
+        builder.setValueKind(ValueKindUtil.toProto(valueKind));
+      }
+      ByteArrayCoder.of().encode(builder.buildPartial().toByteArray(), 
outStream);

Review Comment:
   done



##########
runners/google-cloud-dataflow-java/worker/src/main/java/org/apache/beam/runners/dataflow/worker/WindmillOpenTelemetryContextPropagator.java:
##########
@@ -56,9 +58,9 @@ public Iterable<String> 
keys(BeamFnApi.Elements.ElementMetadata carrier) {
           if (carrier == null) {
             return null;
           }
-          if (TRACEPARENT.equalsIgnoreCase(key)) {
+          if (TRACEPARENT.equals(key)) {
             return carrier.getTraceparent();
-          } else if (TRACESTATE.equalsIgnoreCase(key)) {
+          } else if (TRACESTATE.equals(key)) {
             return carrier.getTracestate();

Review Comment:
   what do you prefer? I can change it back, but worth noting that 
W3CTraceContextPropagator also queries lowercase traceparent and tracestate 
(e.g. W3CTraceContextPropagator.getInstance().extract()



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to