-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathindex.js
More file actions
137 lines (120 loc) · 5.05 KB
/
Copy pathindex.js
File metadata and controls
137 lines (120 loc) · 5.05 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
135
136
137
const cds = require('@sap/cds')
const LOG = cds.log('telemetry')
const { metrics } = require('@opentelemetry/api')
const { getEnv, getEnvWithoutDefaults } = require('@opentelemetry/core')
const { Resource } = require('@opentelemetry/resources')
const {
AggregationTemporality,
DropAggregation,
MeterProvider,
PeriodicExportingMetricReader,
View
} = require('@opentelemetry/sdk-metrics')
const { getDynatraceMetadata, getCredsForDTAsUPS, getCredsForCLSAsUPS, getCredsForCaaS, augmentCLCreds, augmentCaaSCreds, _require } = require('../utils')
const _protocol2module = {
grpc: '@opentelemetry/exporter-metrics-otlp-grpc',
'http/protobuf': '@opentelemetry/exporter-metrics-otlp-proto',
'http/json': '@opentelemetry/exporter-metrics-otlp-http'
}
function _getExporter() {
let {
kind,
metrics: { exporter: metricsExporter },
credentials
} = cds.env.requires.telemetry
// for kind telemetry-to-otlp based on env vars
if (metricsExporter === 'env') {
const cstm_env = getEnvWithoutDefaults()
const otlp_env = getEnv()
let protocol = cstm_env.OTEL_EXPORTER_OTLP_METRICS_PROTOCOL ?? cstm_env.OTEL_EXPORTER_OTLP_PROTOCOL
// on kyma, the otlp endpoint speaks grpc, but otel's default protocol is http/protobuf -> fix default
if (!protocol) {
const endpoint = otlp_env.OTEL_EXPORTER_OTLP_METRICS_ENDPOINT ?? otlp_env.OTEL_EXPORTER_OTLP_ENDPOINT ?? ''
if (endpoint.match(/:4317/)) protocol = 'grpc'
}
protocol ??= otlp_env.OTEL_EXPORTER_OTLP_METRICS_PROTOCOL ?? otlp_env.OTEL_EXPORTER_OTLP_PROTOCOL
metricsExporter = { module: _protocol2module[protocol], class: 'OTLPMetricExporter' }
}
// use _require for better error message
const metricsExporterModule =
metricsExporter.module === '@cap-js/telemetry' ? require('../exporter') : _require(metricsExporter.module)
if (!metricsExporterModule[metricsExporter.class])
throw new Error(`Unknown metrics exporter "${metricsExporter.class}" in module "${metricsExporter.module}"`)
const config = { ...(metricsExporter.config || {}) }
if (kind.match(/to-dynatrace$/)) {
if (!credentials) credentials = getCredsForDTAsUPS()
if (!credentials) throw new Error('No Dynatrace credentials found.')
config.url ??= `${credentials.apiurl}/v2/otlp/v1/metrics`
config.headers ??= {}
// credentials.rest_apitoken?.token is deprecated and only supported for compatibility reasons
const { token_name } = cds.env.requires.telemetry
// metrics_apitoken for compatibility with previous releases
const token = credentials[token_name] || credentials.metrics_apitoken || credentials.rest_apitoken?.token
if (!token)
throw new Error(`Neither "${token_name}" nor deprecated "rest_apitoken.token" found in Dynatrace credentials`)
config.headers.authorization ??= `Api-Token ${token}`
}
if (kind.match(/to-cloud-logging$/)) {
if (!credentials) credentials = getCredsForCLSAsUPS()
if (!credentials) throw new Error('No SAP Cloud Logging credentials found.')
augmentCLCreds(credentials)
config.url ??= credentials.url
config.credentials ??= credentials.credentials
}
if (kind.match(/to-caas$/)) {
if (!credentials) credentials = getCredsForCaaS()
if (!credentials) throw new Error('No CaaS credentials found.')
augmentCaaSCreds(credentials)
config.url ??= credentials.url
}
// default to DELTA
config.temporalityPreference ??= AggregationTemporality.DELTA
const exporter = new metricsExporterModule[metricsExporter.class](config)
LOG._debug && LOG.debug('Using metrics exporter:', exporter)
return exporter
}
module.exports = resource => {
if (!cds.env.requires.telemetry.metrics?.exporter) return
/*
* general setup
*/
let meterProvider = metrics.getMeterProvider()
if (meterProvider.constructor.name === 'NoopMeterProvider') {
const dtmetadata = getDynatraceMetadata()
resource = new Resource({}).merge(resource).merge(dtmetadata)
// unfortunately, we have to pass views to the MeterProvider constructor
// something like meterProvider.addView() would be a lot nicer for locality
let views = []
if (process.env.HOST_METRICS_RETAIN_SYSTEM) {
// nothing to do
} else {
views.push(
new View({
meterName: '@cap-js/telemetry:host-metrics',
instrumentName: 'system.*',
aggregation: new DropAggregation()
})
)
}
meterProvider = new MeterProvider({ resource, views })
metrics.setGlobalMeterProvider(meterProvider)
} else {
LOG._warn && LOG.warn('MeterProvider already initialized by a different module. It will be used as is.')
}
const metricsConfig = cds.env.requires.telemetry.metrics.config
const exporter = _getExporter()
// push vs. pull
if (typeof exporter.export === 'function') {
const metricReader = new PeriodicExportingMetricReader({ ...metricsConfig, exporter })
meterProvider.addMetricReader(metricReader)
} else {
meterProvider.addMetricReader(exporter)
}
/*
* add individual metrics
*/
require('./db-pool')()
require('./queue')()
require('./host')()
return meterProvider
}