-
Notifications
You must be signed in to change notification settings - Fork 1
Add tracing support with OpenTelemetry #58
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
29ab4d0
Add tracing support with OpenTelemetry
itssimon 9fb2cfd
Tweaks
itssimon 6238873
Fix formatting
itssimon bf30160
Merge branch 'main' into simon/dev-11-add-tracing-support-to-java-sdk
itssimon a20b8b3
Add more tests
itssimon 0ad2069
Tweaks
itssimon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,180 @@ | ||
| package io.apitally.common; | ||
|
|
||
| import io.apitally.common.dto.SpanData; | ||
| import io.opentelemetry.api.trace.Span; | ||
| import io.opentelemetry.api.trace.SpanContext; | ||
| import io.opentelemetry.api.trace.SpanKind; | ||
| import io.opentelemetry.api.trace.StatusCode; | ||
| import io.opentelemetry.api.trace.Tracer; | ||
| import io.opentelemetry.context.Context; | ||
| import io.opentelemetry.context.Scope; | ||
| import io.opentelemetry.sdk.trace.ReadWriteSpan; | ||
| import io.opentelemetry.sdk.trace.ReadableSpan; | ||
| import io.opentelemetry.sdk.trace.SpanProcessor; | ||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
| import java.util.concurrent.ConcurrentLinkedQueue; | ||
|
|
||
| public class SpanCollector implements SpanProcessor { | ||
| private final boolean enabled; | ||
| private volatile Tracer tracer; | ||
| private final Map<String, Set<String>> includedSpanIds = new ConcurrentHashMap<>(); | ||
| private final Map<String, ConcurrentLinkedQueue<SpanData>> collectedSpans = | ||
| new ConcurrentHashMap<>(); | ||
|
|
||
| public SpanCollector(boolean enabled) { | ||
| this.enabled = enabled; | ||
| } | ||
|
|
||
| public void setTracer(Tracer tracer) { | ||
| this.tracer = tracer; | ||
| } | ||
|
|
||
| public SpanHandle startCollection() { | ||
| if (!enabled || tracer == null) { | ||
| return null; | ||
| } | ||
|
|
||
| Span span = tracer.spanBuilder("root").setSpanKind(SpanKind.INTERNAL).startSpan(); | ||
| Scope scope = Context.current().with(span).makeCurrent(); | ||
| SpanContext spanContext = span.getSpanContext(); | ||
| String traceId = spanContext.getTraceId(); | ||
|
|
||
| Set<String> spanIds = ConcurrentHashMap.newKeySet(); | ||
| spanIds.add(spanContext.getSpanId()); | ||
| includedSpanIds.put(traceId, spanIds); | ||
| collectedSpans.put(traceId, new ConcurrentLinkedQueue<>()); | ||
|
|
||
| return new SpanHandle(traceId, span, scope, this); | ||
| } | ||
|
|
||
| List<SpanData> getAndClearSpans(String traceId) { | ||
| if (traceId == null) { | ||
| return null; | ||
| } | ||
|
|
||
| includedSpanIds.remove(traceId); | ||
| ConcurrentLinkedQueue<SpanData> spans = collectedSpans.remove(traceId); | ||
| return spans != null ? new ArrayList<>(spans) : null; | ||
| } | ||
|
|
||
| @Override | ||
| public void onStart(Context parentContext, ReadWriteSpan span) { | ||
| if (!enabled) { | ||
| return; | ||
| } | ||
|
|
||
| SpanContext spanContext = span.getSpanContext(); | ||
| String traceId = spanContext.getTraceId(); | ||
| String spanId = spanContext.getSpanId(); | ||
|
|
||
| Set<String> included = includedSpanIds.get(traceId); | ||
| if (included == null) { | ||
| return; | ||
| } | ||
|
|
||
| SpanContext parentSpanContext = span.getParentSpanContext(); | ||
| if (parentSpanContext.isValid() && included.contains(parentSpanContext.getSpanId())) { | ||
| included.add(spanId); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isStartRequired() { | ||
| return enabled; | ||
| } | ||
|
|
||
| @Override | ||
| public void onEnd(ReadableSpan span) { | ||
| if (!enabled) { | ||
| return; | ||
| } | ||
|
|
||
| SpanContext spanContext = span.getSpanContext(); | ||
| String traceId = spanContext.getTraceId(); | ||
| String spanId = spanContext.getSpanId(); | ||
|
|
||
| Set<String> included = includedSpanIds.get(traceId); | ||
| if (included == null || !included.contains(spanId)) { | ||
| return; | ||
| } | ||
|
|
||
| SpanData data = serializeSpan(span); | ||
| ConcurrentLinkedQueue<SpanData> spans = collectedSpans.get(traceId); | ||
| if (spans != null) { | ||
| spans.add(data); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isEndRequired() { | ||
| return enabled; | ||
| } | ||
|
|
||
| private SpanData serializeSpan(ReadableSpan span) { | ||
| io.opentelemetry.sdk.trace.data.SpanData spanData = span.toSpanData(); | ||
| SpanContext spanContext = spanData.getSpanContext(); | ||
| SpanContext parentSpanContext = spanData.getParentSpanContext(); | ||
|
|
||
| String parentSpanId = parentSpanContext.isValid() ? parentSpanContext.getSpanId() : null; | ||
| String status = | ||
| spanData.getStatus().getStatusCode() != StatusCode.UNSET | ||
| ? spanData.getStatus().getStatusCode().name() | ||
| : null; | ||
|
|
||
| Map<String, Object> attributes = null; | ||
| if (!spanData.getAttributes().isEmpty()) { | ||
| Map<String, Object> attrMap = new HashMap<>(); | ||
| spanData.getAttributes().forEach((key, value) -> attrMap.put(key.getKey(), value)); | ||
| attributes = attrMap; | ||
| } | ||
|
|
||
| return new SpanData( | ||
| spanContext.getSpanId(), | ||
| parentSpanId, | ||
| spanData.getName(), | ||
| spanData.getKind().name(), | ||
| spanData.getStartEpochNanos(), | ||
| spanData.getEndEpochNanos(), | ||
| status, | ||
| attributes); | ||
| } | ||
|
|
||
| public static class SpanHandle { | ||
| private final String traceId; | ||
| private final Span span; | ||
| private final Scope scope; | ||
| private final SpanCollector collector; | ||
|
|
||
| SpanHandle(String traceId, Span span, Scope scope, SpanCollector collector) { | ||
| this.traceId = traceId; | ||
| this.span = span; | ||
| this.scope = scope; | ||
| this.collector = collector; | ||
| } | ||
|
|
||
| public String getTraceId() { | ||
| return traceId; | ||
| } | ||
|
|
||
| public void setName(String name) { | ||
| span.updateName(name); | ||
| } | ||
|
|
||
| public List<SpanData> end() { | ||
| scope.close(); | ||
| span.end(); | ||
| return collector.getAndClearSpans(traceId); | ||
| } | ||
| } | ||
|
|
||
| void resetForTest() { | ||
| this.tracer = null; | ||
| this.includedSpanIds.clear(); | ||
| this.collectedSpans.clear(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.