Skip to content

Commit

Permalink
Merge pull request #36 from helios/collect_pubsub_attributes
Browse files Browse the repository at this point in the history
[HLS-2536] collect pubsub attributes
  • Loading branch information
nozik authored Oct 2, 2022
2 parents 10b4ce6 + 6c95ff0 commit fda989f
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import static net.bytebuddy.matcher.ElementMatchers.isPublic;
import static net.bytebuddy.matcher.ElementMatchers.named;

import com.google.cloud.pubsub.v1.Publisher;
import com.google.pubsub.v1.PubsubMessage;
import io.opentelemetry.context.Context;
import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge;
Expand All @@ -35,9 +36,10 @@ public void transform(TypeTransformer typeTransformer) {
public static class PubsubPublisherAddAttributesAdvice {
@Advice.OnMethodEnter(suppress = Throwable.class)
public static void onEnterHandle(
@Advice.This Publisher publisher,
@Advice.Argument(value = 0, readOnly = false) PubsubMessage pubsubMessage) {
Context parentContext = Java8BytecodeBridge.currentContext();
startAndInjectSpan(parentContext, pubsubMessage);
startAndInjectSpan(parentContext, pubsubMessage, publisher);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package io.opentelemetry.javaagent.instrumentation.pubsub;

import com.google.cloud.pubsub.v1.Publisher;
import com.google.pubsub.v1.PubsubMessage;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.trace.Span;
Expand All @@ -13,6 +14,7 @@
import io.opentelemetry.instrumentation.api.instrumenter.SpanKindExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.SpanNameExtractor;
import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge;
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -44,13 +46,7 @@ public static Instrumenter<PubsubMessage, Void> publisherInstrumenter() {
}

private static Instrumenter<PubsubMessage, Void> createPublisherInstrumenter() {
SpanNameExtractor publisherSpanNameExtractor =
new SpanNameExtractor() {
@Override
public String extract(Object o) {
return publisherSpanName;
}
};
SpanNameExtractor publisherSpanNameExtractor = o -> publisherSpanName;

return Instrumenter.<PubsubMessage, Void>builder(
GlobalOpenTelemetry.get(), instrumentationName, publisherSpanNameExtractor)
Expand All @@ -59,20 +55,15 @@ public String extract(Object o) {

public static Instrumenter<PubsubMessage, Void> createSubscriberInstrumenter() {

SpanNameExtractor subscriberSpanNameExtractor =
new SpanNameExtractor() {
@Override
public String extract(Object o) {
return subscriberSpanName;
}
};
SpanNameExtractor subscriberSpanNameExtractor = o -> subscriberSpanName;

return Instrumenter.<PubsubMessage, Void>builder(
GlobalOpenTelemetry.get(), instrumentationName, subscriberSpanNameExtractor)
.newInstrumenter(SpanKindExtractor.alwaysConsumer());
}

public static void startAndInjectSpan(Context parentContext, PubsubMessage pubsubMessage) {
public static void startAndInjectSpan(
Context parentContext, PubsubMessage pubsubMessage, Publisher publisher) {
if (!publisherInstrumenter().shouldStart(parentContext, pubsubMessage)) {
return;
}
Expand All @@ -83,6 +74,10 @@ public static void startAndInjectSpan(Context parentContext, PubsubMessage pubsu
Context context = publisherInstrumenter().start(parentContext, pubsubMessage);
Span span = Java8BytecodeBridge.spanFromContext(context);
span.setAttribute(MESSAGE_PAYLOAD_ATTRIBUTE, new String(pubsubMessage.getData().toByteArray()));
span.setAttribute(SemanticAttributes.MESSAGING_SYSTEM, "pubsub");
span.setAttribute(SemanticAttributes.MESSAGING_DESTINATION_KIND, "topic");
span.setAttribute(SemanticAttributes.MESSAGING_DESTINATION, publisher.getTopicNameString());

GlobalOpenTelemetry.get()
.getPropagators()
.getTextMapPropagator()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public class HeliosConfiguration {
public static final String HELIOS_ENVIRONMENT_ENV_VAR = "HS_ENVIRONMENT";
public static final String HELIOS_SERVICE_NAME_ENV_VAR = "HS_SERVICE_NAME";
public static final String HELIOS_TOKEN_ENV_VAR = "HS_TOKEN";
public static final String HELIOS_COLLECTOR_ENDPOINT_ENV_VAR = "HS_COLLECTOR_ENDPOINT";
public static final String DEFAULT_COLLECTOR_ENDPOINT = "https://collector.heliosphere.io/traces";

public static String getEnvironmentName() {
return System.getenv(HELIOS_ENVIRONMENT_ENV_VAR);
Expand All @@ -32,4 +34,9 @@ public static String getServiceName() {
public static String getHsToken() {
return System.getenv(HELIOS_TOKEN_ENV_VAR);
}

public static String getCollectorEndpoint() {
String result = System.getenv(HELIOS_COLLECTOR_ENDPOINT_ENV_VAR);
return result == null ? DEFAULT_COLLECTOR_ENDPOINT : result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package io.opentelemetry.javaagent.tooling;

import static io.opentelemetry.javaagent.tooling.HeliosConfiguration.getCollectorEndpoint;
import static io.opentelemetry.javaagent.tooling.HeliosConfiguration.getHsToken;

import io.opentelemetry.instrumentation.api.appender.internal.LogEmitterProvider;
Expand Down Expand Up @@ -68,8 +69,7 @@ static void setHeliosSystemProperties() {

if (hsToken != null) {
System.setProperty("otel.exporter.otlp.headers", String.format("Authorization=%s", hsToken));
System.setProperty(
"otel.exporter.otlp.traces.endpoint", "https://collector.heliosphere.io/traces");
System.setProperty("otel.exporter.otlp.traces.endpoint", getCollectorEndpoint());
System.setProperty("otel.exporter.otlp.traces.protocol", "http/protobuf");
}
}
Expand Down
2 changes: 1 addition & 1 deletion version.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
val stableVersion = "1.0.2"
val stableVersion = "1.0.3"
val alphaVersion = "1.17.1-alpha-SNAPSHOT"

allprojects {
Expand Down

0 comments on commit fda989f

Please sign in to comment.