-
Notifications
You must be signed in to change notification settings - Fork 7
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: enable exporting logs via otel #249
Draft
sjvans
wants to merge
8
commits into
main
Choose a base branch
from
logging
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 3 commits
Commits
Show all changes
8 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 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