-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: legger inn en felles opentelemetry wrapper (#1401)
* chore: legger inn en felles opentelemetry wrapper * bump bom og bruk otel versjon fra bom
- Loading branch information
Showing
3 changed files
with
94 additions
and
2 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
88 changes: 88 additions & 0 deletions
88
felles/log/src/main/java/no/nav/vedtak/log/tracing/OtelSpanWrapper.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,88 @@ | ||
package no.nav.vedtak.log.tracing; | ||
|
||
import java.util.concurrent.Callable; | ||
import java.util.function.Supplier; | ||
import java.util.function.UnaryOperator; | ||
|
||
import io.opentelemetry.api.GlobalOpenTelemetry; | ||
import io.opentelemetry.api.trace.Span; | ||
import io.opentelemetry.api.trace.SpanBuilder; | ||
import io.opentelemetry.api.trace.StatusCode; | ||
import io.opentelemetry.api.trace.Tracer; | ||
|
||
/** | ||
* Gir mulighet for å opprette opentelemetry traces og spans i biblioteker og applikasjoner. | ||
*/ | ||
public class OtelSpanWrapper { | ||
private final Tracer tracer; | ||
|
||
public static OtelSpanWrapper forApplikasjon() { | ||
var tracer = GlobalOpenTelemetry.getTracer("application"); | ||
return new OtelSpanWrapper(tracer); | ||
} | ||
|
||
public static OtelSpanWrapper forBibliotek(String libraryName, String libraryVersion) { | ||
var tracer = GlobalOpenTelemetry.getTracer(libraryName, libraryVersion); | ||
return new OtelSpanWrapper(tracer); | ||
} | ||
|
||
public OtelSpanWrapper(Tracer tracer) { | ||
this.tracer = tracer; | ||
} | ||
|
||
public <V> V span(String navn, | ||
UnaryOperator<SpanBuilder> spanBuilderTransformer, | ||
Supplier<V> supplier) { | ||
var span = startSpan(navn, spanBuilderTransformer); | ||
try (var unused = span.makeCurrent()) { | ||
return supplier.get(); | ||
} catch (RuntimeException e) { | ||
span.recordException(e); | ||
span.setStatus(StatusCode.ERROR, e.getClass().getSimpleName()); | ||
throw e; | ||
} finally { | ||
span.end(); | ||
} | ||
} | ||
|
||
public <E extends Exception> void span(String navn, | ||
UnaryOperator<SpanBuilder> spanBuilderTransformer, | ||
RunnableWithException<E> runnable) throws E { | ||
var span = startSpan(navn, spanBuilderTransformer); | ||
try (var unused = span.makeCurrent()) { | ||
runnable.run(); | ||
} catch (Exception e) { | ||
span.recordException(e); | ||
span.setStatus(StatusCode.ERROR, e.getClass().getSimpleName()); | ||
throw e; | ||
} finally { | ||
span.end(); | ||
} | ||
} | ||
|
||
public <V> V span(String navn, | ||
UnaryOperator<SpanBuilder> spanBuilderTransformer, | ||
Callable<V> runnable) throws Exception { | ||
var span = startSpan(navn, spanBuilderTransformer); | ||
try (var unused = span.makeCurrent()) { | ||
return runnable.call(); | ||
} catch (Exception e) { | ||
span.recordException(e); | ||
span.setStatus(StatusCode.ERROR, e.getClass().getSimpleName()); | ||
throw e; | ||
} finally { | ||
span.end(); | ||
} | ||
} | ||
|
||
private Span startSpan(String navn, UnaryOperator<SpanBuilder> spanBuilderTransformer) { | ||
var spanBuilder = tracer.spanBuilder(navn); | ||
spanBuilder = spanBuilderTransformer.apply(spanBuilder); | ||
return spanBuilder.startSpan(); | ||
} | ||
|
||
@FunctionalInterface | ||
public interface RunnableWithException<E extends Exception> { | ||
void run() throws E; | ||
} | ||
} |
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