-
Notifications
You must be signed in to change notification settings - Fork 20
Add manual Otel instrumentation for Spans #837
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
4 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
|
|
||
| <parent> | ||
| <groupId>com.genexus</groupId> | ||
| <artifactId>parent</artifactId> | ||
| <version>${revision}${changelist}</version> | ||
| </parent> | ||
|
|
||
| <artifactId>gxobservability</artifactId> | ||
| <name>GeneXus Observability</name> | ||
|
|
||
| <dependencies> | ||
| <dependency> | ||
| <groupId>io.opentelemetry</groupId> | ||
| <artifactId>opentelemetry-api</artifactId> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>io.opentelemetry</groupId> | ||
| <artifactId>opentelemetry-sdk-trace</artifactId> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>io.opentelemetry</groupId> | ||
| <artifactId>opentelemetry-exporter-otlp</artifactId> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>io.opentelemetry</groupId> | ||
| <artifactId>opentelemetry-sdk</artifactId> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>io.opentelemetry</groupId> | ||
| <artifactId>opentelemetry-semconv</artifactId> | ||
| <version>1.30.1-alpha</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>io.opentelemetry</groupId> | ||
| <artifactId>opentelemetry-extension-annotations</artifactId> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>io.opentelemetry</groupId> | ||
| <artifactId>opentelemetry-sdk-extension-autoconfigure</artifactId> | ||
| <version>1.36.0</version> | ||
| </dependency> | ||
| </dependencies> | ||
| <dependencyManagement> | ||
| <dependencies> | ||
| <dependency> | ||
| <groupId>io.opentelemetry</groupId> | ||
| <artifactId>opentelemetry-bom</artifactId> | ||
| <version>1.23.0</version> | ||
| <type>pom</type> | ||
| <scope>import</scope> | ||
| </dependency> | ||
| </dependencies> | ||
| </dependencyManagement> | ||
|
|
||
| <build> | ||
| <finalName>gxobservability</finalName> | ||
| </build> | ||
| </project> | ||
33 changes: 33 additions & 0 deletions
33
gxobservability/src/main/java/com/genexus/opentelemetry/GXSpanContext.java
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,33 @@ | ||
| package com.genexus.opentelemetry; | ||
|
|
||
| import io.opentelemetry.api.trace.Span; | ||
| import io.opentelemetry.api.trace.Tracer; | ||
| import io.opentelemetry.api.GlobalOpenTelemetry; | ||
| import io.opentelemetry.api.OpenTelemetry; | ||
| import io.opentelemetry.api.trace.StatusCode; | ||
| import io.opentelemetry.context.Context; | ||
| public class GXSpanContext | ||
| { | ||
| private io.opentelemetry.api.trace.SpanContext _spanContext; | ||
|
|
||
| public io.opentelemetry.api.trace.SpanContext getSpanContext() | ||
| { | ||
| return _spanContext; | ||
| } | ||
| public GXSpanContext(io.opentelemetry.api.trace.SpanContext spanContext) | ||
| { | ||
| this._spanContext = spanContext; | ||
| } | ||
| public GXSpanContext() | ||
| { | ||
| _spanContext = Span.current().getSpanContext(); | ||
| } | ||
| public String traceId() | ||
| { | ||
| return _spanContext.getTraceId(); | ||
| } | ||
| public String spanId() | ||
| { | ||
| return _spanContext.getSpanId(); | ||
| } | ||
| } |
21 changes: 21 additions & 0 deletions
21
gxobservability/src/main/java/com/genexus/opentelemetry/GXTraceContext.java
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,21 @@ | ||
| package com.genexus.opentelemetry; | ||
|
|
||
| import io.opentelemetry.api.trace.Span; | ||
| import io.opentelemetry.api.trace.Tracer; | ||
| import io.opentelemetry.api.GlobalOpenTelemetry; | ||
| import io.opentelemetry.api.OpenTelemetry; | ||
| import io.opentelemetry.api.trace.StatusCode; | ||
| import io.opentelemetry.context.Context; | ||
|
|
||
| public class GXTraceContext | ||
| { | ||
| private Context context; | ||
| public GXTraceContext(io.opentelemetry.context.Context context) | ||
| { | ||
| this.context = context; | ||
| } | ||
| public Context getTraceContext() | ||
| { | ||
| return this.context; | ||
| } | ||
| } |
186 changes: 186 additions & 0 deletions
186
gxobservability/src/main/java/com/genexus/opentelemetry/OtelSpan.java
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,186 @@ | ||
| package com.genexus.opentelemetry; | ||
|
|
||
| import java.util.concurrent.atomic.AtomicReference; | ||
| import io.opentelemetry.api.trace.Span; | ||
| import io.opentelemetry.api.trace.Tracer; | ||
| import io.opentelemetry.api.GlobalOpenTelemetry; | ||
| import io.opentelemetry.api.OpenTelemetry; | ||
| import io.opentelemetry.api.trace.StatusCode; | ||
| import io.opentelemetry.context.Context; | ||
| import io.opentelemetry.context.Scope; | ||
| import io.opentelemetry.extension.annotations.SpanAttribute; | ||
| import io.opentelemetry.extension.annotations.WithSpan; | ||
| import io.opentelemetry.api.common.AttributeKey; | ||
| import io.opentelemetry.api.common.Attributes; | ||
| import io.opentelemetry.api.baggage.Baggage; | ||
| import io.opentelemetry.api.baggage.BaggageBuilder; | ||
| public class OtelSpan { | ||
| private Span span; | ||
| public enum SpanStatusCode | ||
| { | ||
| UNSET, | ||
| OK, | ||
| ERROR | ||
| } | ||
| public OtelSpan(Span span) | ||
| { | ||
| this.span=span; | ||
| } | ||
| public OtelSpan() | ||
| {} | ||
| //region EO Properties | ||
| public String getTraceId() | ||
| { | ||
| if (span != null) | ||
| return span.getSpanContext().getTraceId(); | ||
| return ""; | ||
| } | ||
| public String getSpanId() | ||
| { | ||
| if (span != null) | ||
| return span.getSpanContext().getSpanId(); | ||
| return ""; | ||
| } | ||
| public Boolean isRecording() | ||
| { | ||
| if (span != null) | ||
| return span.isRecording(); | ||
| return false; | ||
| } | ||
| public GXSpanContext getSpanContext() { | ||
| return new GXSpanContext(getSpanContext(span)); | ||
| } | ||
| //endregion | ||
| //region EO Methods | ||
| public void endSpan() | ||
| { | ||
| if (span!=null) | ||
| span.end(); | ||
| } | ||
| public GXTraceContext addBaggage(String key, String value) | ||
| { | ||
| return new GXTraceContext(addBaggageReturnContext(key, value)); | ||
| } | ||
| public String getBaggaeItem(String key,GXTraceContext gxTraceContext) | ||
| { | ||
| return getBaggaeItemInContext(gxTraceContext.getTraceContext(),key); | ||
| } | ||
| public GXTraceContext getGXTraceContext() | ||
| { | ||
| return new GXTraceContext(getContext()); | ||
| } | ||
| public void recordException(String message) | ||
| { | ||
| recordException(span,new Throwable(message)); | ||
| } | ||
| public void setStringAttribute(String key, String value) | ||
| { | ||
| if (span != null) | ||
| span.setAttribute(key,value); | ||
| } | ||
| public void setBooleanAttribute(String key, boolean value) | ||
| { | ||
| if (span != null) | ||
| span.setAttribute(key,value); | ||
| } | ||
| public void setDoubleAttribute(String key, double value) | ||
| { | ||
| if (span != null) | ||
| span.setAttribute(key,value); | ||
| } | ||
| public void setLongAttribute(String key, long value) | ||
| { | ||
| if (span != null) | ||
| span.setAttribute(key,value); | ||
| } | ||
| public void setStatus(Byte spanStatusCodeByte) | ||
| { | ||
| StatusCode statusCode = toStatusCode(spanStatusCodeByte); | ||
| if (span != null) | ||
| span.setStatus(statusCode); | ||
| } | ||
| public void setStatus(Byte spanStatusCodeByte, String message) | ||
| { | ||
| StatusCode statusCode = toStatusCode(spanStatusCodeByte); | ||
| if (span != null) | ||
| span.setStatus(statusCode, message); | ||
| } | ||
| //endregion | ||
|
|
||
| //region Private methods | ||
|
|
||
| private String getBaggaeItemInContext(Context context, String key) | ||
| { | ||
| AtomicReference<String> value = new AtomicReference<>(""); | ||
| Baggage.fromContext(context).asMap().forEach((k, v) -> { | ||
| if (k.equals(key)) { | ||
| value.set(v.getValue()); | ||
| } | ||
| }); | ||
| if (value != null) | ||
| return value.get(); | ||
| return ""; | ||
| } | ||
| private Context addBaggageReturnContext(String key, String value) | ||
| { | ||
| Baggage baggage = Baggage.current().toBuilder().put(key,value).build(); | ||
| return baggage.storeInContext(getContext()); | ||
| } | ||
| private Context getContext() | ||
| { | ||
| if (span != null) | ||
| return Context.current().with(span); | ||
| return null; | ||
| } | ||
| private Context getContextCurrentSpan() | ||
| { | ||
| return Context.current(); | ||
| } | ||
|
|
||
| private static void recordException(Span span, Throwable exc) { | ||
| if (span != null && exc != null) { | ||
| span.recordException(exc); | ||
| } | ||
| } | ||
| private io.opentelemetry.api.trace.SpanContext getSpanContext(Span span) | ||
| { | ||
| if (span != null) | ||
| return span.getSpanContext(); | ||
| return null; | ||
| } | ||
| private boolean isRecording(Span span) | ||
| { | ||
| if (span != null) | ||
| return span.isRecording(); | ||
| return false; | ||
| } | ||
| private Span current() | ||
| { | ||
| return Span.current(); | ||
|
|
||
| } | ||
| private static StatusCode toStatusCode (Byte spanStatusCode){ | ||
| switch (spanStatusCode) { | ||
| case 0: | ||
| return StatusCode.UNSET; | ||
| case 1: | ||
| return StatusCode.OK; | ||
| case 2: | ||
| return StatusCode.ERROR; | ||
| } | ||
| return null; | ||
| } | ||
| private static SpanStatusCode fromStatusCode (StatusCode statusCode){ | ||
| switch (statusCode) { | ||
| case UNSET: | ||
| return SpanStatusCode.UNSET; | ||
| case OK: | ||
| return SpanStatusCode.OK; | ||
| case ERROR: | ||
| return SpanStatusCode.ERROR; | ||
| } | ||
| return null; | ||
| } | ||
| //endregion | ||
|
|
||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we bump to version 1.36?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bumped to the latest.