Skip to content

Commit

Permalink
[java][parametric] add OpenTracing and OpenTelemetry baggage endpoints (
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaspimentel authored Jan 9, 2025
1 parent ba3b2cc commit 0e1570e
Show file tree
Hide file tree
Showing 14 changed files with 185 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -44,10 +31,12 @@
public class OpenTelemetryController {
private final Tracer tracer;
private final Map<Long, Span> spans;
private Baggage baggage;

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

@PostMapping("start_span")
Expand Down Expand Up @@ -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<String, String> 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) {
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 @@ -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;
Expand Down Expand Up @@ -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<String, String> 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) {
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 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,9 @@
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,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) {
}

0 comments on commit 0e1570e

Please sign in to comment.