Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Java][parametric] add OpenTracing and OpenTelemetry baggage endpoints #3535

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,13 @@
import static io.opentelemetry.api.trace.SpanKind.SERVER;
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.KeyValue;
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.common.Attributes;
import io.opentelemetry.api.common.AttributesBuilder;
import io.opentelemetry.api.trace.Span;
Expand Down Expand Up @@ -58,11 +44,13 @@ public class OpenTelemetryController {
private final Tracer tracer;
private final TextMapPropagator propagator;
private final Map<Long, Span> spans;
private Baggage baggage;
Copy link
Member Author

@lucaspimentel lucaspimentel Nov 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With Spring, does this survive across multiple requests to the same controller? In the built-in .NET MVC frameworks, a new controller instance is created for each request, so these are static members.

private static readonly Dictionary<ulong, ISpan> Spans = new();
private static readonly Dictionary<ulong, Datadog.Trace.ISpanContext> DDContexts = new();


public OpenTelemetryController() {
this.tracer = GlobalOpenTelemetry.getTracer("java-client");
this.propagator = GlobalOpenTelemetry.getPropagators().getTextMapPropagator();
this.spans = new HashMap<>();
this.baggage = Baggage.empty();
}

private static SpanKind parseSpanKindNumber(int spanKindNumber) {
Expand Down Expand Up @@ -318,6 +306,50 @@ public FlushResult flush(@RequestBody FlushArgs args) {
}
}

// set_baggage
@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();
}

// get_baggage
@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);
}

// get_all_baggage
@GetMapping("get_all_baggage")
public GetAllBaggageResult getAllBaggage() {
LOGGER.info("Getting all OTel baggage entries");
Map<String, String> baggageMap = new HashMap<>();
this.baggage.forEach((key, entry) -> baggageMap.put(key, entry.getValue()));
return new GetAllBaggageResult(baggageMap);
}

// remove_baggage
@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();
}

// remove_all_baggage
@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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.datadoghq.trace.opentelemetry.dto;

import java.util.Map;

public record GetAllBaggageResult(
Map<String, String> baggage) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.datadoghq.trace.opentelemetry.dto;

public record GetBaggageArgs(
String key) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.datadoghq.trace.opentelemetry.dto;

public record GetBaggageResult(
String baggage) {
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.datadoghq.trace.opentelemetry.dto;

public record RemoveBaggageArgs(
String key) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.datadoghq.trace.opentelemetry.dto;

public record SetBaggageArgs(
String key,
String value) {
}

Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,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;
Expand Down Expand Up @@ -196,6 +185,60 @@ 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<String, String> baggageMap = new HashMap<>();

for (var entry : span.context().baggageItems()) {
baggageMap.put(entry.getKey(), entry.getValue());
}

return new SpanGetAllBaggageResult(baggageMap);
}
return null;
}

// /trace/span/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);
}
}

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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.datadoghq.trace.opentracing.dto;

import com.fasterxml.jackson.annotation.JsonProperty;

public record SpanGetAllBaggageArgs(
@JsonProperty("span_id") long spanId) {
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.datadoghq.trace.opentracing.dto;

import java.util.Map;

public record SpanGetAllBaggageResult(
Map<String, String> baggage) {
}
Original file line number Diff line number Diff line change
@@ -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) {
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.datadoghq.trace.opentracing.dto;

public record SpanGetBaggageResult(
String baggage) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.datadoghq.trace.opentracing.dto;

import java.util.Map;

public record SpanGetBaggageResults(
Map<String, String> baggage) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.datadoghq.trace.opentracing.dto;

import com.fasterxml.jackson.annotation.JsonProperty;

public record SpanRemoveAllBaggageArgs(
@JsonProperty("span_id")
long spanId) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.datadoghq.trace.opentracing.dto;

import com.fasterxml.jackson.annotation.JsonProperty;

public record SpanRemoveBaggageArgs(
@JsonProperty("span_id")
long spanId,
String key) {
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.datadoghq.trace.opentracing.dto;

import com.fasterxml.jackson.annotation.JsonProperty;

public record SpanSetBaggageArgs(
@JsonProperty("span_id")
long spanId,
String key,
String value) {
}