Skip to content

Commit

Permalink
metrics and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
brunobat committed Nov 28, 2024
1 parent acfcf9e commit d81b882
Show file tree
Hide file tree
Showing 34 changed files with 578 additions and 368 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ private static Severity toSeverity(final Level level) {
}

public static void install(final OpenTelemetry openTelemetry) {
Logger logger = openTelemetry.getLogsBridge().loggerBuilder(OpenTelemetryConfig.INSTRUMENTATION_NAME).build();
Logger logger = openTelemetry.getLogsBridge()
.loggerBuilder(OpenTelemetryConfig.INSTRUMENTATION_NAME)
.setInstrumentationVersion(OpenTelemetryConfig.INSTRUMENTATION_VERSION)
.build();
LogManager.getLogManager().getLogger("").addHandler(new OpenTelemetryHandler(logger));
}
}
8 changes: 0 additions & 8 deletions implementation/observation-otel-bridge/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,6 @@
<artifactId>smallrye-opentelemetry-observation-otel-bridge</artifactId>
<name>SmallRye OpenTelemetry: Observation to OpenTelemetry bridge</name>

<properties>
<micrometer-docs-generator.version>1.0.2</micrometer-docs-generator.version>
<micrometer-docs-generator.inputPath>${project.build.sourceDirectory}</micrometer-docs-generator.inputPath>
<micrometer-docs-generator.inclusionPattern>.*</micrometer-docs-generator.inclusionPattern>
<micrometer-docs-generator.outputPath>${project.build.directory}/observation-docs/
</micrometer-docs-generator.outputPath>
</properties>

<dependencies>
<dependency>
<groupId>io.smallrye.opentelemetry</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,14 @@
import jakarta.enterprise.util.Nonbinding;

import io.micrometer.observation.annotation.Observed;
import io.smallrye.opentelemetry.instrumentation.observation.ObservationRegistryProducer;
import io.smallrye.opentelemetry.instrumentation.observation.handler.OpenTelemetryObservationHandler;

public class ObservationExtension implements Extension {
public void beforeBeanDiscovery(@Observes BeforeBeanDiscovery beforeBeanDiscovery, BeanManager beanManager) {
beforeBeanDiscovery.addInterceptorBinding(
new ObservedAnnotatedType(beanManager.createAnnotatedType(Observed.class)));

beforeBeanDiscovery.addAnnotatedType(OpenTelemetryObservationHandler.class,
OpenTelemetryObservationHandler.class.getName());
// beforeBeanDiscovery.addAnnotatedType(OpenTelemetryObservationHandler.class,
// OpenTelemetryObservationHandler.class.getName());
beforeBeanDiscovery.addAnnotatedType(ObservationRegistryProducer.class, ObservationRegistryProducer.class.getName());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.smallrye.opentelemetry.instrumentation.observation;
package io.smallrye.opentelemetry.instrumentation.observation.cdi;

import jakarta.enterprise.inject.Produces;
import jakarta.inject.Inject;
Expand All @@ -23,15 +23,12 @@ public class ObservationRegistryProducer {
OpenTelemetry openTelemetry;

@Inject
OpenTelemetryObservationHandler openTelemetryObservationHandler;

@Inject
MeterRegistry registry;
MeterRegistry meterRegistry;

@Produces
@Singleton
public ObservationRegistry registry() {
ObservationRegistry observationRegistry = ObservationRegistry.create();
final ObservationRegistry observationRegistry = ObservationRegistry.create();

observationRegistry.observationConfig()
// .observationFilter(new CloudObservationFilter()) // Where global filters go
Expand All @@ -41,9 +38,9 @@ public ObservationRegistry registry() {
openTelemetry.getPropagators().getTextMapPropagator()),
new PropagatingReceiverTracingObservationHandler(tracer,
openTelemetry.getPropagators().getTextMapPropagator()),
// new TracingAwareMeterObservationHandler(tracer) // For exemplars... Maybe not be needed
openTelemetryObservationHandler))
.observationHandler(new DefaultMeterObservationHandler(registry));
// new TracingAwareMeterObservationHandler(tracer) // For exemplars...
new OpenTelemetryObservationHandler(tracer)))
.observationHandler(new DefaultMeterObservationHandler(meterRegistry));
// .observationHandler(new PrintOutHandler()) // Can be implemented for debugging. Other handlers for future frameworks can also be added.
return observationRegistry;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import static io.opentelemetry.semconv.SemanticAttributes.NET_SOCK_PEER_ADDR;
import static io.opentelemetry.semconv.SemanticAttributes.NET_SOCK_PEER_PORT;
import static io.opentelemetry.semconv.SemanticAttributes.PEER_SERVICE;
import static io.smallrye.opentelemetry.instrumentation.observation.handler.HandlerUtil.HIGH_CARD_ATTRIBUTES;
import static io.smallrye.opentelemetry.instrumentation.observation.handler.HandlerUtil.LOW_CARD_ATTRIBUTES;

