forked from newrelic/newrelic-telemetry-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TelemetryClientExample.java
105 lines (90 loc) · 4.06 KB
/
TelemetryClientExample.java
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
/*
* Copyright 2019 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/
package com.newrelic.telemetry.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.exceptions.ResponseException;
import com.newrelic.telemetry.metrics.Count;
import com.newrelic.telemetry.metrics.Gauge;
import com.newrelic.telemetry.metrics.MetricBatch;
import com.newrelic.telemetry.metrics.MetricBatchSender;
import com.newrelic.telemetry.metrics.MetricBuffer;
import com.newrelic.telemetry.metrics.Summary;
import com.newrelic.telemetry.spans.Span;
import com.newrelic.telemetry.spans.SpanBatch;
import com.newrelic.telemetry.spans.SpanBatchSender;
import java.net.InetAddress;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
import java.util.Collections;
import java.util.UUID;
/**
* This example shows how to use the TelemetryClient to handle standard error conditions.
*
* <p>It also demonstrates that a single MetricBatch can contain metrics of different types.
*
* <p>To run this example, provide a command line argument for your Insights Insert key.
*/
public class TelemetryClientExample {
public static void main(String[] args) throws Exception {
String insightsInsertKey = args[0];
MetricBatchSender batchSender =
SimpleMetricBatchSender.builder(insightsInsertKey, Duration.of(10, ChronoUnit.SECONDS))
.build();
SpanBatchSender spanBatchSender = SimpleSpanBatchSender.builder(insightsInsertKey).build();
TelemetryClient telemetryClient = new TelemetryClient(batchSender, spanBatchSender);
Attributes commonAttributes = new Attributes().put("exampleName", "TelemetryClientExample");
commonAttributes.put("host", InetAddress.getLocalHost().getHostName());
commonAttributes.put("appName", "testApplication");
commonAttributes.put("environment", "staging");
sendSampleSpan(telemetryClient, commonAttributes);
sendSampleMetrics(telemetryClient, commonAttributes);
// make sure to shutdown the client, else the background Executor will stop the program from
// exiting.
telemetryClient.shutdown();
}
private static void sendSampleMetrics(
TelemetryClient telemetryClient, Attributes commonAttributes) {
long startTime = System.currentTimeMillis();
MetricBuffer metricBuffer = new MetricBuffer(commonAttributes);
metricBuffer.addMetric(
new Gauge("temperatureC", 44d, startTime, new Attributes().put("room", "kitchen")));
metricBuffer.addMetric(
new Gauge("temperatureC", 25d, startTime, new Attributes().put("room", "bathroom")));
metricBuffer.addMetric(
new Gauge("temperatureC", 10d, startTime, new Attributes().put("room", "basement")));
metricBuffer.addMetric(
new Count(
"bugsSquashed",
5d,
startTime,
System.currentTimeMillis(),
new Attributes().put("project", "JAVA")));
metricBuffer.addMetric(
new Summary(
"throughput", 25, 100, 1, 10, startTime, System.currentTimeMillis(), new Attributes()));
MetricBatch batch = metricBuffer.createBatch();
// The TelemetryClient uses the recommended techniques for responding to errors from the
// New Relic APIs. It uses a background thread to schedule the sending, handling retries
// transparently.
telemetryClient.sendBatch(batch);
}
private static void sendSampleSpan(TelemetryClient telemetryClient, Attributes commonAttributes)
throws ResponseException {
Span sampleSpan =
Span.builder(UUID.randomUUID().toString())
.timestamp(System.currentTimeMillis())
.durationMs(150d)
.serviceName("Test Service")
.name("testSpan")
.build();
String traceId = UUID.randomUUID().toString();
SpanBatch spanBatch =
new SpanBatch(Collections.singleton(sampleSpan), commonAttributes, traceId);
telemetryClient.sendBatch(spanBatch);
}
}