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 23 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
b108750
Add OTel span support
sdaubin May 3, 2024
53a0a96
Documentation
sdaubin May 3, 2024
50ada7a
Add static constructor
sdaubin May 3, 2024
fe873d7
Add test showing the SpanBuilder.setParent doesn't link async work
sdaubin May 3, 2024
8faee60
Implement more of the OTel API for spans
sdaubin May 6, 2024
583a36a
Add a switch to disable custom otel span builder
sdaubin May 7, 2024
bcb9146
Merge remote-tracking branch 'origin' into saxon/replace-otel-spans2
sdaubin May 7, 2024
c7bdc0a
Fix test
sdaubin May 8, 2024
4563453
Log if `otel.exporter.otlp.endpoint` is set
sdaubin May 8, 2024
224947c
Update DefaultTracerTest.java
sdaubin May 8, 2024
6041d13
Allow individual opentelemetry instrumentation scopes to be disabled
sdaubin May 10, 2024
a9029cc
Try to fix test
sdaubin May 10, 2024
67efb93
Use OpenTelemetry no op Span instance
sdaubin May 16, 2024
509ce8a
Fix sql obfuscation of QueryConverter used by otel spans
sdaubin May 21, 2024
4911fc1
Add another external span test
sdaubin May 21, 2024
ba4835a
Merge branch 'main' into saxon/replace-otel-spans2
jasonjkeller Jun 28, 2024
972a16c
Add copyright header and formatting
jasonjkeller Jul 1, 2024
12e2ef4
Merge branch 'main' of github.com:newrelic/newrelic-java-agent into s…
jasonjkeller Sep 23, 2024
f99b9ed
Merge branch 'main' of github.com:newrelic/newrelic-java-agent into s…
jasonjkeller Oct 7, 2024
35c91f8
Add some comments
jasonjkeller Oct 29, 2024
0750c0b
Merge main
jasonjkeller Nov 14, 2024
c308a60
Add readme for OTel functionality
jasonjkeller Dec 2, 2024
61bf958
Merge branch 'main' of github.com:newrelic/newrelic-java-agent into s…
jasonjkeller Jan 6, 2025
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
Prev Previous commit
Next Next commit
Add copyright header and formatting
jasonjkeller committed Jul 1, 2024
commit 972a16c8ce550c204ad33a886f9d5e65ad312f3c
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
/*
*
* * 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() {}
private SqlQueryConverter() {
}

@Override
public String toRawQueryString(String rawQuery) {
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.context;

import com.newrelic.agent.bridge.AgentBridge;
@@ -8,7 +15,8 @@
import io.opentelemetry.sdk.trace.ExitTracerSpan;

class ContextHelper {
private 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.
@@ -38,7 +46,7 @@ public static Scope makeCurrent(Context context, Scope scope) {
Span currentSpan = Span.fromContext(context);

if (currentSpan instanceof ExitTracerSpan) {
return ((ExitTracerSpan)currentSpan).createScope(scope);
return ((ExitTracerSpan) currentSpan).createScope(scope);
}
}
return scope;
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.context;

import com.newrelic.api.agent.weaver.MatchType;
@@ -11,6 +18,6 @@ public static Context current() {
}

public Scope makeCurrent() {
return ContextHelper.makeCurrent((Context)this, Weaver.callOriginal());
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;
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.agent.bridge.AgentBridge;
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.trace;

import io.opentelemetry.api.common.Attributes;
@@ -6,7 +13,8 @@
import java.util.Map;

public class AttributesHelper {
private AttributesHelper() {}
private AttributesHelper() {
}

public static Attributes toAttributes(Map<String, Object> attributes) {
AttributesBuilder builder = Attributes.builder();
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.trace;

import com.newrelic.agent.bridge.AgentBridge;
@@ -61,9 +68,9 @@ public class ExitTracerSpan implements ReadWriteSpan {
// these attributes are reported as agent attributes, we don't want to duplicate them in user attributes
private static final Set<String> AGENT_ATTRIBUTE_KEYS =
Collections.unmodifiableSet(
Stream.of(DB_STATEMENT, DB_SQL_TABLE, DB_SYSTEM, DB_OPERATION, SERVER_ADDRESS, SERVER_PORT)
.map(AttributeKey::getKey)
.collect(Collectors.toSet()));
Stream.of(DB_STATEMENT, DB_SQL_TABLE, DB_SYSTEM, DB_OPERATION, SERVER_ADDRESS, SERVER_PORT)
.map(AttributeKey::getKey)
.collect(Collectors.toSet()));

final ExitTracer tracer;
private final SpanKind spanKind;
@@ -78,7 +85,8 @@ public class ExitTracerSpan implements ReadWriteSpan {
private long endEpochNanos;
private final Resource resource;

ExitTracerSpan(ExitTracer tracer, InstrumentationLibraryInfo instrumentationLibraryInfo, SpanKind spanKind, String spanName, SpanContext parentSpanContext, Resource resource, Map<String, Object> attributes, Consumer<ExitTracerSpan> onEnd) {
ExitTracerSpan(ExitTracer tracer, InstrumentationLibraryInfo instrumentationLibraryInfo, SpanKind spanKind, String spanName, SpanContext parentSpanContext,
Resource resource, Map<String, Object> attributes, Consumer<ExitTracerSpan> onEnd) {
this.tracer = tracer;
this.spanKind = spanKind;
this.spanName = spanName;
@@ -94,7 +102,8 @@ public class ExitTracerSpan implements ReadWriteSpan {

public static ExitTracerSpan wrap(ExitTracer tracer) {
return new ExitTracerSpan(tracer, InstrumentationLibraryInfo.empty(), SpanKind.INTERNAL, tracer.getMetricName(), SpanContext.getInvalid(),
Resource.empty(), Collections.emptyMap(), span -> {});
Resource.empty(), Collections.emptyMap(), span -> {
});
}

@Override
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.trace;

import com.newrelic.agent.bridge.AgentBridge;
@@ -35,10 +42,11 @@ class NRSpanBuilder implements SpanBuilder {
private final TracerSharedState sharedState;
private final Consumer<ExitTracerSpan> endHandler;
private final InstrumentationLibraryInfo instrumentationLibraryInfo;
private SpanKind spanKind= SpanKind.INTERNAL;
private SpanKind spanKind = SpanKind.INTERNAL;
private SpanContext parentSpanContext;

public NRSpanBuilder(Instrumentation instrumentation, String instrumentationScopeName, String instrumentationScopeVersion, TracerSharedState sharedState, String spanName) {
public NRSpanBuilder(Instrumentation instrumentation, String instrumentationScopeName, String instrumentationScopeVersion, TracerSharedState sharedState,
String spanName) {
this.instrumentation = instrumentation;
this.spanName = spanName;
this.sharedState = sharedState;
@@ -50,7 +58,8 @@ public NRSpanBuilder(Instrumentation instrumentation, String instrumentationScop
if (sharedState.getActiveSpanProcessor().isEndRequired()) {
endHandler = sharedState.getActiveSpanProcessor()::onEnd;
} else {
endHandler = span -> {};
endHandler = span -> {
};
}
}

@@ -144,7 +153,8 @@ public Span startSpan() {
tracer.addCustomAttribute("span.kind", spanKind.name());
}
// REVIEW - we're not picking up the global resources
return onStart(new ExitTracerSpan(tracer, instrumentationLibraryInfo, spanKind, spanName, parentSpanContext, sharedState.getResource(), attributes, endHandler));
return onStart(new ExitTracerSpan(tracer, instrumentationLibraryInfo, spanKind, spanName, parentSpanContext, sharedState.getResource(), attributes,
endHandler));
}

private Span startServerSpan(SpanContext parentSpanContext) {
@@ -208,7 +218,7 @@ public String getMethod() {
@Override
public int getStatus() throws Exception {
Object statusCode = attributes.get("http.response.status_code");
return statusCode instanceof Number ? ((Number)statusCode).intValue() : 0;
return statusCode instanceof Number ? ((Number) statusCode).intValue() : 0;
}

@Override
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.trace;

import com.newrelic.agent.bridge.AgentBridge;
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.trace;

import com.newrelic.api.agent.NewRelic;
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.context;

import com.google.common.collect.ImmutableMap;
@@ -41,6 +48,7 @@ public class SpanTest {
static {
System.setProperty("otel.java.global-autoconfigure.enabled", "true");
}

static final Tracer OTEL_TRACER = GlobalOpenTelemetry.get().getTracer("test", "1.0");

@Test
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.Agent;
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.trace;

import com.newrelic.agent.bridge.ExitTracer;
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.trace;

import com.newrelic.api.agent.Config;
@@ -37,5 +44,4 @@ private Config createConfig(Boolean autoconfigureEnabled, Boolean spansEnabled)
return config;
}


}
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.trace;

import com.newrelic.api.agent.Config;
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.trace;

import com.newrelic.agent.bridge.ExitTracer;
@@ -56,7 +63,8 @@ public TracerBuilder withTracer(ExitTracer tracer) {
@Override
public Tracer build() {
Supplier<SpanLimits> spanLimitsSupplier = () -> SpanLimits.getDefault();
TracerSharedState sharedState = new TracerSharedState(Clock.getDefault(), IdGenerator.random(), resource, spanLimitsSupplier, Sampler.alwaysOn(), spanProcessors);
TracerSharedState sharedState = new TracerSharedState(Clock.getDefault(), IdGenerator.random(), resource, spanLimitsSupplier, Sampler.alwaysOn(),
spanProcessors);
return spanName -> new NRSpanBuilder(instrumentation, instrumentationScopeName, instrumentationScopeVersion, sharedState, spanName);
}
}