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/extract otlp logger #130

Merged
merged 4 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 0 additions & 90 deletions create-log-processor.js

This file was deleted.

87 changes: 4 additions & 83 deletions otlp-logger.js → lib/opentelemetry-mapper.js
Original file line number Diff line number Diff line change
@@ -1,67 +1,6 @@
'use strict'

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

const { SeverityNumber, logs } = require('@opentelemetry/api-logs') // TODO: optional import
const {
Resource,
detectResourcesSync,
envDetectorSync,
hostDetectorSync,
osDetectorSync,
processDetector
} = require('@opentelemetry/resources')

const DEFAULT_MESSAGE_KEY = 'msg'

/**
* @typedef {Object} Options
* @property {string} loggerName
* @property {string} serviceVersion
* @property {import('@opentelemetry/sdk-logs').LogRecordProcessor} logRecordProcessor
* @property {Object} [resourceAttributes={}]
* @property {string} [messageKey="msg"]
*
* @param {Options} opts
*/
function getOtlpLogger (opts) {
const detectedResource = detectResourcesSync({
detectors: [
envDetectorSync,
hostDetectorSync,
osDetectorSync,
processDetector
]
})
const loggerProvider = new LoggerProvider({
resource: detectedResource.merge(
new Resource({ ...opts.resourceAttributes })
)
})

loggerProvider.addLogRecordProcessor(opts.logRecordProcessor)

logs.setGlobalLoggerProvider(loggerProvider)

const logger = logs.getLogger(opts.loggerName, opts.serviceVersion)

const mapperOptions = {
messageKey: opts.messageKey || DEFAULT_MESSAGE_KEY
}

return {
/**
* @param {Bindings} obj
*/
emit (obj) {
logger.emit(toOpenTelemetry(obj, mapperOptions))
},
async shutdown () {
return loggerProvider.shutdown()
}
}
}
const { SeverityNumber } = require('@opentelemetry/api-logs') // TODO: optional import

/**
* If the source format has only a single severity that matches the meaning of the range
Expand Down Expand Up @@ -134,41 +73,23 @@ function toOpenTelemetry (sourceObject, { messageKey }) {
hostname,
pid,
[messageKey]: msg,
...rawAttributes
...attributes
} = sourceObject

const severityNumber =
SEVERITY_NUMBER_MAP[sourceObject.level] ?? SeverityNumber.UNSPECIFIED
const severityText = SEVERITY_NAME_MAP[severityNumber] ?? 'UNSPECIFIED'

let context = api.context.active()
/* eslint-disable camelcase */
const { trace_id, span_id, trace_flags, ...attributes } = rawAttributes

if (
typeof trace_id !== 'undefined' &&
typeof span_id !== 'undefined' &&
typeof trace_flags !== 'undefined'
) {
context = api.trace.setSpanContext(context, {
traceId: trace_id,
spanId: span_id,
traceFlags: trace_flags,
isRemote: true
})
}
/* eslint-enable camelcase */

return {
timestamp: time,
body: msg,
severityNumber,
attributes,
severityText,
context
severityText
}
}

module.exports = {
getOtlpLogger
toOpenTelemetry
}
37 changes: 37 additions & 0 deletions lib/pino-opentelemetry-transport.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use strict'

const build = require('pino-abstract-transport')
const { getOtlpLogger } = require('otlp-logger')
const { toOpenTelemetry } = require('./opentelemetry-mapper')
const DEFAULT_MESSAGE_KEY = 'msg'

/**
* Pino OpenTelemetry transport
*
* @typedef {Object} PinoOptions
* @property {string} [messageKey="msg"]
*
* @typedef {PinoOptions & import('otlp-logger').Options} Options
*
* @param { Options } opts
*/
module.exports = async function (opts) {
const logger = getOtlpLogger(opts)

const mapperOptions = {
messageKey: opts.messageKey || DEFAULT_MESSAGE_KEY
}

return build(
async function (/** @type { AsyncIterable<Bindings> } */ source) {
for await (const obj of source) {
logger.emit(toOpenTelemetry(obj, mapperOptions))
}
},
{
async close () {
return logger.shutdown()
}
}
)
}
38 changes: 0 additions & 38 deletions multi-log-processor.js

This file was deleted.

13 changes: 4 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
"name": "pino-opentelemetry-transport",
"version": "0.4.1",
"description": "OpenTelemetry transport for Pino",
"main": "pino-opentelemetry-transport.js",
"main": "lib/pino-opentelemetry-transport.js",
"scripts": {
"validate-and-test": "standard | snazzy && tap test/*.test.js --branches=45 --lines=45 --functions=45 --statements=45 --coverage-report=lcovonly && tsd",
"validate-and-test": "standard | snazzy && tap test/**/*.test.js --branches=45 --lines=45 --functions=45 --statements=45 --coverage-report=lcovonly && tsd",
"test": "npm run validate-and-test",
"docker-run": "docker compose up",
"generate-types": "tsc",
Expand All @@ -16,13 +16,8 @@
"repository": "github:Vunovati/pino-opentelemetry-transport",
"license": "MIT",
"dependencies": {
"@opentelemetry/api-logs": "^0.46.0",
"@opentelemetry/core": "^1.17.0",
"@opentelemetry/exporter-logs-otlp-grpc": "^0.46.0",
"@opentelemetry/exporter-logs-otlp-http": "^0.46.0",
"@opentelemetry/exporter-logs-otlp-proto": "^0.46.0",
"@opentelemetry/resources": "^1.17.0",
"@opentelemetry/sdk-logs": "^0.46.0",
"@opentelemetry/api-logs": "^0.47.0",
"otlp-logger": "^1.1.0",
"pino-abstract-transport": "^1.1.0"
},
"types": "./types/pino-opentelemetry-transport.d.ts",
Expand Down
37 changes: 0 additions & 37 deletions pino-opentelemetry-transport.js

This file was deleted.

Loading
Loading