forked from newrelic/opentelemetry-exporter-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicExample.java
More file actions
134 lines (115 loc) · 5.47 KB
/
BasicExample.java
File metadata and controls
134 lines (115 loc) · 5.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package com.newrelic.telemetry.opentelemetry.examples;
import com.newrelic.telemetry.Attributes;
import com.newrelic.telemetry.SimpleMetricBatchSender;
import com.newrelic.telemetry.SimpleSpanBatchSender;
import com.newrelic.telemetry.TelemetryClient;
import com.newrelic.telemetry.metrics.MetricBatchSender;
import com.newrelic.telemetry.opentelemetry.export.NewRelicMetricExporter;
import com.newrelic.telemetry.opentelemetry.export.NewRelicSpanExporter;
import com.newrelic.telemetry.spans.SpanBatchSender;
import io.opentelemetry.OpenTelemetry;
import io.opentelemetry.context.Scope;
import io.opentelemetry.metrics.LongCounter;
import io.opentelemetry.metrics.LongMeasure;
import io.opentelemetry.metrics.LongMeasure.BoundLongMeasure;
import io.opentelemetry.metrics.Meter;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.metrics.export.IntervalMetricReader;
import io.opentelemetry.sdk.metrics.export.MetricExporter;
import io.opentelemetry.sdk.trace.export.BatchSpansProcessor;
import io.opentelemetry.sdk.trace.export.SpanExporter;
import io.opentelemetry.trace.Span;
import io.opentelemetry.trace.Span.Kind;
import io.opentelemetry.trace.Status;
import io.opentelemetry.trace.Tracer;
import java.util.Collections;
import java.util.Random;
public class BasicExample {
public static void main(String[] args) throws InterruptedException {
// NOTE: Under "normal" circumstances, you wouldn't use OpenTelemetry this way. This is just an
// example to show how you can manually create a tracer for experimentation.
// See the OpenTelemetry documentation at https://opentelemetry.io for more normal usage.
String apiKey = System.getenv("INSIGHTS_INSERT_KEY");
MetricBatchSender metricBatchSender =
SimpleMetricBatchSender.builder(apiKey).enableAuditLogging().build();
SpanBatchSender spanBatchSender =
SimpleSpanBatchSender.builder(apiKey).enableAuditLogging().build();
TelemetryClient telemetryClient = new TelemetryClient(metricBatchSender, spanBatchSender);
Attributes serviceAttributes = new Attributes().put("service.name", "best service ever");
// 1. Create a `NewRelicSpanExporter`
SpanExporter exporter =
NewRelicSpanExporter.newBuilder()
.telemetryClient(telemetryClient)
.commonAttributes(serviceAttributes)
.build();
// 2. Create a `NewRelicMetricExporter`
MetricExporter metricExporter =
NewRelicMetricExporter.newBuilder()
.telemetryClient(telemetryClient)
.commonAttributes(serviceAttributes)
.build();
// 3. Build the OpenTelemetry `BatchSpansProcessor` with the `NewRelicSpanExporter`
BatchSpansProcessor spanProcessor = BatchSpansProcessor.newBuilder(exporter).build();
// 4. Add the span processor to the TracerProvider from the SDK
OpenTelemetrySdk.getTracerProvider().addSpanProcessor(spanProcessor);
// 5. Create an `IntervalMetricReader` that will batch up metrics every 5 seconds.
IntervalMetricReader intervalMetricReader =
IntervalMetricReader.builder()
.setMetricProducers(
Collections.singleton(OpenTelemetrySdk.getMeterProvider().getMetricProducer()))
.setExportIntervalMillis(5000)
.setMetricExporter(metricExporter)
.build();
// Now, we've got the SDK fully configured. Let's write some very simple instrumentation to
// demonstrate how it all works.
// 6. Create an OpenTelemetry `Tracer` and a `Meter` and use them for some manual
// instrumentation.
Tracer tracer = OpenTelemetry.getTracerProvider().get("sample-app", "1.0");
Meter meter = OpenTelemetry.getMeterProvider().get("sample-app", "1.0");
// 7. Here is an example of a counter
LongCounter spanCounter =
meter
.longCounterBuilder("spanCounter")
.setUnit("one")
.setDescription("Counting all the spans")
.setMonotonic(true)
.build();
// 8. Here's an example of a measure.
LongMeasure spanTimer =
meter
.longMeasureBuilder("spanTimer")
.setUnit("ms")
.setDescription("How long the spans take")
.setAbsolute(true)
.build();
// 9. Optionally, you can pre-bind a set of labels, rather than passing them in every time.
BoundLongMeasure boundTimer = spanTimer.bind("spanName", "testSpan");
// 10. use these to instrument some work
doSomeSimulatedWork(tracer, spanCounter, boundTimer);
// clean up so the JVM can exit. Note: these will flush any data to the exporter
// before they exit.
spanProcessor.shutdown();
intervalMetricReader.shutdown();
}
private static void doSomeSimulatedWork(
Tracer tracer, LongCounter spanCounter, BoundLongMeasure boundTimer)
throws InterruptedException {
Random random = new Random();
for (int i = 0; i < 1000; i++) {
long startTime = System.currentTimeMillis();
Span span =
tracer.spanBuilder("testSpan").setSpanKind(Kind.INTERNAL).setNoParent().startSpan();
try (Scope scope = tracer.withSpan(span)) {
boolean markAsError = random.nextBoolean();
if (markAsError) {
span.setStatus(Status.INTERNAL.withDescription("internalError"));
}
spanCounter.add(1, "spanName", "testSpan", "isItAnError", "" + markAsError);
// do some work
Thread.sleep(random.nextInt(1000));
span.end();
boundTimer.record(System.currentTimeMillis() - startTime);
}
}
}
}