-
Notifications
You must be signed in to change notification settings - Fork 407
Expand file tree
/
Copy pathotlp_http_metric_exporter.js
More file actions
65 lines (57 loc) · 2.01 KB
/
Copy pathotlp_http_metric_exporter.js
File metadata and controls
65 lines (57 loc) · 2.01 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
'use strict'
const OtlpHttpExporterBase = require('../otlp/otlp_http_exporter_base')
const OtlpTransformer = require('./otlp_transformer')
/**
* @typedef {import('@opentelemetry/resources').Resource} Resource
* @typedef {import('./periodic_metric_reader').AggregatedMetric} AggregatedMetric
*/
/**
* OtlpHttpMetricExporter exports metrics via OTLP over HTTP.
*
* @class OtlpHttpMetricExporter
*/
class OtlpHttpMetricExporter extends OtlpHttpExporterBase {
/**
* Creates a new OtlpHttpMetricExporter instance.
*
* @param {string} url - OTLP endpoint URL
* @param {Record<string, string>|undefined} headers - Additional HTTP headers parsed from the
* corresponding `OTEL_EXPORTER_OTLP_*_HEADERS` env by the MAP parser.
* @param {number} timeout - Request timeout in milliseconds
* @param {string} protocol - OTLP protocol (http/protobuf or http/json)
* @param {Resource} resource - Resource attributes
*/
constructor (url, headers, timeout, protocol, resource) {
super(url, headers, timeout, protocol, 'metrics')
this.transformer = new OtlpTransformer(resource, protocol)
}
/**
* Exports metrics via OTLP over HTTP.
*
* @param {Map<string, AggregatedMetric>} metrics - Map of metric data to export
*
* @param {Function} [done] Called after the HTTP export completes
*/
export (metrics, done) {
if (metrics.size === 0) {
done?.({ code: 0 })
return
}
let dataPointCount = 0
for (const metric of metrics.values()) {
if (metric.dataPointMap) {
dataPointCount += metric.dataPointMap.size
}
}
const additionalTags = [`points:${dataPointCount}`]
this.recordTelemetry('otel.metrics_export_attempts', 1, additionalTags)
const payload = this.transformer.transformMetrics(metrics.values())
this.sendPayload(payload, (result) => {
if (result.code === 0) {
this.recordTelemetry('otel.metrics_export_successes', 1, additionalTags)
}
done?.(result)
})
}
}
module.exports = OtlpHttpMetricExporter