-
Notifications
You must be signed in to change notification settings - Fork 407
Expand file tree
/
Copy pathindex.js
More file actions
94 lines (81 loc) · 3.2 KB
/
Copy pathindex.js
File metadata and controls
94 lines (81 loc) · 3.2 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
'use strict'
const os = require('os')
/**
* @typedef {import('../../config')} Config
*/
/**
* OpenTelemetry Logs Implementation for `dd-trace-js`
*
* This package provides a custom OpenTelemetry Logs implementation that integrates
* with the Datadog tracing library. It includes all necessary components for
* emitting, processing, and exporting log records via OTLP (OpenTelemetry Protocol).
*
* Key Components:
* - LoggerProvider: Main entry point for creating loggers
* - Logger: Provides methods to emit log records
* - BatchLogRecordProcessor: Processes log records in batches for efficient export
* - OtlpHttpLogExporter: Exports log records via OTLP over HTTP
* - OtlpTransformer: Transforms log records to OTLP format
*
* This is a custom implementation to avoid pulling in the full OpenTelemetry SDK,
* based on OTLP Protocol v1.7.0. It supports both protobuf and JSON serialization
* formats and integrates with Datadog's configuration system.
*
* @package
*/
const { registerTelemetryFlusher } = require('../../flush')
const LoggerProvider = require('./logger_provider')
const BatchLogRecordProcessor = require('./batch_log_processor')
const OtlpHttpLogExporter = require('./otlp_http_log_exporter')
let unregisterTelemetryFlusher
/**
* Initializes OpenTelemetry Logs support
* @param {import('../../config/config-base')} config - Tracer configuration instance
*/
function initializeOpenTelemetryLogs (config) {
// Build resource attributes
const resourceAttributes = {
'service.name': config.service,
'service.version': config.version,
'deployment.environment': config.env,
}
// Add all tracer tags (includes DD_TAGS, OTEL_RESOURCE_ATTRIBUTES, DD_TRACE_TAGS, etc.)
// Exclude Datadog-style keys that duplicate OpenTelemetry standard keys
if (config.tags) {
const filteredTags = { ...config.tags }
delete filteredTags.service
delete filteredTags.version
delete filteredTags.env
Object.assign(resourceAttributes, filteredTags)
}
// Add host.name if reportHostname is enabled
if (config.reportHostname) {
resourceAttributes['host.name'] = os.hostname()
}
// Create OTLP exporter using resolved config values
const exporter = new OtlpHttpLogExporter(
config.OTEL_EXPORTER_OTLP_LOGS_ENDPOINT,
config.OTEL_EXPORTER_OTLP_LOGS_HEADERS,
config.OTEL_EXPORTER_OTLP_LOGS_TIMEOUT,
config.OTEL_EXPORTER_OTLP_LOGS_PROTOCOL,
resourceAttributes
)
// Create batch processor for exporting logs to Datadog Agent
const processor = new BatchLogRecordProcessor(
exporter,
config.OTEL_BSP_SCHEDULE_DELAY,
config.OTEL_BSP_MAX_EXPORT_BATCH_SIZE
)
// Create logger provider with processor for Datadog Agent export
const loggerProvider = new LoggerProvider({ processor })
// Expose this provider to application calls through the OpenTelemetry Logs API.
loggerProvider.register()
// Remove the old provider callback so lifecycle retention flushes only this global provider.
unregisterTelemetryFlusher?.()
// Include final log batches in lifecycle retention with trace delivery.
unregisterTelemetryFlusher = registerTelemetryFlusher(done => loggerProvider.forceFlush(done))
}
module.exports = {
LoggerProvider,
initializeOpenTelemetryLogs,
}