-
Notifications
You must be signed in to change notification settings - Fork 3
/
elastic-node-sdk.js
172 lines (155 loc) · 6.81 KB
/
elastic-node-sdk.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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/**
* @typedef {import('@opentelemetry/sdk-node').NodeSDKConfiguration} NodeSDKConfiguration
*/
const os = require('os');
const {metrics, NodeSDK, api} = require('@opentelemetry/sdk-node');
const {BatchLogRecordProcessor} = require('@opentelemetry/sdk-logs');
const {log, registerOTelDiagLogger} = require('./logging');
const {resolveDetectors} = require('./detectors');
const {setupEnvironment, restoreEnvironment} = require('./environment');
const {getInstrumentations} = require('./instrumentations');
const {enableHostMetrics, HOST_METRICS_VIEWS} = require('./metrics/host');
// @ts-ignore - compiler options do not allow lookp outside `lib` folder
const DISTRO_VERSION = require('../package.json').version;
class ElasticNodeSDK extends NodeSDK {
/**
* @param {Partial<NodeSDKConfiguration>} opts
*/
constructor(opts = {}) {
log.trace('ElasticNodeSDK opts:', opts);
registerOTelDiagLogger(api);
// Setup & fix some env
setupEnvironment();
// - NodeSDK defaults to `TracerProviderWithEnvExporters` if neither
// `spanProcessor` nor `traceExporter` are passed in.
/** @type {Partial<NodeSDKConfiguration>} */
const defaultConfig = {
resourceDetectors: resolveDetectors(opts.resourceDetectors),
// if no instrumentations in `opts` get them based on env
instrumentations: opts.instrumentations || getInstrumentations(),
};
// Protocols for exporters. Default is `http/proto`
const exporterPkgNameFromEnvVar = {
grpc: 'grpc',
'http/json': 'http',
'http/protobuf': 'proto',
};
// Get logs exporter protocol based on environment.
const logsExportProtocol =
process.env.OTEL_EXPORTER_OTLP_LOGS_PROTOCOL ||
process.env.OTEL_EXPORTER_OTLP_PROTOCOL ||
'http/protobuf';
let logExporterType = exporterPkgNameFromEnvVar[logsExportProtocol];
if (!logExporterType) {
log.warn(
`Logs exporter protocol "${logsExportProtocol}" unknown. Using default "http/protobuf" protocol`
);
logExporterType = 'proto';
}
log.trace(`Logs exporter protocol set to ${logsExportProtocol}`);
const {OTLPLogExporter} = require(
`@opentelemetry/exporter-logs-otlp-${logExporterType}`
);
defaultConfig.logRecordProcessor = new BatchLogRecordProcessor(
new OTLPLogExporter()
);
// Default metrics exporter.
// Currently NodeSDK does not handle `OTEL_METRICS_EXPORTER`
// https://opentelemetry.io/docs/concepts/sdk-configuration/general-sdk-configuration/#otel_metrics_exporter
// For now we configure periodic (60s) export via OTLP/proto.
// TODO metrics exporter should do for metrics what `TracerProviderWithEnvExporters` does for traces, does that include `url` export endpoint?
// TODO what `temporalityPreference`?
// Disable metrics by config
const metricsDisabled =
process.env.ELASTIC_OTEL_METRICS_DISABLED === 'true';
if (!metricsDisabled) {
// Get metrics exporter protocol based on environment.
const metricsExportProtocol =
process.env.OTEL_EXPORTER_OTLP_METRICS_PROTOCOL ||
process.env.OTEL_EXPORTER_OTLP_PROTOCOL ||
'http/protobuf';
let metricExporterType =
exporterPkgNameFromEnvVar[metricsExportProtocol];
if (!metricExporterType) {
log.warn(
`Metrics exporter protocol "${metricsExportProtocol}" unknown. Using default "http/protobuf" protocol`
);
metricExporterType = 'proto';
}
log.trace(
`Metrics exporter protocol set to ${metricsExportProtocol}`
);
const {OTLPMetricExporter} = require(
`@opentelemetry/exporter-metrics-otlp-${metricExporterType}`
);
// Note: Default values has been taken from the specs
// https://opentelemetry.io/docs/specs/otel/configuration/sdk-environment-variables/#periodic-exporting-metricreader
const metricsInterval =
Number(process.env.OTEL_METRIC_EXPORT_INTERVAL) || 60000;
const metricsTimeout =
Number(process.env.OTEL_METRIC_EXPORT_TIMEOUT) || 30000;
defaultConfig.metricReader =
new metrics.PeriodicExportingMetricReader({
exporter: new OTLPMetricExporter(),
exportIntervalMillis: metricsInterval,
exportTimeoutMillis: metricsTimeout,
});
defaultConfig.views = [
// Add views for `host-metrics` to avoid excess of data being sent to the server
...HOST_METRICS_VIEWS,
];
}
const configuration = Object.assign(defaultConfig, opts);
super(configuration);
// Once NodeSDK's constructor finish we can restore env
restoreEnvironment();
/** @private */
this._metricsDisabled = metricsDisabled;
/** @private */
this._log = log;
}
/**
* Starts the SDK
*/
start() {
this._log.info(
{
preamble: true,
distroVersion: DISTRO_VERSION,
env: {
// For darwin: https://en.wikipedia.org/wiki/Darwin_%28operating_system%29#Release_history
os: `${os.platform()} ${os.release()}`,
arch: os.arch(),
runtime: `Node.js ${process.version}`,
},
},
'start Elastic Distribution for OpenTelemetry Node.js'
);
super.start();
if (!this._metricsDisabled) {
// TODO: make this configurable, user might collect host metrics with a separate utility
enableHostMetrics();
}
}
}
module.exports = {
ElasticNodeSDK,
};