-
Notifications
You must be signed in to change notification settings - Fork 8
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
feat: logs via otel #249
Changes from 3 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
e4dceea
feat: enable exporting logs via otel
sjvans 52a3f73
Merge branch 'main' into logging
sjvans 25740ce
changelog
sjvans c10c314
fix cl
sjvans c7476df
Merge branch 'main' into logging
sjvans d4cf86d
Merge branch 'main' into logging
sjvans 94cb036
test
sjvans b21d96a
custom processor
sjvans 8de8627
Merge remote-tracking branch 'origin/main' into logging
sjvans db7ac1b
TODO
sjvans c04809f
on served
sjvans 78526c9
attributes + readme
sjvans 5729b78
replace format function of existing loggers
sjvans ccbdfb0
better test
sjvans 036706c
readme
sjvans 9269717
readme
sjvans 50f0620
only own cl entry
sjvans 67f84f6
relative paths
sjvans 6f1946a
use to.containSubset from chai-subset
sjvans File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' } | ||
}) | ||
} | ||
|
||
return res | ||
} | ||
|
||
// clear cached loggers | ||
cds.log.loggers = {} | ||
|
||
return loggerProvider | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
to do
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
check https://opentelemetry.io/docs/specs/otel/logs/data-model/#log-and-event-record-definition
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
check https://github.com/SAP/cf-nodejs-logging-support/blob/4e4449f5b451a760451fa873bec3e145cd8fc825/src/lib/plugins/otelOutput.ts#L29
There was a problem hiding this comment.
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