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 20 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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,27 @@
/*
*
* * Copyright 2024 New Relic Corporation. All rights reserved.
* * SPDX-License-Identifier: Apache-2.0
*
*/

package com.newrelic.agent.bridge.datastore;

import com.newrelic.api.agent.QueryConverter;

public final 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 @@ -33,4 +33,6 @@ public interface SpanEvent {
String getStatusText();

Map<String, Object> getAgentAttributes();

Map<String, Object> getUserAttributes();
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,9 @@ public String getStatusText() {
public Map<String, Object> getAgentAttributes() {
return spanEvent.getAgentAttributes();
}

@Override
public Map<String, Object> getUserAttributes() {
return spanEvent.getUserAttributesCopy();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ 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")
testImplementation("com.google.guava:guava:30.1.1-jre")
}

jar {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
*
* * Copyright 2024 New Relic Corporation. All rights reserved.
* * SPDX-License-Identifier: Apache-2.0
*
*/

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;

/**
* Helper class for managing the OpenTelemetry Context
*/
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,26 @@
/*
*
* * Copyright 2024 New Relic Corporation. All rights reserved.
* * SPDX-License-Identifier: Apache-2.0
*
*/

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;

/**
* Weaved to manage the OpenTelemetry Context
*/
@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
@@ -1,3 +1,10 @@
/*
*
* * Copyright 2024 New Relic Corporation. All rights reserved.
* * SPDX-License-Identifier: Apache-2.0
*
*/

package io.opentelemetry.sdk.autoconfigure;

import com.newrelic.api.agent.NewRelic;
Expand All @@ -7,16 +14,20 @@

import java.util.logging.Level;

/**
* Weaved to autoconfigure the OpenTelemetrySDK properties
* and resources for compatability with New Relic.
*/
@Weave(type = MatchType.ExactClass)
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
@@ -0,0 +1,90 @@
/*
*
* * Copyright 2024 New Relic Corporation. All rights reserved.
* * SPDX-License-Identifier: Apache-2.0
*
*/

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.UUID;
import java.util.logging.Level;

/**
* Helper class for customizing OpenTelemetrySDK properties
* and resources for compatability with New Relic.
*/
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) {
final String existingEndpoint = configProperties.getString("otel.exporter.otlp.endpoint");
if (existingEndpoint == null) {
agent.getLogger().log(Level.INFO, "Auto-initializing OpenTelemetry SDK");
final String host = agent.getConfig().getValue("host");
final String endpoint = "https://" + host + ":443";
final String licenseKey = agent.getConfig().getValue("license_key");
final Map<String, String> properties = new HashMap<>();
properties.put("otel.exporter.otlp.headers", "api-key=" + licenseKey);
properties.put("otel.exporter.otlp.endpoint", endpoint);
properties.put("otel.exporter.otlp.protocol", "http/protobuf");
properties.put("otel.span.attribute.value.length.limit", "4095");
properties.put("otel.exporter.otlp.compression", "gzip");
properties.put("otel.exporter.otlp.metrics.temporality.preference", "DELTA");
properties.put("otel.exporter.otlp.metrics.default.histogram.aggregation", "BASE2_EXPONENTIAL_BUCKET_HISTOGRAM");
properties.put("otel.experimental.exporter.otlp.retry.enabled", "true");
properties.put("otel.experimental.resource.disabled.keys", "process.command_line");

final Object appName = agent.getConfig().getValue("app_name");
properties.put("otel.service.name", appName.toString());

return properties;
} else {
agent.getLogger().log(Level.WARNING,
"The OpenTelemetry exporter endpoint is set to {0}, the agent will not autoconfigure the SDK",
existingEndpoint);
}
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.

This file was deleted.

Loading
Loading