This repository has been archived by the owner on Jul 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
sample.js
58 lines (48 loc) · 1.76 KB
/
sample.js
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
'use strict';
const { diag, DiagConsoleLogger, DiagLogLevel } = require('@opentelemetry/api');
const { Resource } = require('@opentelemetry/resources');
const { MeterProvider } = require('@opentelemetry/sdk-metrics');
const { configureDynatraceMetricExport } = require('..');
// optional: set up logging for OpenTelemetry
diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.ALL)
// Configure a MeterProvider
const provider = new MeterProvider({
resource: new Resource({
'service.name': 'opentelemetry-metrics-sample-dynatrace'
})
});
const reader = configureDynatraceMetricExport(
// exporter configuration
{
prefix: 'sample', // optional
defaultDimensions: [ // optional
{ key: 'default-dim', value: 'default-dim-value' },
],
// If no OneAgent is available locally, export directly to the Dynatrace server:
// url: 'https://myenv123.live.dynatrace.com/api/v2/metrics/ingest',
// apiToken: '<load API token from secure location such as env or config file>'
},
// metric reader configuration
{
exportIntervalMillis: 5000
}
);
provider.addMetricReader(reader);
const meter = provider.getMeter('opentelemetry-metrics-sample-dynatrace');
// Your SDK should be set up correctly now. You can create instruments...
const requestCounter = meter.createCounter('requests', {
description: 'Example of a Counter'
});
const upDownCounter = meter.createUpDownCounter('test_up_down_counter', {
description: 'Example of a UpDownCounter'
});
// ...set up attributes...
const attributes = {
pid: process.pid.toString(),
environment: 'staging'
};
// ... and start recording metrics:
setInterval(() => {
requestCounter.add(Math.round(Math.random() * 1000), attributes);
upDownCounter.add(Math.random() > 0.5 ? 1 : -1, attributes);
}, 1000);