Skip to content

Commit 146610f

Browse files
committed
feat: add possibility to start and end spans with child-parent relations
1 parent 094cc77 commit 146610f

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

src/main/java/io/supertokens/telemetry/TelemetryProvider.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
import io.opentelemetry.api.GlobalOpenTelemetry;
2020
import io.opentelemetry.api.OpenTelemetry;
2121
import io.opentelemetry.api.common.Attributes;
22+
import io.opentelemetry.api.trace.Span;
2223
import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator;
24+
import io.opentelemetry.context.Context;
2325
import io.opentelemetry.context.propagation.ContextPropagators;
2426
import io.opentelemetry.exporter.otlp.logs.OtlpGrpcLogRecordExporter;
2527
import io.opentelemetry.exporter.otlp.trace.OtlpGrpcSpanExporter;
@@ -65,6 +67,7 @@ public static void createLogEvent(Main main, TenantIdentifier tenantIdentifier,
6567
String logLevel) {
6668
getInstance(main).openTelemetry.getTracer("core-tracer")
6769
.spanBuilder(logLevel)
70+
.setParent(Context.current())
6871
.setAttribute("tenant.connectionUriDomain", tenantIdentifier.getConnectionUriDomain())
6972
.setAttribute("tenant.appId", tenantIdentifier.getAppId())
7073
.setAttribute("tenant.tenantId", tenantIdentifier.getTenantId())
@@ -77,6 +80,34 @@ public static void createLogEvent(Main main, TenantIdentifier tenantIdentifier,
7780
.end();
7881
}
7982

83+
public static Span startSpan(Main main, TenantIdentifier tenantIdentifier, String spanName) {
84+
Span span = getInstance(main).openTelemetry.getTracer("core-tracer")
85+
.spanBuilder(spanName)
86+
.setParent(Context.current())
87+
.setAttribute("tenant.connectionUriDomain", tenantIdentifier.getConnectionUriDomain())
88+
.setAttribute("tenant.appId", tenantIdentifier.getAppId())
89+
.setAttribute("tenant.tenantId", tenantIdentifier.getTenantId())
90+
.startSpan();
91+
92+
span.makeCurrent(); // Set the span as the current context
93+
return span;
94+
}
95+
96+
public static Span endSpan(Span span) {
97+
if (span != null) {
98+
span.end();
99+
}
100+
return span;
101+
}
102+
103+
public static Span addEventToSpan(Span span, String eventName, Attributes attributes) {
104+
if (span != null) {
105+
span.addEvent(eventName, attributes, System.currentTimeMillis(), TimeUnit.MILLISECONDS);
106+
}
107+
return span;
108+
}
109+
110+
80111
private static OpenTelemetry initializeOpenTelemetry(Main main) {
81112
if (getInstance(main) != null && getInstance(main).openTelemetry != null) {
82113
return getInstance(main).openTelemetry; // already initialized

0 commit comments

Comments
 (0)