-
Notifications
You must be signed in to change notification settings - Fork 143
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
sdaubin
wants to merge
20
commits into
main
Choose a base branch
from
saxon/replace-otel-spans2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
OTel span support #1886
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
b108750
Add OTel span support
sdaubin 53a0a96
Documentation
sdaubin 50ada7a
Add static constructor
sdaubin fe873d7
Add test showing the SpanBuilder.setParent doesn't link async work
sdaubin 8faee60
Implement more of the OTel API for spans
sdaubin 583a36a
Add a switch to disable custom otel span builder
sdaubin bcb9146
Merge remote-tracking branch 'origin' into saxon/replace-otel-spans2
sdaubin c7bdc0a
Fix test
sdaubin 4563453
Log if `otel.exporter.otlp.endpoint` is set
sdaubin 224947c
Update DefaultTracerTest.java
sdaubin 6041d13
Allow individual opentelemetry instrumentation scopes to be disabled
sdaubin a9029cc
Try to fix test
sdaubin 67efb93
Use OpenTelemetry no op Span instance
sdaubin 509ce8a
Fix sql obfuscation of QueryConverter used by otel spans
sdaubin 4911fc1
Add another external span test
sdaubin ba4835a
Merge branch 'main' into saxon/replace-otel-spans2
jasonjkeller 972a16c
Add copyright header and formatting
jasonjkeller 12e2ef4
Merge branch 'main' of github.com:newrelic/newrelic-java-agent into s…
jasonjkeller f99b9ed
Merge branch 'main' of github.com:newrelic/newrelic-java-agent into s…
jasonjkeller 35c91f8
Add some comments
jasonjkeller File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
agent-bridge/src/main/java/com/newrelic/agent/bridge/datastore/SqlQueryConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
...-extension-autoconfigure-1.28.0/src/main/java/io/opentelemetry/context/ContextHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...-autoconfigure-1.28.0/src/main/java/io/opentelemetry/context/Context_Instrumentation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
...e-1.28.0/src/main/java/io/opentelemetry/sdk/autoconfigure/OpenTelemetrySDKCustomizer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
40 changes: 0 additions & 40 deletions
40
...nfigure-1.28.0/src/main/java/io/opentelemetry/sdk/autoconfigure/PropertiesCustomizer.java
This file was deleted.
Oops, something went wrong.
31 changes: 0 additions & 31 deletions
31
...toconfigure-1.28.0/src/main/java/io/opentelemetry/sdk/autoconfigure/ResourceCustomer.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 asignatureId
. This was the easiest way to get a normal, synchronous tracer.