An OpenTelemetry reporter for sending spans and metrics to New Relic using the New Relic Java Telemetry SDK.
For the details on how OpenTelemetry data is mapped to New Relic data, see documentation in Our exporter specifications documentation
To send spans or metrics to New Relic, you will need an Insights Insert API Key.
Note: There is an example BasicExample.java
in the test source code hierarchy that matches this example code. It should be considered as the canonical code for this example, since OpenTelemetry internal SDK APIs are still a work in progress.
Important: If you are using auto-instrumentation, you should skip the configuration of the SDK, and go right to step 4.
- Create a
NewRelicSpanExporter
NewRelicSpanExporter exporter = NewRelicSpanExporter.newBuilder()
.apiKey(System.getenv("INSIGHTS_INSERT_KEY"))
.commonAttributes(new Attributes().put(SERVICE_NAME, "best service ever")).build();- Build the OpenTelemetry
BatchSpansProcessorwith theNewRelicSpanExporter
BatchSpanProcessor spanProcessor = BatchSpanProcessor.newBuilder(exporter).build();- Add the span processor to the default TracerSdkProvider
TracerSdkProvider tracerSdkProvider = (TracerSdkProvider) OpenTelemetry.getTracerProvider();
tracerSdkProvider.addSpanProcessor(spanProcessor);- Create the OpenTelemetry
Tracerand use it for recording spans.
Tracer tracer = OpenTelemetry.getTracerProvider().get("sample-app", "1.0");
Span span = tracer.spanBuilder("testSpan").setSpanKind(Kind.INTERNAL).startSpan();
try (Scope scope = tracer.withSpan(span)) {
//do some work
Thread.sleep(1000);
span.end();
}- Find your spans in New Relic One: go to https://one.newrelic.com/ and select Distributed Tracing.
- Create a
NewRelicMetricExporter
MetricExporter metricExporter =
NewRelicMetricExporter.newBuilder()
.apiKey(System.getenv("INSIGHTS_INSERT_KEY"))
.commonAttributes(new Attributes().put(SERVICE_NAME, "best service ever"))
.build();- Create an
IntervalMetricReaderthat will batch up metrics every 5 seconds:
IntervalMetricReader intervalMetricReader =
IntervalMetricReader.builder()
.setMetricProducers(
Collections.singleton(OpenTelemetrySdk.getMeterProvider().getMetricProducer()))
.setExportIntervalMillis(5000)
.setMetricExporter(metricExporter)
.build();- Create a sample Meter:
Meter meter = OpenTelemetry.getMeterProvider().get("sample-app", "1.0");- Here is an example of a counter:
LongCounter spanCounter =
meter
.longCounterBuilder("spanCounter")
.setUnit("one")
.setDescription("Counting all the spans")
.setMonotonic(true)
.build();- Here is an example of a measure:
LongMeasure spanTimer =
meter
.longMeasureBuilder("spanTimer")
.setUnit("ms")
.setDescription("How long the spans take")
.setAbsolute(true)
.build();- Use these instruments for recording some metrics:
spanCounter.add(1, "spanName", "testSpan", "isItAnError", "true");
spanTimer.record(1000, "spanName", "testSpan")- Find your metrics in New Relic One: go to https://one.newrelic.com/ and locate your service
in the Entity explorer (based on the
"service.name"attributes you've used).
To instrument trace using opentelemetry-auto-instr-java,
opentelemetry-exporter-newrelic-auto-<version>.jar can be used as opentelemetry exporter. Here is an example.
java -javaagent:path/to/opentelemetry-auto-<version>.jar \
-Dota.exporter.jar=path/to/opentelemetry-exporter-newrelic-auto-<version>.jar \
-Dota.exporter.newrelic.api.key=${INSIGHTS_INSERT_KEY} \
-Dota.exporter.newrelic.service.name=best-service-ever \
-jar myapp.jarIf you wish to turn on debug logging for the exporter running in the auto-instrumentation agent, use the following system property:
-Dio.opentelemetry.auto.slf4j.simpleLogger.log.com.newrelic.telemetry=debug
And, if you wish to enable audit logging for the exporter running in the auto-instrumentaiotn agent, use this system property:
-Dota.exporter.newrelic.enable.audit.logging=true
For tips on how to find and query your data in New Relic, see Find trace/span data.
For general querying information, see:
build.gradle:
repositories {
maven {
url = "https://oss.sonatype.org/content/repositories/snapshots"
}
}
implementation("com.newrelic.telemetry:opentelemetry-exporters-newrelic:0.5.0")
implementation("io.opentelemetry:opentelemetry-sdk:0.5.0")
implementation("com.newrelic.telemetry:telemetry-core:0.6.0")
implementation("com.newrelic.telemetry:telemetry-http-okhttp:0.6.0")
CI builds are run on Azure Pipelines:
The project uses gradle 5 for building, and the gradle wrapper is provided.
To compile, run the tests and build the jar:
$ ./gradlew build
Full details are available in our CONTRIBUTING.md file. We'd love to get your contributions to improve the New Relic OpenTelemetry exporter! Keep in mind when you submit your pull request, you'll need to sign the CLA via the click-through using CLA-Assistant. You only have to sign the CLA one time per project. To execute our corporate CLA, which is required if your contribution is on behalf of a company, or if you have any questions, please drop us an email at open-source@newrelic.com.