Skip to content

Commit

Permalink
chore: legger inn en felles opentelemetry wrapper (#1401)
Browse files Browse the repository at this point in the history
* chore: legger inn en felles opentelemetry wrapper

* bump bom og bruk otel versjon fra bom
  • Loading branch information
mrsladek authored Dec 7, 2024
1 parent e35daea commit d87bd20
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 2 deletions.
4 changes: 4 additions & 0 deletions felles/log/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,9 @@
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-api</artifactId>
</dependency>
</dependencies>
</project>
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;
}
}
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>no.nav.foreldrepenger.felles</groupId>
<artifactId>fp-bom</artifactId>
<version>3.5.2</version>
<version>3.6.0</version>
</parent>

<artifactId>felles-root</artifactId>
Expand All @@ -33,7 +33,7 @@
<dependency>
<groupId>no.nav.foreldrepenger.felles</groupId>
<artifactId>fp-bom</artifactId>
<version>3.5.2</version>
<version>3.6.0</version>
<scope>import</scope>
<type>pom</type>
</dependency>
Expand Down

0 comments on commit d87bd20

Please sign in to comment.