Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: logs via otel #249

Merged
merged 19 commits into from
Feb 6, 2025
19 changes: 14 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,19 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
### Added

- Predefined kind `telemetry-to-otlp` that creates exporters based on OTLP exporter configuration via environment variables
- If `@opentelemetry/instrumentation-runtime-node` is in the project's dependencies but not in `cds.requires.telemetry.instrumentations`, it is registered automatically
- Disable via `cds.requires.telemetry.instrumentations.instrumentation-runtime-node = false`
- Experimental!: Propagate W3C trace context to SAP HANA via session context `SAP_PASSPORT`
- Enable via environment variable `SAP_PASSPORT`
- If `@opentelemetry/instrumentation-runtime-node` is in the project's dependencies but not in `cds.env.requires.telemetry.instrumentations`, it is registered automatically
- Disable via `cds.env.requires.telemetry.instrumentations.instrumentation-runtime-node = false`
- Experimental!: Intercept and export application logs (cf. `cds.log()`) via OpenTelemetry
- Enable by adding section `logging` to `cds.requires.telemetry` as follows (using `grpc` as an example):
```json
"logging": {
"module": "@opentelemetry/exporter-logs-otlp-grpc",
"class": "OTLPLogExporter"
}
```
- Requires additional dependencies `@opentelemetry/api-logs`, `@opentelemetry/sdk-logs`, and the configured exporter module (`cds.requires.telemetry.logging.module`)

### Changed

Expand Down Expand Up @@ -77,7 +86,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
### Added

- Support for local modules (e.g., exporters) via `[...].module = '<path relative to cds.root>'`
- Disable pool metrics via `cds.env.requires.telemetry.metrics._db_pool = false` (beta)
- Disable pool metrics via `cds.requires.telemetry.metrics._db_pool = false` (beta)

### Fixed

Expand All @@ -90,7 +99,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
### Added

- Support for own, high resolution timestamps
- Enable via `cds.env.requires.telemetry.tracing.hrtime = true`
- Enable via `cds.requires.telemetry.tracing.hrtime = true`
- Enabled by default in development profile

## Version 0.0.5 - 2024-03-11
Expand All @@ -107,7 +116,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
- Disable change via environment variable `HOST_METRICS_RETAIN_SYSTEM=true`
- Metric exporter's property `temporalityPreference` always gets defaulted to `DELTA`
- Was previously only done for kind `telemetry-to-dynatrace`
- Set custom value via `cds.env.requires.telemetry.metrics.exporter.config.temporalityPreference`
- Set custom value via `cds.requires.telemetry.metrics.exporter.config.temporalityPreference`

### Fixed

