diff --git a/utils/build/docker/java/parametric/src/main/java/com/datadoghq/trace/opentelemetry/controller/OpenTelemetryController.java b/utils/build/docker/java/parametric/src/main/java/com/datadoghq/trace/opentelemetry/controller/OpenTelemetryController.java index 1fba249e0e..d5655197db 100644 --- a/utils/build/docker/java/parametric/src/main/java/com/datadoghq/trace/opentelemetry/controller/OpenTelemetryController.java +++ b/utils/build/docker/java/parametric/src/main/java/com/datadoghq/trace/opentelemetry/controller/OpenTelemetryController.java @@ -6,26 +6,13 @@ import static com.datadoghq.trace.opentelemetry.controller.OpenTelemetryTypeHelper.parseSpanKindNumber; import static java.util.concurrent.TimeUnit.MICROSECONDS; -import com.datadoghq.trace.opentelemetry.dto.AddEventArgs; -import com.datadoghq.trace.opentelemetry.dto.EndSpanArgs; -import com.datadoghq.trace.opentelemetry.dto.FlushArgs; -import com.datadoghq.trace.opentelemetry.dto.FlushResult; -import com.datadoghq.trace.opentelemetry.dto.IsRecordingArgs; -import com.datadoghq.trace.opentelemetry.dto.IsRecordingResult; -import com.datadoghq.trace.opentelemetry.dto.RecordExceptionArgs; -import com.datadoghq.trace.opentelemetry.dto.SetAttributesArgs; -import com.datadoghq.trace.opentelemetry.dto.SetNameArgs; -import com.datadoghq.trace.opentelemetry.dto.SetStatusArgs; -import com.datadoghq.trace.opentelemetry.dto.SpanContextArgs; -import com.datadoghq.trace.opentelemetry.dto.SpanContextResult; -import com.datadoghq.trace.opentelemetry.dto.SpanLink; -import com.datadoghq.trace.opentelemetry.dto.StartSpanArgs; -import com.datadoghq.trace.opentelemetry.dto.StartSpanResult; +import com.datadoghq.trace.opentelemetry.dto.*; import datadog.trace.api.DDSpanId; import datadog.trace.api.DDTraceId; import datadog.trace.api.GlobalTracer; import datadog.trace.api.internal.InternalTracer; import io.opentelemetry.api.GlobalOpenTelemetry; +import io.opentelemetry.api.baggage.Baggage; import io.opentelemetry.api.trace.Span; import io.opentelemetry.api.trace.SpanBuilder; import io.opentelemetry.api.trace.SpanContext; @@ -44,10 +31,12 @@ public class OpenTelemetryController { private final Tracer tracer; private final Map spans; + private Baggage baggage; public OpenTelemetryController() { this.tracer = GlobalOpenTelemetry.getTracer("java-client"); this.spans = new HashMap<>(); + this.baggage = Baggage.empty(); } @PostMapping("start_span") @@ -210,6 +199,45 @@ public FlushResult flush(@RequestBody FlushArgs args) { } } + @PostMapping("set_baggage") + public void setBaggage(@RequestBody SetBaggageArgs args) { + LOGGER.info("Setting OTel baggage: {}", args); + this.baggage = this.baggage + .toBuilder() + .put(args.key(), args.value()) + .build(); + } + + @GetMapping("get_baggage") + public GetBaggageResult getBaggage(@RequestBody GetBaggageArgs args) { + LOGGER.info("Getting an OTel baggage entry"); + var value = this.baggage.getEntryValue(args.key()); + return new GetBaggageResult(value); + } + + @GetMapping("get_all_baggage") + public GetAllBaggageResult getAllBaggage() { + LOGGER.info("Getting all OTel baggage entries"); + Map baggageMap = new HashMap<>(); + this.baggage.forEach((key, entry) -> baggageMap.put(key, entry.getValue())); + return new GetAllBaggageResult(baggageMap); + } + + @PostMapping("remove_baggage") + public void removeBaggage(@RequestBody RemoveBaggageArgs args) { + LOGGER.info("Removing OTel baggage entry: {}", args); + this.baggage = this.baggage + .toBuilder() + .remove(args.key()) + .build(); + } + + @PostMapping("remove_all_baggage") + public void removeAllBaggage() { + LOGGER.info("Removing all OTel baggage entries"); + this.baggage = Baggage.empty(); + } + private Span getSpan(long spanId) { Span span = this.spans.get(spanId); if (span == null) { diff --git a/utils/build/docker/java/parametric/src/main/java/com/datadoghq/trace/opentelemetry/dto/GetAllBaggageResult.java b/utils/build/docker/java/parametric/src/main/java/com/datadoghq/trace/opentelemetry/dto/GetAllBaggageResult.java new file mode 100644 index 0000000000..6268e6b114 --- /dev/null +++ b/utils/build/docker/java/parametric/src/main/java/com/datadoghq/trace/opentelemetry/dto/GetAllBaggageResult.java @@ -0,0 +1,7 @@ +package com.datadoghq.trace.opentelemetry.dto; + +import java.util.Map; + +public record GetAllBaggageResult( + Map baggage) { +} diff --git a/utils/build/docker/java/parametric/src/main/java/com/datadoghq/trace/opentelemetry/dto/GetBaggageArgs.java b/utils/build/docker/java/parametric/src/main/java/com/datadoghq/trace/opentelemetry/dto/GetBaggageArgs.java new file mode 100644 index 0000000000..a3eaa46f1f --- /dev/null +++ b/utils/build/docker/java/parametric/src/main/java/com/datadoghq/trace/opentelemetry/dto/GetBaggageArgs.java @@ -0,0 +1,5 @@ +package com.datadoghq.trace.opentelemetry.dto; + +public record GetBaggageArgs( + String key) { +} diff --git a/utils/build/docker/java/parametric/src/main/java/com/datadoghq/trace/opentelemetry/dto/GetBaggageResult.java b/utils/build/docker/java/parametric/src/main/java/com/datadoghq/trace/opentelemetry/dto/GetBaggageResult.java new file mode 100644 index 0000000000..633302064f --- /dev/null +++ b/utils/build/docker/java/parametric/src/main/java/com/datadoghq/trace/opentelemetry/dto/GetBaggageResult.java @@ -0,0 +1,6 @@ +package com.datadoghq.trace.opentelemetry.dto; + +public record GetBaggageResult( + String baggage) { +} + diff --git a/utils/build/docker/java/parametric/src/main/java/com/datadoghq/trace/opentelemetry/dto/RemoveBaggageArgs.java b/utils/build/docker/java/parametric/src/main/java/com/datadoghq/trace/opentelemetry/dto/RemoveBaggageArgs.java new file mode 100644 index 0000000000..bae4b7d65e --- /dev/null +++ b/utils/build/docker/java/parametric/src/main/java/com/datadoghq/trace/opentelemetry/dto/RemoveBaggageArgs.java @@ -0,0 +1,5 @@ +package com.datadoghq.trace.opentelemetry.dto; + +public record RemoveBaggageArgs( + String key) { +} diff --git a/utils/build/docker/java/parametric/src/main/java/com/datadoghq/trace/opentelemetry/dto/SetBaggageArgs.java b/utils/build/docker/java/parametric/src/main/java/com/datadoghq/trace/opentelemetry/dto/SetBaggageArgs.java new file mode 100644 index 0000000000..7d1ce9008e --- /dev/null +++ b/utils/build/docker/java/parametric/src/main/java/com/datadoghq/trace/opentelemetry/dto/SetBaggageArgs.java @@ -0,0 +1,7 @@ +package com.datadoghq.trace.opentelemetry.dto; + +public record SetBaggageArgs( + String key, + String value) { +} + diff --git a/utils/build/docker/java/parametric/src/main/java/com/datadoghq/trace/opentracing/controller/OpenTracingController.java b/utils/build/docker/java/parametric/src/main/java/com/datadoghq/trace/opentracing/controller/OpenTracingController.java index aa419b2b59..8c85c431cb 100644 --- a/utils/build/docker/java/parametric/src/main/java/com/datadoghq/trace/opentracing/controller/OpenTracingController.java +++ b/utils/build/docker/java/parametric/src/main/java/com/datadoghq/trace/opentracing/controller/OpenTracingController.java @@ -8,18 +8,7 @@ import static java.util.Collections.emptyList; import static java.util.stream.Collectors.toCollection; -import com.datadoghq.trace.opentracing.dto.KeyValue; -import com.datadoghq.trace.opentracing.dto.SpanErrorArgs; -import com.datadoghq.trace.opentracing.dto.SpanFinishArgs; -import com.datadoghq.trace.opentracing.dto.SpanInjectHeadersArgs; -import com.datadoghq.trace.opentracing.dto.SpanInjectHeadersResult; -import com.datadoghq.trace.opentracing.dto.SpanExtractHeadersArgs; -import com.datadoghq.trace.opentracing.dto.SpanExtractHeadersResult; -import com.datadoghq.trace.opentracing.dto.SpanSetMetaArgs; -import com.datadoghq.trace.opentracing.dto.SpanSetMetricArgs; -import com.datadoghq.trace.opentracing.dto.SpanSetResourceArgs; -import com.datadoghq.trace.opentracing.dto.StartSpanArgs; -import com.datadoghq.trace.opentracing.dto.StartSpanResult; +import com.datadoghq.trace.opentracing.dto.*; import datadog.trace.api.DDSpanId; import datadog.trace.api.DDTags; import datadog.trace.api.DDTraceId; @@ -198,6 +187,61 @@ public void flushSpans() { } } + @PostMapping("set_baggage") + public void setBaggage(@RequestBody SpanSetBaggageArgs args) { + LOGGER.info("Setting baggage item for OT span: {}", args); + Span span = getSpan(args.spanId()); + if (span != null && args.key() != null) { + span.setBaggageItem(args.key(), args.value()); + } + } + + @GetMapping("get_baggage") + public SpanGetBaggageResult getBaggage(@RequestBody SpanGetBaggageArgs args) { + LOGGER.info("Getting single baggage item for OT span: {}", args); + Span span = getSpan(args.spanId()); + if (span != null && args.key() != null) { + return new SpanGetBaggageResult(span.getBaggageItem(args.key())); + } + return null; + } + + @GetMapping("get_all_baggage") + public SpanGetAllBaggageResult getAllBaggage(@RequestBody SpanGetAllBaggageArgs args) { + LOGGER.info("Getting all baggage items for OT span: {}", args); + Span span = getSpan(args.spanId()); + if (span != null) { + Map baggageMap = new HashMap<>(); + + for (var entry : span.context().baggageItems()) { + baggageMap.put(entry.getKey(), entry.getValue()); + } + + return new SpanGetAllBaggageResult(baggageMap); + } + return null; + } + + @PostMapping("remove_baggage") + public void removeBaggage(@RequestBody SpanRemoveBaggageArgs args) { + LOGGER.info("Removing single baggage item for OT span: {}", args); + Span span = getSpan(args.spanId()); + if (span != null && args.key() != null) { + span.setBaggageItem(args.key(), null); + } + } + + @PostMapping("remove_all_baggage") + public void removeAllBaggage(@RequestBody SpanRemoveAllBaggageArgs args) { + LOGGER.info("Removing all baggage items for OT span: {}", args); + Span span = getSpan(args.spanId()); + if (span != null) { + for (var entry : span.context().baggageItems()) { + span.setBaggageItem(entry.getKey(), null); + } + } + } + private Span getSpan(long spanId) { Span span = this.spans.get(spanId); if (span == null) { diff --git a/utils/build/docker/java/parametric/src/main/java/com/datadoghq/trace/opentracing/dto/SpanGetAllBaggageArgs.java b/utils/build/docker/java/parametric/src/main/java/com/datadoghq/trace/opentracing/dto/SpanGetAllBaggageArgs.java new file mode 100644 index 0000000000..5b74c5a4ac --- /dev/null +++ b/utils/build/docker/java/parametric/src/main/java/com/datadoghq/trace/opentracing/dto/SpanGetAllBaggageArgs.java @@ -0,0 +1,8 @@ +package com.datadoghq.trace.opentracing.dto; + +import com.fasterxml.jackson.annotation.JsonProperty; + +public record SpanGetAllBaggageArgs( + @JsonProperty("span_id") long spanId) { +} + diff --git a/utils/build/docker/java/parametric/src/main/java/com/datadoghq/trace/opentracing/dto/SpanGetAllBaggageResult.java b/utils/build/docker/java/parametric/src/main/java/com/datadoghq/trace/opentracing/dto/SpanGetAllBaggageResult.java new file mode 100644 index 0000000000..2ce5fc7067 --- /dev/null +++ b/utils/build/docker/java/parametric/src/main/java/com/datadoghq/trace/opentracing/dto/SpanGetAllBaggageResult.java @@ -0,0 +1,7 @@ +package com.datadoghq.trace.opentracing.dto; + +import java.util.Map; + +public record SpanGetAllBaggageResult( + Map baggage) { +} diff --git a/utils/build/docker/java/parametric/src/main/java/com/datadoghq/trace/opentracing/dto/SpanGetBaggageArgs.java b/utils/build/docker/java/parametric/src/main/java/com/datadoghq/trace/opentracing/dto/SpanGetBaggageArgs.java new file mode 100644 index 0000000000..2df4dbec79 --- /dev/null +++ b/utils/build/docker/java/parametric/src/main/java/com/datadoghq/trace/opentracing/dto/SpanGetBaggageArgs.java @@ -0,0 +1,10 @@ +package com.datadoghq.trace.opentracing.dto; + +import com.fasterxml.jackson.annotation.JsonProperty; + +public record SpanGetBaggageArgs( + @JsonProperty("span_id") long spanId, + String key) { +} + + diff --git a/utils/build/docker/java/parametric/src/main/java/com/datadoghq/trace/opentracing/dto/SpanGetBaggageResult.java b/utils/build/docker/java/parametric/src/main/java/com/datadoghq/trace/opentracing/dto/SpanGetBaggageResult.java new file mode 100644 index 0000000000..f8eecfe7b7 --- /dev/null +++ b/utils/build/docker/java/parametric/src/main/java/com/datadoghq/trace/opentracing/dto/SpanGetBaggageResult.java @@ -0,0 +1,5 @@ +package com.datadoghq.trace.opentracing.dto; + +public record SpanGetBaggageResult( + String baggage) { +} diff --git a/utils/build/docker/java/parametric/src/main/java/com/datadoghq/trace/opentracing/dto/SpanRemoveAllBaggageArgs.java b/utils/build/docker/java/parametric/src/main/java/com/datadoghq/trace/opentracing/dto/SpanRemoveAllBaggageArgs.java new file mode 100644 index 0000000000..a5750327d2 --- /dev/null +++ b/utils/build/docker/java/parametric/src/main/java/com/datadoghq/trace/opentracing/dto/SpanRemoveAllBaggageArgs.java @@ -0,0 +1,7 @@ +package com.datadoghq.trace.opentracing.dto; + +import com.fasterxml.jackson.annotation.JsonProperty; + +public record SpanRemoveAllBaggageArgs( + @JsonProperty("span_id") long spanId) { +} diff --git a/utils/build/docker/java/parametric/src/main/java/com/datadoghq/trace/opentracing/dto/SpanRemoveBaggageArgs.java b/utils/build/docker/java/parametric/src/main/java/com/datadoghq/trace/opentracing/dto/SpanRemoveBaggageArgs.java new file mode 100644 index 0000000000..a3cb507830 --- /dev/null +++ b/utils/build/docker/java/parametric/src/main/java/com/datadoghq/trace/opentracing/dto/SpanRemoveBaggageArgs.java @@ -0,0 +1,9 @@ +package com.datadoghq.trace.opentracing.dto; + +import com.fasterxml.jackson.annotation.JsonProperty; + +public record SpanRemoveBaggageArgs( + @JsonProperty("span_id") long spanId, + String key) { +} + diff --git a/utils/build/docker/java/parametric/src/main/java/com/datadoghq/trace/opentracing/dto/SpanSetBaggageArgs.java b/utils/build/docker/java/parametric/src/main/java/com/datadoghq/trace/opentracing/dto/SpanSetBaggageArgs.java new file mode 100644 index 0000000000..1dc9e740d7 --- /dev/null +++ b/utils/build/docker/java/parametric/src/main/java/com/datadoghq/trace/opentracing/dto/SpanSetBaggageArgs.java @@ -0,0 +1,10 @@ +package com.datadoghq.trace.opentracing.dto; + +import com.fasterxml.jackson.annotation.JsonProperty; + +public record SpanSetBaggageArgs( + @JsonProperty("span_id") long spanId, + String key, + String value) { +} +