Skip to content

Commit

Permalink
add OpenTelemetry baggage endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaspimentel committed Dec 5, 2024
1 parent ba70912 commit ac256d8
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 16 deletions.
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;

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) {
}

0 comments on commit ac256d8

Please sign in to comment.