-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #77 from quarkiverse/opentelemetry
Add initial OpenTelemetry support for declarative services
- Loading branch information
Showing
14 changed files
with
455 additions
and
32 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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
86 changes: 86 additions & 0 deletions
86
core/runtime/src/main/java/io/quarkiverse/langchain4j/runtime/aiservice/SpanWrapper.java
This file contains 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,86 @@ | ||
package io.quarkiverse.langchain4j.runtime.aiservice; | ||
|
||
import java.util.Optional; | ||
import java.util.function.Function; | ||
|
||
import jakarta.inject.Inject; | ||
|
||
import io.opentelemetry.api.OpenTelemetry; | ||
import io.opentelemetry.api.trace.SpanKind; | ||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.context.Scope; | ||
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter; | ||
import io.opentelemetry.instrumentation.api.instrumenter.InstrumenterBuilder; | ||
import io.opentelemetry.instrumentation.api.instrumenter.SpanKindExtractor; | ||
import io.opentelemetry.instrumentation.api.instrumenter.SpanNameExtractor; | ||
|
||
public class SpanWrapper implements AiServiceMethodImplementationSupport.Wrapper { | ||
|
||
private static final String INSTRUMENTATION_NAME = "io.quarkus.opentelemetry"; | ||
|
||
private final Instrumenter<AiServiceMethodImplementationSupport.Input, Void> instrumenter; | ||
|
||
@Inject | ||
public SpanWrapper(OpenTelemetry openTelemetry) { | ||
InstrumenterBuilder<AiServiceMethodImplementationSupport.Input, Void> builder = Instrumenter.builder( | ||
openTelemetry, | ||
INSTRUMENTATION_NAME, | ||
InputSpanNameExtractor.INSTANCE); | ||
|
||
// TODO: there is probably more information here we need to set | ||
this.instrumenter = builder | ||
.buildInstrumenter(new SpanKindExtractor<>() { | ||
@Override | ||
public SpanKind extract(AiServiceMethodImplementationSupport.Input input) { | ||
return SpanKind.INTERNAL; | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public Object wrap(AiServiceMethodImplementationSupport.Input input, | ||
Function<AiServiceMethodImplementationSupport.Input, Object> fun) { | ||
|
||
Context parentContext = Context.current(); | ||
Context spanContext = null; | ||
Scope scope = null; | ||
boolean shouldStart = instrumenter.shouldStart(parentContext, input); | ||
if (shouldStart) { | ||
spanContext = instrumenter.start(parentContext, input); | ||
scope = spanContext.makeCurrent(); | ||
} | ||
|
||
try { | ||
Object result = fun.apply(input); | ||
|
||
if (shouldStart) { | ||
instrumenter.end(spanContext, input, null, null); | ||
} | ||
|
||
return result; | ||
} catch (Throwable t) { | ||
if (shouldStart) { | ||
instrumenter.end(spanContext, input, null, t); | ||
} | ||
throw t; | ||
} finally { | ||
if (scope != null) { | ||
scope.close(); | ||
} | ||
} | ||
} | ||
|
||
private static class InputSpanNameExtractor implements SpanNameExtractor<AiServiceMethodImplementationSupport.Input> { | ||
|
||
private static final InputSpanNameExtractor INSTANCE = new InputSpanNameExtractor(); | ||
|
||
@Override | ||
public String extract(AiServiceMethodImplementationSupport.Input input) { | ||
Optional<AiServiceMethodCreateInfo.SpanInfo> spanInfoOpt = input.createInfo.getSpanInfo(); | ||
if (spanInfoOpt.isPresent()) { | ||
return spanInfoOpt.get().getName(); | ||
} | ||
return null; | ||
} | ||
} | ||
} |
Oops, something went wrong.