Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OTel span support #1886

Draft
wants to merge 19 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

package com.newrelic.agent.bridge;

import com.newrelic.api.agent.Token;

import java.lang.reflect.InvocationHandler;


Expand All @@ -19,10 +21,19 @@ public interface ExitTracer extends InvocationHandler, TracedMethod {
*/
void finish(int opcode, Object returnValue);

default void finish() {
// 177 is Opcodes.RETURN
finish(177, null);
}

/**
* Called when a method invocation throws an exception.
*
* @param throwable
*/
void finish(Throwable throwable);

default Token getToken() {
return NoOpToken.INSTANCE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

import com.newrelic.api.agent.NewRelic;
import com.newrelic.api.agent.Token;
import com.newrelic.api.agent.Trace;

public interface Instrumentation {

Expand Down Expand Up @@ -59,6 +58,10 @@ ExitTracer createTracer(Object invocationTarget, int signatureId, boolean dispat

ExitTracer createScalaTxnTracer();

default ExitTracer createTracer(String metricName, int flags) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The createSegment API still generates an asynchronous tracer, which is not what we want. And from the bridge there's no easy way to register a signature to get a signatureId. This was the easiest way to get a normal, synchronous tracer.

return null;
}

/**
* Returns the current transaction. This should not be called directly - instead use {@link Agent#getTransaction()}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
*/
public interface TracedMethod extends com.newrelic.api.agent.TracedMethod {

default String getTraceId() {
return "0000000000000000";
}
default String getSpanId() {
return "0000000000000000";
}
/**
* Returns the parent of this traced method, or null if this is the root tracer.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.newrelic.agent.bridge.datastore;

import com.newrelic.api.agent.QueryConverter;

public class SqlQueryConverter implements QueryConverter<String> {
public static final QueryConverter<String> INSTANCE = new SqlQueryConverter();

private SqlQueryConverter() {}

@Override
public String toRawQueryString(String rawQuery) {
return rawQuery;
}

@Override
public String toObfuscatedQueryString(String rawQuery) {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ dependencies {
implementation(project(":newrelic-weaver-api"))
implementation("io.opentelemetry:opentelemetry-sdk-extension-autoconfigure:1.28.0")
testImplementation("junit:junit:4.12")
testImplementation("io.opentelemetry:opentelemetry-exporter-otlp:1.28.0")
testImplementation("io.opentelemetry.instrumentation:opentelemetry-instrumentation-annotations:1.28.0")
}

jar {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package io.opentelemetry.context;

import com.newrelic.agent.bridge.AgentBridge;
import com.newrelic.agent.bridge.ExitTracer;
import com.newrelic.agent.bridge.Transaction;
import com.newrelic.api.agent.TracedMethod;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.sdk.trace.ExitTracerSpan;

class ContextHelper {
private ContextHelper() {}

/**
* If there's no span on the context, but there is a NR tracer on the stack, return a context with our span.
*/
public static Context current(Context context) {
Span currentSpan = Span.fromContext(context);
if (currentSpan == Span.getInvalid()) {
Transaction transaction = AgentBridge.getAgent().getTransaction(false);
if (transaction != null) {
TracedMethod tracedMethod = transaction.getTracedMethod();
if (tracedMethod instanceof ExitTracer) {
return context.with(ExitTracerSpan.wrap((ExitTracer) tracedMethod));
}
}
}
return context;
}

/**
* If there's currently no NR transaction but the current contains a NR span, create a
* {@link com.newrelic.api.agent.Token} related to that span's transaction and hook it into
* the returned {@link Scope}.
*/
public static Scope makeCurrent(Context context, Scope scope) {
final Transaction currentTransaction = AgentBridge.getAgent().getTransaction(false);
if (currentTransaction == null) {
Span currentSpan = Span.fromContext(context);

if (currentSpan instanceof ExitTracerSpan) {
return ((ExitTracerSpan)currentSpan).createScope(scope);
}
}
return scope;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.opentelemetry.context;

import com.newrelic.api.agent.weaver.MatchType;
import com.newrelic.api.agent.weaver.Weave;
import com.newrelic.api.agent.weaver.Weaver;

@Weave(type = MatchType.Interface, originalName = "io.opentelemetry.context.Context")
public abstract class Context_Instrumentation {
public static Context current() {
return ContextHelper.current(Weaver.callOriginal());
}

public Scope makeCurrent() {
return ContextHelper.makeCurrent((Context)this, Weaver.callOriginal());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ public class AutoConfiguredOpenTelemetrySdk {

public static AutoConfiguredOpenTelemetrySdkBuilder builder() {
final AutoConfiguredOpenTelemetrySdkBuilder builder = Weaver.callOriginal();
Boolean autoConfigure = NewRelic.getAgent().getConfig().getValue("opentelemetry.sdk.autoconfigure.enabled");
final Boolean autoConfigure = NewRelic.getAgent().getConfig().getValue("opentelemetry.sdk.autoconfigure.enabled");
if (autoConfigure == null || autoConfigure) {
NewRelic.getAgent().getLogger().log(Level.INFO, "Appending OpenTelemetry SDK customizers");
builder.addPropertiesCustomizer(new PropertiesCustomizer());
builder.addResourceCustomizer(new ResourceCustomer());
builder.addPropertiesCustomizer(OpenTelemetrySDKCustomizer::applyProperties);
builder.addResourceCustomizer(OpenTelemetrySDKCustomizer::applyResources);
}
return builder;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
package io.opentelemetry.sdk.autoconfigure;

import com.newrelic.agent.bridge.AgentBridge;
import com.newrelic.api.agent.Agent;
import com.newrelic.api.agent.Logger;
import com.newrelic.api.agent.NewRelic;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.sdk.resources.ResourceBuilder;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.UUID;
import java.util.logging.Level;

public final class PropertiesCustomizer implements Function<ConfigProperties, Map<String, String>> {
@Override
public Map<String, String> apply(ConfigProperties configProperties) {
final class OpenTelemetrySDKCustomizer {
static final AttributeKey<String> SERVICE_INSTANCE_ID_ATTRIBUTE_KEY = AttributeKey.stringKey("service.instance.id");

static Map<String, String> applyProperties(ConfigProperties configProperties) {
return applyProperties(configProperties, NewRelic.getAgent());
}

/**
* Configure OpenTelemetry exporters to send data to the New Relic backend.
*/
static Map<String, String> applyProperties(ConfigProperties configProperties, Agent agent) {
if (configProperties.getString("otel.exporter.otlp.endpoint") == null) {
final Agent agent = NewRelic.getAgent();
agent.getLogger().log(Level.INFO, "Auto-initializing OpenTelemetry SDK");
final String host = agent.getConfig().getValue("host");
final String endpoint = "https://" + host + ":443";
Expand All @@ -37,4 +49,26 @@ public Map<String, String> apply(ConfigProperties configProperties) {
}
return Collections.emptyMap();
}

static Resource applyResources(Resource resource, ConfigProperties configProperties) {
return applyResources(resource, AgentBridge.getAgent(), NewRelic.getAgent().getLogger());
}

/**
* Add the monitored service's entity.guid to resources.
*/
static Resource applyResources(Resource resource, com.newrelic.agent.bridge.Agent agent, Logger logger) {
logger.log(Level.FINE, "Appending OpenTelemetry resources");
final ResourceBuilder builder = new ResourceBuilder().putAll(resource);
final String instanceId = resource.getAttribute(SERVICE_INSTANCE_ID_ATTRIBUTE_KEY);
if (instanceId == null) {
builder.put(SERVICE_INSTANCE_ID_ATTRIBUTE_KEY, UUID.randomUUID().toString());
}

final String entityGuid = agent.getEntityGuid(true);
if (entityGuid != null) {
builder.put("entity.guid", entityGuid);
}
return builder.build();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.opentelemetry.sdk.trace;

import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.common.AttributesBuilder;

import java.util.Map;

public class AttributesHelper {
private AttributesHelper() {}

public static Attributes toAttributes(Map<String, Object> attributes) {
AttributesBuilder builder = Attributes.builder();
attributes.forEach((key, value) -> {
if (value instanceof String) {
builder.put(key, (String) value);
} else if (value instanceof Float || value instanceof Double) {
builder.put(key, ((Number) value).doubleValue());
} else if (value instanceof Number) {
builder.put(key, ((Number) value).longValue());
}
});
return builder.build();
}
}
Loading
Loading