Expand Down
8 changes: 7 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const { SEMRESATTRS_SERVICE_NAME, SEMRESATTRS_SERVICE_VERSION } = require('@open

const tracing = require('./tracing')
const metrics = require('./metrics')
const logging = require('./logging')
const { getDiagLogLevel, getResource, _require } = require('./utils')

function _getInstrumentations() {
Expand Down Expand Up @@ -63,13 +64,18 @@ module.exports = function () {
*/
const meterProvider = metrics(resource)

/*
* setup logging
*/
const loggerProvider = logging(resource)

/*
* register instrumentations
*/
registerInstrumentations({
tracerProvider,
meterProvider,
// loggerProvider,
loggerProvider,
instrumentations: _getInstrumentations()
})
}
104 changes: 104 additions & 0 deletions lib/logging/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
const cds = require('@sap/cds')
const LOG = cds.log('telemetry')

const { getEnv, getEnvWithoutDefaults } = require('@opentelemetry/core')

const { getCredsForCLSAsUPS, augmentCLCreds, _require } = require('../utils')

const _protocol2module = {
grpc: '@opentelemetry/exporter-logs-otlp-grpc',
'http/protobuf': '@opentelemetry/exporter-logs-otlp-proto',
'http/json': '@opentelemetry/exporter-logs-otlp-http'
}

function _getExporter() {
let {
kind,
logging: { exporter: loggingExporter },
credentials
} = cds.env.requires.telemetry

// for kind telemetry-to-otlp based on env vars
if (loggingExporter === 'env') {
const otlp_env = getEnvWithoutDefaults()
const dflt_env = getEnv()
const protocol =
otlp_env.OTEL_EXPORTER_OTLP_LOGS_PROTOCOL ??
otlp_env.OTEL_EXPORTER_OTLP_PROTOCOL ??
dflt_env.OTEL_EXPORTER_OTLP_LOGS_PROTOCOL ??
dflt_env.OTEL_EXPORTER_OTLP_PROTOCOL
loggingExporter = { module: _protocol2module[protocol], class: 'OTLPLogExporter' }
}

// use _require for better error message
const loggingExporterModule = _require(loggingExporter.module)
if (!loggingExporterModule[loggingExporter.class])
throw new Error(`Unknown logs exporter "${loggingExporter.class}" in module "${loggingExporter.module}"`)
const loggingConfig = { ...(loggingExporter.config || {}) }

if (kind.match(/to-cloud-logging$/)) {
if (!credentials) credentials = getCredsForCLSAsUPS()
if (!credentials) throw new Error('No SAP Cloud Logging credentials found.')
augmentCLCreds(credentials)
loggingConfig.url ??= credentials.url
loggingConfig.credentials ??= credentials.credentials
}

const exporter = new loggingExporterModule[loggingExporter.class](loggingConfig)
LOG._debug && LOG.debug('Using logs exporter:', exporter)

return exporter
}

module.exports = resource => {
if (!cds.env.requires.telemetry.logging?.exporter) return

const { logs, SeverityNumber } = require('@opentelemetry/api-logs')
const { LoggerProvider, BatchLogRecordProcessor } = require('@opentelemetry/sdk-logs')

let loggerProvider = logs.getLoggerProvider()
if (!loggerProvider.getDelegateLogger()) {
loggerProvider = new LoggerProvider({ resource })
logs.setGlobalLoggerProvider(loggerProvider)
} else {
LOG._warn && LOG.warn('LoggerProvider already initialized by a different module. It will be used as is.')
loggerProvider = loggerProvider.getDelegateLogger()
}

const exporter = _getExporter()
loggerProvider.addLogRecordProcessor(new BatchLogRecordProcessor(exporter))

const loggers = {}
const l2s = { 1: 'ERROR', 2: 'WARN', 3: 'INFO', 4: 'DEBUG', 5: 'TRACE' }

// intercept logs via format
const { format } = cds.log
cds.log.format = (module, level, ...args) => {
const res = format(module, level, ...args)

let log
try {
log = res.length === 1 && res[0].startsWith?.('{"') && JSON.parse(res[0])
} catch {
// ignore
}
if (log) {
const logger = loggers[module] || (loggers[module] = loggerProvider.getLogger(module))
const severity = l2s[level]
// TODO: what to log?
logger.emit({
severityNumber: SeverityNumber[severity],
severityText: severity,
body: log.msg,
attributes: { 'log.type': 'LogRecord' }
})
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to do

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link

@thsandfuchs thsandfuchs Jan 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

additional some semantic conventions from CPA come to mind... (hope it is ok to post SAP-internal references here in a comment, otherwise I remove them again :))

e.g. https://github.tools.sap/CPA/telemetry-semantic-conventions/blob/main/sap-extensions/logs/trace-correlation.md
or something from here? https://github.tools.sap/CPA/telemetry-semantic-conventions/blob/main/sap-extensions/resource/service.md

}

return res
}

// clear cached loggers
cds.log.loggers = {}

return loggerProvider
}
1 change: 1 addition & 0 deletions lib/metrics/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ function _getExporter() {

const exporter = new metricsExporterModule[metricsExporter.class](metricsConfig)
LOG._debug && LOG.debug('Using metrics exporter:', exporter)

return exporter
}

Expand Down
7 changes: 4 additions & 3 deletions lib/tracing/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ function _getExporter() {
const tracingExporterModule =
tracingExporter.module === '@cap-js/telemetry' ? require('../exporter') : _require(tracingExporter.module)
if (!tracingExporterModule[tracingExporter.class])
throw new Error(`Unknown tracing exporter "${tracingExporter.class}" in module "${tracingExporter.module}"`)
throw new Error(`Unknown trace exporter "${tracingExporter.class}" in module "${tracingExporter.module}"`)
const tracingConfig = { ...(tracingExporter.config || {}) }

if (kind.match(/to-dynatrace$/)) {
Expand All @@ -124,7 +124,8 @@ function _getExporter() {
}

const exporter = new tracingExporterModule[tracingExporter.class](tracingConfig)
LOG._debug && LOG.debug('Using tracing exporter:', exporter)
LOG._debug && LOG.debug('Using trace exporter:', exporter)

return exporter
}

Expand All @@ -150,7 +151,7 @@ module.exports = resource => {
!hasDependency('@opentelemetry/exporter-trace-otlp-proto')
if (via_one_agent) {
// if Dynatrace OneAgent is present, no exporter is needed
LOG._info && LOG.info('Dynatrace OneAgent detected, disabling tracing exporter')
LOG._info && LOG.info('Dynatrace OneAgent detected, disabling trace exporter')
} else {
const exporter = _getExporter()
const spanProcessor =
Expand Down
Loading