-
Notifications
You must be signed in to change notification settings - Fork 4
/
create-log-processor.js
98 lines (85 loc) · 3.23 KB
/
create-log-processor.js
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
95
96
97
98
'use strict'
const {
SimpleLogRecordProcessor,
BatchLogRecordProcessor,
ConsoleLogRecordExporter
} = require('@opentelemetry/sdk-logs')
const { MultiLogRecordProcessor } = require('./multi-log-processor')
/**
* @param {LogRecordProcessorOptions | LogRecordProcessorOptions[]} [logRecordProcessorOptions]
*/
module.exports = function createLogProcessor (logRecordProcessorOptions) {
return Array.isArray(logRecordProcessorOptions)
? new MultiLogRecordProcessor(
logRecordProcessorOptions.map(createLogRecordProcessor)
)
: createLogRecordProcessor(logRecordProcessorOptions)
}
/**
* @typedef {"batch" | "simple"} RecordProcessorType
* @typedef {Object} LogRecordProcessorOptions
* @property {RecordProcessorType} recordProcessorType = "batch"
* @property {ExporterOptions} [exporterOptions]
* @property {import('@opentelemetry/sdk-logs').BufferConfig} processorConfig
*
* @param {LogRecordProcessorOptions} [opts]
* @returns {import('@opentelemetry/sdk-logs').LogRecordProcessor}
*/
function createLogRecordProcessor (opts) {
const exporter = createExporter(opts?.exporterOptions)
if (opts?.recordProcessorType === 'simple') {
return new SimpleLogRecordProcessor(exporter)
}
return new BatchLogRecordProcessor(exporter)
}
/**
* @typedef {Object} GrpcExporterOptions
* @property {"grpc"} protocol
* @property {import('@opentelemetry/otlp-grpc-exporter-base').OTLPGRPCExporterConfigNode} [grpcExporterOptions]
*
* @typedef {Object} HttpExporterOptions
* @property {"http"} protocol
* @property {import('@opentelemetry/otlp-exporter-base').OTLPExporterNodeConfigBase} [httpExporterOptions]
*
* @typedef {Object} ProtobufExporterOptions
* @property {"http/protobuf"} protocol
* @property {import('@opentelemetry/otlp-exporter-base').OTLPExporterNodeConfigBase} [protobufExporterOptions]
*
* @typedef {Object} ConsoleExporterOptions
* @property {"console"} protocol
*
* @typedef {GrpcExporterOptions | HttpExporterOptions | ProtobufExporterOptions | ConsoleExporterOptions } ExporterOptions
*
* @param {ExporterOptions} exporterOptions
* @returns {import('@opentelemetry/sdk-logs').LogRecordExporter}
*/
function createExporter (exporterOptions) {
const exporterProtocol =
exporterOptions?.protocol ??
process.env.OTEL_EXPORTER_OTLP_LOGS_PROTOCOL ??
process.env.OTEL_EXPORTER_OTLP_PROTOCOL
if (exporterProtocol === 'grpc') {
const {
OTLPLogExporter
} = require('@opentelemetry/exporter-logs-otlp-grpc')
return new OTLPLogExporter(exporterOptions?.grpcExporterOptions)
}
if (exporterProtocol === 'http') {
const {
OTLPLogExporter
} = require('@opentelemetry/exporter-logs-otlp-http')
return new OTLPLogExporter(exporterOptions?.httpExporterOptions)
}
if (exporterProtocol === 'console') {
return new ConsoleLogRecordExporter()
}
const {
OTLPLogsExporter,
OTLPLogExporter
} = require('@opentelemetry/exporter-logs-otlp-proto')
if (typeof OTLPLogExporter === 'function') {
return new OTLPLogExporter(exporterOptions?.protobufExporterOptions)
}
// TODO: remove this once https://github.com/open-telemetry/opentelemetry-js/issues/3812#issuecomment-1713830883 is resolved
return new OTLPLogsExporter(exporterOptions?.protobufExporterOptions)
}