import java.net.URI;
import java.util.logging.Logger;
Expand Down Expand Up @@ -108,16 +110,37 @@ protected Span getParentSpan(T context) {
return null;
}

@SuppressWarnings("unchecked")
protected void tagSpan(T context, Span span) {
final Attributes highCardAttributes = context.get(HIGH_CARD_ATTRIBUTES);
setOtelAttributes(span, highCardAttributes);

final Attributes lowCardAttributes = context.get(LOW_CARD_ATTRIBUTES);
setOtelAttributes(span, lowCardAttributes);

for (KeyValue keyValue : context.getAllKeyValues()) {
if (!keyValue.getKey().equalsIgnoreCase("ERROR")) {
span.setAttribute(keyValue.getKey(), keyValue.getValue());

} else {
span.recordException(new RuntimeException(keyValue.getValue()));
}
}
}

private void setOtelAttributes(Span span, Attributes contextAttributes) {
if (contextAttributes != null) {
contextAttributes.forEach((key, value) -> {
// FIXME this is a bit of a hack because KeyValue only allows String values
if (key.getKey().equalsIgnoreCase("ERROR")) {
span.recordException(new RuntimeException(value.toString()));
} else {
span.setAttribute((AttributeKey<Object>) key, value);
}
});
}
}

protected SpanBuilder remoteSpanBuilder(Kind kind,
String remoteServiceName,
String remoteServiceAddress,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package io.smallrye.opentelemetry.instrumentation.observation.handler;

public class HandlerUtil {
public static final String LOW_CARD_ATTRIBUTES = "low_card_attributes";
public static final String HIGH_CARD_ATTRIBUTES = "high_card_attributes";
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,21 @@

import java.util.logging.Logger;

import jakarta.inject.Inject;
import jakarta.inject.Singleton;

import io.micrometer.common.util.StringUtils;
import io.micrometer.observation.Observation;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.context.Context;
import io.smallrye.opentelemetry.instrumentation.observation.context.TracingObservationContext;

@Singleton
public class OpenTelemetryObservationHandler extends AbstractTracingObservationHandler<Observation.Context> {

private static final Logger logger = Logger.getLogger(OpenTelemetryObservationHandler.class.getName());
private final Tracer tracer;

@Inject
Tracer tracer;
public OpenTelemetryObservationHandler(Tracer tracer) {
this.tracer = tracer;
}

@Override
public void onStart(Observation.Context context) {
Expand Down Expand Up @@ -52,7 +50,8 @@ private Span nextSpan(Tracer tracer, Span parent) {
}

private Span nextSpan(Tracer tracer) {
return tracer.spanBuilder("").startSpan();
return tracer.spanBuilder("")
.startSpan();
}

private String getSpanName(Observation.Context context) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
otel.logs.exporter=none
#otel.logs.exporter=none
otel.metric.export.interval=1000
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,12 @@
import io.smallrye.opentelemetry.implementation.config.OpenTelemetryConfigProducer;
import io.smallrye.opentelemetry.implementation.micrometer.cdi.MicrometerExtension;
import io.smallrye.opentelemetry.instrumentation.observation.cdi.ObservationExtension;
import io.smallrye.opentelemetry.instrumentation.observation.handler.OpenTelemetryObservationHandler;
import io.smallrye.opentelemetry.test.InMemoryExporter;
import io.smallrye.opentelemetry.test.InMemoryExporterProducer;

@EnableAutoWeld
@AddExtensions({ OpenTelemetryExtension.class, ConfigExtension.class, ObservationExtension.class, MicrometerExtension.class })
@AddBeanClasses({ OpenTelemetryConfigProducer.class, ObservationRegistryProducer.class, OpenTelemetryObservationHandler.class,
@AddBeanClasses({ OpenTelemetryConfigProducer.class,
InMemoryExporter.class, InMemoryExporterProducer.class })
class ObservationOTelTest {
@Inject
Expand Down
2 changes: 1 addition & 1 deletion implementation/rest-observation/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>io.smallrye.opentelemetry</groupId>
<artifactId>smallrye-opentelemetry-parent</artifactId>
<version>2.6.1-SNAPSHOT</version>
<version>2.8.2-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ public Class<? extends ServerFilterConvention> getDefaultConvention() {

@Override
public KeyName[] getLowCardinalityKeyNames() {
return KeyName.merge(LowCardinalityValues.values(), ServerLowCardinalityValues.values());
return LowCardinalityValues.values();
}

@Override
public KeyName[] getHighCardinalityKeyNames() {
return HighCardinalityValues.values();
return KeyName.merge(HighCardinalityValues.values(), ServerHighCardinalityValues.values());
}
},
CLIENT {
Expand All @@ -32,12 +32,12 @@ public Class<? extends ClientFilterConvention> getDefaultConvention() {

@Override
public KeyName[] getLowCardinalityKeyNames() {
return KeyName.merge(LowCardinalityValues.values(), ClientLowCardinalityValues.values());
return LowCardinalityValues.values();
}

@Override
public KeyName[] getHighCardinalityKeyNames() {
return HighCardinalityValues.values();
return KeyName.merge(HighCardinalityValues.values(), ClientHighCardinalityValues.values());
}
};

Expand All @@ -51,12 +51,6 @@ public String asString() {
return "http.request.method";
}
},
URL_PATH {
@Override
public String asString() {
return "url.path";
}
},
HTTP_ROUTE {
@Override
public String asString() {
Expand Down Expand Up @@ -89,7 +83,7 @@ public String asString() {
}
}

public enum ServerLowCardinalityValues implements KeyName {
public enum ServerHighCardinalityValues implements KeyName {
SERVER_PORT {
@Override
public String asString() {
Expand All @@ -104,7 +98,7 @@ public String asString() {
}
}

public enum ClientLowCardinalityValues implements KeyName {
public enum ClientHighCardinalityValues implements KeyName {
CLIENT_ADDRESS {
@Override
public String asString() {
Expand All @@ -120,6 +114,12 @@ public String asString() {
}

public enum HighCardinalityValues implements KeyName {
URL_PATH {
@Override
public String asString() {
return "url.path";
}
},
URL_QUERY {
@Override
public String asString() {
Expand All @@ -137,6 +137,12 @@ public String asString() {
public String asString() {
return "url.full";
}
},
USER_AGENT_ORIGINAL {
@Override
public String asString() {
return "user_agent.original";
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package io.smallrye.opentelemetry.implementation.rest.observation;

import static io.opentelemetry.semconv.SemanticAttributes.HTTP_METHOD;
import static io.opentelemetry.semconv.SemanticAttributes.HTTP_REQUEST_METHOD;
import static io.opentelemetry.semconv.SemanticAttributes.HTTP_RESPONSE_STATUS_CODE;
import static io.opentelemetry.semconv.SemanticAttributes.HTTP_ROUTE;
import static io.opentelemetry.semconv.SemanticAttributes.HTTP_STATUS_CODE;
import static io.opentelemetry.semconv.HttpAttributes.HTTP_REQUEST_METHOD;
import static io.opentelemetry.semconv.HttpAttributes.HTTP_RESPONSE_STATUS_CODE;
import static io.opentelemetry.semconv.HttpAttributes.HTTP_ROUTE;
import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;

Expand All @@ -23,8 +21,7 @@
import io.opentelemetry.api.common.AttributesBuilder;
import io.opentelemetry.context.Scope;
import io.opentelemetry.context.propagation.TextMapSetter;
import io.opentelemetry.instrumentation.api.instrumenter.http.HttpClientAttributesGetter;
import io.opentelemetry.instrumentation.api.internal.SemconvStability;
import io.opentelemetry.instrumentation.api.semconv.http.HttpClientAttributesGetter;
import io.smallrye.opentelemetry.implementation.rest.observation.client.ClientFilterConvention;
import io.smallrye.opentelemetry.implementation.rest.observation.client.DefaultClientFilterConvention;
import io.smallrye.opentelemetry.implementation.rest.observation.client.ObservationClientContext;
Expand Down Expand Up @@ -52,7 +49,7 @@ public void filter(final ClientRequestContext request) {
}
final ObservationClientContext observationRequestContext = new ObservationClientContext(request);

Observation observation = FilterDocumentation.SERVER
Observation observation = FilterDocumentation.CLIENT
.start(this.userClientFilterConvention,
new DefaultClientFilterConvention(),
() -> observationRequestContext,
Expand All @@ -72,7 +69,7 @@ public void filter(final ClientRequestContext request, final ClientResponseConte
return;
}

contextAndScope.getObservationRequestContext().setResponseContext(response);
contextAndScope.getObservationRequestContext().setResponse(response);
Observation.Scope observationScope = contextAndScope.getObservationScope();

try {
Expand All @@ -86,13 +83,10 @@ public void filter(final ClientRequestContext request, final ClientResponseConte
private Attributes getHistogramAttributes(ClientRequestContext request, ClientResponseContext response) {
AttributesBuilder builder = Attributes.builder();
builder.put(HTTP_ROUTE.getKey(), request.getUri().getPath().toString());// Fixme must contain a template /users/:userID?
if (SemconvStability.emitOldHttpSemconv()) {
builder.put(HTTP_METHOD, request.getMethod());// FIXME semantic conventions
builder.put(HTTP_STATUS_CODE, response.getStatus());
} else {
builder.put(HTTP_REQUEST_METHOD, request.getMethod());// FIXME semantic conventions
builder.put(HTTP_RESPONSE_STATUS_CODE, response.getStatus());
}

builder.put(HTTP_REQUEST_METHOD, request.getMethod());// FIXME semantic conventions
builder.put(HTTP_RESPONSE_STATUS_CODE, response.getStatus());

return builder.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public void filter(final ContainerRequestContext request) {
registry);

Observation.Scope observationScope = observation.openScope();
// the observation req context can be obtained from the observation scope
request.setProperty("otel.span.server.context",
new ObservationRequestContextAndScope(observationRequestContext, observationScope));
}
Expand All @@ -60,7 +61,7 @@ public void filter(final ContainerRequestContext request, final ContainerRespons
return;
}

contextAndScope.getObservationRequestContext().setResponseContext(response);
contextAndScope.getObservationRequestContext().setResponse(response);
Observation.Scope observationScope = contextAndScope.getObservationScope();

try {
Expand Down
Loading

0 comments on commit d81b882

Please sign in to comment.