-
Notifications
You must be signed in to change notification settings - Fork 156
[WIP] OTel Bridge API support #1886
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
Draft
sdaubin
wants to merge
68
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
Changes from all commits
Commits
Show all changes
68 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 0750c0b
Merge main
jasonjkeller c308a60
Add readme for OTel functionality
jasonjkeller 61bf958
Merge branch 'main' of github.com:newrelic/newrelic-java-agent into s…
jasonjkeller 34ac22a
Merge branch 'main' of github.com:newrelic/newrelic-java-agent into s…
jasonjkeller df75b02
Merge branch 'main' of github.com:newrelic/newrelic-java-agent into s…
jasonjkeller 36a4c8f
Additional unit tests
jtduffy 9a1c6a4
Fix broken trace propagation with w3c headers
jasonjkeller a7bd252
Cleanup
jasonjkeller 6199e9a
Merge pull request #2299 from newrelic/fix-broken-trace-propagation
jasonjkeller 1eafb84
Merge branch 'main' of github.com:newrelic/newrelic-java-agent into s…
jasonjkeller d7ab6b6
Add assertions on OTel span data
jasonjkeller f15b8ee
Merge pull request #2338 from newrelic/otel-cross-agent-tests
jasonjkeller 01ccb3e
Merge branch 'main' of github.com:newrelic/newrelic-java-agent into s…
jasonjkeller f685382
initial test of excluding meters by name
kanderson250 9190f61
Initial prototype of OTel Logs API support
jasonjkeller 66c895c
Add basic configuration for otel meter excludes
kanderson250 25ef8d0
Add otel config class
kanderson250 38cb41a
Add tests for Logs API
jasonjkeller 8f4c713
Some cleanup
jasonjkeller ca00627
Add meter customizer to working branch
kanderson250 02fa4b3
Merge pull request #2452 from newrelic/otel-logs-api
jasonjkeller 87347d5
Merge branch 'main' of github.com:newrelic/newrelic-java-agent into s…
jasonjkeller b67d941
Fix AutoConfiguredOpenTelemetrySdkBuilder
jasonjkeller 6cde0e3
Flesh out meter excludes tests
kanderson250 b23727d
Merge branch 'saxon/replace-otel-spans2' into otel-metric-excludes
kanderson250 b02023b
Add otel config tests and some naming
kanderson250 b3fcb92
Fix NRLogRecordTest
jasonjkeller f218a8a
Fix SpanTest
jasonjkeller 49bff67
Merge pull request #2459 from newrelic/otel-metric-excludes
kanderson250 0794620
OTel attribute mapper impl
jtduffy 59a7e6e
Update JSON
jtduffy 7d77e68
Update config in README
jasonjkeller 68b8eb4
Merge pull request #2461 from newrelic/attribute-mapper
jtduffy 72f970f
WIP
jtduffy 35acb9c
Updated JSON file and attribute types
jtduffy d28ebb7
Some cleanup around attributes
jasonjkeller 146d968
Merge branch 'saxon/replace-otel-spans2' of github.com:newrelic/newre…
jasonjkeller 15b3292
WIP
jtduffy 13987d5
Update logic for getting the timestamp and log level
jasonjkeller f2f03b1
Configure OTel SDK to only export metrics via OTLP
jasonjkeller 0d28dd4
Add supportability metrics when logs, traces, or metrics are enabled
jasonjkeller b95802c
Formatting
jasonjkeller 3b26999
Merge pull request #2477 from newrelic/attributes-json
jtduffy 26e6bb5
Add LogDuplicationChecker
jasonjkeller cc1223f
Merge branch 'saxon/replace-otel-spans2' of github.com:newrelic/newre…
jasonjkeller e5dcce7
Merge branch 'main' of github.com:newrelic/newrelic-java-agent into s…
jasonjkeller 9483d56
Get thread attributes from context data if present
jasonjkeller 24125d9
Update tests and test resource
jtduffy 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
226 changes: 226 additions & 0 deletions
226
instrumentation/opentelemetry-sdk-extension-autoconfigure-1.28.0/README.md
This file contains hidden or 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,226 @@ | ||
# OpenTelemetry Instrumentation | ||
|
||
This instrumentation module weaves parts of the OpenTelemetry SDK to incorporate bits of OpenTelemetry functionality into the New Relic Java agent. | ||
|
||
Specifically, it can: | ||
|
||
* Detect OpenTelemetry Spans and add them to New Relic Java agent traces as New Relic Spans. | ||
* Detect OpenTelemetry LogRecords and report them as New Relic LogEvents. | ||
* Detect OpenTelemetry dimensional metrics and report them to the APM entity being monitored by the Java agent. | ||
* Autoconfigure the OpenTelemetry SDK so that OpenTelemetry data is sent to New Relic and properly associated with an APM entity guid. | ||
|
||
## OpenTelemetry Configuration | ||
|
||
To use the OpenTelemetry functionality incorporated into the New Relic Java agent you must enable the following config options: | ||
|
||
```commandline | ||
-Dotel.java.global-autoconfigure.enabled=true | ||
``` | ||
|
||
## New Relic Java Agent Configuration | ||
|
||
To use the OpenTelemetry Span and dimensional metric functionality incorporated into the New Relic Java agent you must enable the following config options: | ||
|
||
Configuration via yaml: | ||
|
||
```yaml | ||
opentelemetry: | ||
# config to enable different types of telemetry from the OpenTelemetry SDK | ||
sdk: | ||
autoconfigure: | ||
enabled: true | ||
spans: | ||
enabled: true | ||
logs: | ||
enabled: true | ||
# instrumentation scope names which are excluded from reporting traces and logs | ||
instrumentation: | ||
specific-instrumentation-scope-name-1: | ||
enabled: true | ||
specific-instrumentation-scope-name-2: | ||
enabled: true | ||
metrics: | ||
# comma-separated list of meter names which are excluded from reporting metrics | ||
exclude: foo-module,bar-module | ||
``` | ||
|
||
Configuration via system property: | ||
|
||
``` | ||
-Dnewrelic.config.opentelemetry.sdk.autoconfigure.enabled=true | ||
-Dnewrelic.config.opentelemetry.sdk.spans.enabled=true | ||
-Dnewrelic.config.opentelemetry.sdk.logs.enabled=true | ||
|
||
# instrumentation scope names which are excluded from reporting traces and logs | ||
-Dnewrelic.config.opentelemetry.instrumentation.[SPECIFIC_INSTRUMENTATION_SCOPE_NAME].enabled=true | ||
|
||
# comma-separated list of meter names which are excluded from reporting metrics | ||
-Dnewrelic.config.opentelemetry.metrics.exclude=foo-module,bar-module | ||
``` | ||
|
||
Configuration via environment variable: | ||
|
||
``` | ||
NEW_RELIC_OPENTELEMETRY_SDK_AUTOCONFIGURE_ENABLED=true | ||
NEW_RELIC_OPENTELEMETRY_SDK_SPANS_ENABLED=true | ||
NEW_RELIC_OPENTELEMETRY_SDK_LOGS_ENABLED=true | ||
|
||
# instrumentation scope names which are excluded from reporting traces and logs | ||
NEW_RELIC_OPENTELEMETRY_INSTRUMENTATION_[SPECIFIC_INSTRUMENTATION_SCOPE_NAME]_ENABLED=true | ||
|
||
# comma-separated list of meter names which are excluded from reporting metrics | ||
NEW_RELIC_OPENTELEMETRY_METRICS_EXCLUDE=foo-module,bar-module | ||
``` | ||
|
||
## OpenTelemetry Dimensional Metrics | ||
|
||
OpenTelemetry APIs can be used to create dimensional metrics which will be detected by the New Relic Java agent and reported to the APM entity being monitored | ||
by the New Relic Java agent. | ||
|
||
To use this functionality, enable the feature as documented above, add the required `opentelemetry` dependencies to your application: | ||
|
||
```groovy | ||
implementation(platform("io.opentelemetry:opentelemetry-bom:1.44.1")) | ||
implementation("io.opentelemetry:opentelemetry-sdk-extension-autoconfigure") | ||
implementation("io.opentelemetry:opentelemetry-exporter-otlp") | ||
``` | ||
|
||
Then utilize the OpenTelemetry APIs to record dimensional metrics: | ||
|
||
```java | ||
LongCounter longCounter = GlobalOpenTelemetry.get().getMeterProvider().get("my-application").counterBuilder("my.application.counter").build(); | ||
longCounter. | ||
|
||
add(1,Attributes.of(AttributeKey.stringKey("foo"), "bar")); | ||
``` | ||
|
||
Any recorded dimensional metrics can be found in the Metrics Explorer for the associated APM entity and can be used to build custom dashboards. | ||
|
||
## OpenTelemetry Spans | ||
|
||
Documented below are several approaches for incorporating OpenTelemetry Spans into New Relic Java agent traces. | ||
|
||
### `@WithSpan` Annotation | ||
|
||
The New Relic Java agent will detect usage of the OpenTelemetry [@WithSpan](https://opentelemetry.io/docs/zero-code/java/agent/annotations/) annotation. The | ||
`@WithSpan` annotation can be used as an alternative to the `@Trace` annotation. | ||
|
||
This does not currently support the following config options: | ||
|
||
* [Suppressing @WithSpan instrumentation](https://opentelemetry.io/docs/zero-code/java/agent/annotations/#suppressing-withspan-instrumentation) | ||
* [Creating spans around methods with otel.instrumentation.methods.include](https://opentelemetry.io/docs/zero-code/java/agent/annotations/#creating-spans-around-methods-with-otelinstrumentationmethodsinclude) | ||
|
||
Note that OpenTelemetry config properties can be set through environment or system properties, like our agent, and eventually through a config file. We can use | ||
our existing OpenTelemetry instrumentation model to get access to the normalized version of the instrumentation settings to include and exclude methods and pass | ||
those to the core agent through the bridge. | ||
|
||
See `ClassTransformerConfigImpl.java` for implementation details of the `@WithSpan` annotation. | ||
|
||
### Spans Emitted From OpenTelemetry Instrumentation | ||
|
||
The New Relic Java agent will detect Spans emitted by [OpenTelemetry instrumentation](https://opentelemetry.io/docs/languages/java/instrumentation/). It does | ||
this by weaving the `io.opentelemetry.sdk.trace.SdkTracerProvider` so that it will create a New Relic Tracer each time an OpenTelemetry Span is started and | ||
weaving the `io.opentelemetry.context.Context` to propagate context between New Relic and OpenTelemetry Spans. | ||
|
||
Currently, the New Relic Java agent does not load any OpenTelemetry instrumentation it simply detects Spans emitted by OpenTelemetry manual instrumentation, | ||
native instrumentation, library instrumentation, or zero code instrumentation (i.e. bytecode instrumentation that would also require running the OpenTelemetry | ||
Java agent). | ||
|
||
Depending on the OpenTelemetry Span `SpanKind`, it may result in the New Relic Java agent starting a transaction (when one doesn't already exist). | ||
|
||
* `SpanKind.INTERNAL` | ||
* Creating a span with no `SpanKind`, which defaults to `SpanKind.INTERNAL`, will not start a transaction | ||
* If `SpanKind.INTERNAL` spans occur within an already existing New Relic transaction they will be included in the trace | ||
* `SpanKind.CLIENT` | ||
* Creating a span with `SpanKind.CLIENT` will not start a transaction. If a `CLIENT` span has certain db attributes it will be treated as a DB span, and | ||
other specific attributes will cause it to be treated as an external span | ||
* If `SpanKind.CLIENT` spans occur within an already existing New Relic transaction they will be included in the trace | ||
* `SpanKind.SERVER` | ||
* Creating a span with `SpanKind.SERVER` will start a `WebTransaction/Uri/*` transaction. | ||
* If `SpanKind.SERVER` spans occur within an already existing New Relic transaction they will be included in the trace | ||
* `SpanKind.CONSUMER` | ||
* Creating a span with `SpanKind.CONSUMER` will start a `OtherTransaction/*` transaction. | ||
* If `SpanKind.CONSUMER` spans occur within an already existing New Relic transaction they will be included in the trace | ||
* `SpanKind.PRODUCER` | ||
* Creating a span with `SpanKind.PRODUCER` will not start a transaction. There is no explicit processing for `PRODUCER` spans currently. | ||
* If `SpanKind.PRODUCER` spans occur within an already existing New Relic transaction they will be included in the trace (though it's effectively no | ||
different from a `SpanKind.INTERNAL` span) | ||
|
||
## OpenTelemetry Logs | ||
|
||
OpenTelemetry APIs can be used to create log records which will be detected by the New Relic Java agent and reported to the APM entity being monitored by the | ||
New Relic Java agent. The log records will be reported as log events in New Relic, which will be associated with a transaction if the logging occurred within | ||
one. | ||
|
||
To use this functionality, enable the feature as documented above, add the required `opentelemetry` dependencies to your application: | ||
|
||
```groovy | ||
implementation(platform("io.opentelemetry:opentelemetry-bom:1.28.0")) | ||
implementation("io.opentelemetry:opentelemetry-sdk-extension-autoconfigure") | ||
implementation("io.opentelemetry:opentelemetry-exporter-logging") | ||
``` | ||
|
||
Example usage of OpenTelemetry Logs APIs: | ||
|
||
```java | ||
// create LogRecordExporter | ||
private static final SystemOutLogRecordExporter systemOutLogRecordExporter = SystemOutLogRecordExporter.create(); | ||
|
||
// create LogRecordProcessor | ||
private static final LogRecordProcessor logRecordProcessor = SimpleLogRecordProcessor.create(systemOutLogRecordExporter); | ||
|
||
// create Attributes | ||
private static final Attributes attributes = Attributes.builder() | ||
.put("service.name", NewRelic.getAgent().getConfig().getValue("app_name", "unknown")) | ||
.put("service.version", "4.5.1") | ||
.put("environment", "production") | ||
.build(); | ||
|
||
// create Resource | ||
private static final Resource customResource = Resource.create(attributes); | ||
|
||
// create SdkLoggerProvider | ||
private static final SdkLoggerProvider sdkLoggerProvider = SdkLoggerProvider.builder() | ||
.addLogRecordProcessor(logRecordProcessor) | ||
.setResource(customResource) | ||
.build(); | ||
|
||
// create LoggerBuilder | ||
private static final LoggerBuilder loggerBuilder = sdkLoggerProvider | ||
.loggerBuilder("demo-otel-logger") | ||
.setInstrumentationVersion("1.0.0") | ||
.setSchemaUrl("https://opentelemetry.io/schemas/1.0.0"); | ||
|
||
// create Logger | ||
private static final Logger logger = loggerBuilder.build(); | ||
|
||
// utility method to build and emit OpenTelemetry log records | ||
public static void emitOTelLogs(Severity severity) { | ||
// create LogRecordBuilder | ||
LogRecordBuilder logRecordBuilder = logger.logRecordBuilder(); | ||
|
||
Instant now = Instant.now(); | ||
logRecordBuilder | ||
// .setContext() | ||
.setBody("Generating OpenTelemetry LogRecord") | ||
.setSeverity(severity) | ||
.setSeverityText("This is the severity text") | ||
.setAttribute(AttributeKey.stringKey("foo"), "bar") | ||
.setObservedTimestamp(now) | ||
.setObservedTimestamp(now.toEpochMilli(), java.util.concurrent.TimeUnit.MILLISECONDS) | ||
.setTimestamp(now) | ||
.setTimestamp(now.toEpochMilli(), java.util.concurrent.TimeUnit.MILLISECONDS); | ||
|
||
if (severity == Severity.ERROR) { | ||
try { | ||
throw new RuntimeException("This is a test exception for severity ERROR"); | ||
} catch (RuntimeException e) { | ||
logRecordBuilder.setAttribute(AttributeKey.stringKey("exception.message"), e.getMessage()); | ||
logRecordBuilder.setAttribute(AttributeKey.stringKey("exception.type"), e.getClass().getName()); | ||
logRecordBuilder.setAttribute(AttributeKey.stringKey("exception.stacktrace"), Arrays.toString(e.getStackTrace())); | ||
} | ||
} | ||
|
||
logRecordBuilder.emit(); | ||
} | ||
``` |
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.