@splunk/otel
Version:
The Splunk distribution of OpenTelemetry Node Instrumentation provides a Node agent that automatically instruments your Node application to capture and report distributed traces to Splunk APM.
154 lines • 7.09 kB
JavaScript
;
/*
* Copyright Splunk Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.startLogging = startLogging;
exports._setDefaultOptions = _setDefaultOptions;
exports.defaultLogRecordProcessorFactory = defaultLogRecordProcessorFactory;
const util = require("util");
const logsAPI = require("@opentelemetry/api-logs");
const exporter_logs_otlp_http_1 = require("@opentelemetry/exporter-logs-otlp-http");
const otlp_exporter_base_1 = require("@opentelemetry/otlp-exporter-base");
const resources_1 = require("@opentelemetry/resources");
const semantic_conventions_1 = require("@opentelemetry/semantic-conventions");
const sdk_logs_1 = require("@opentelemetry/sdk-logs");
const utils_1 = require("../utils");
const configuration_1 = require("../configuration");
const resource_1 = require("../resource");
function startLogging(options) {
let processors = options.logRecordProcessorFactory(options);
if (!Array.isArray(processors)) {
processors = [processors];
}
const loggerProvider = new sdk_logs_1.LoggerProvider({
resource: options.resource,
processors,
logRecordLimits: {
attributeCountLimit: (0, configuration_1.getConfigNumber)('OTEL_LOGRECORD_ATTRIBUTE_COUNT_LIMIT', (0, configuration_1.getConfigNumber)('OTEL_ATTRIBUTE_COUNT_LIMIT', 128)),
attributeValueLengthLimit: (0, configuration_1.getConfigNumber)('OTEL_LOGRECORD_ATTRIBUTE_VALUE_LENGTH_LIMIT', (0, configuration_1.getConfigNumber)('OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT', 12000)),
},
});
logsAPI.logs.setGlobalLoggerProvider(loggerProvider);
return {
stop: () => {
return loggerProvider.shutdown();
},
};
}
function _setDefaultOptions(options = {}) {
var _a;
const envResource = (0, resource_1.getDetectedResource)();
const serviceName = options.serviceName ||
(0, configuration_1.getNonEmptyConfigVar)('OTEL_SERVICE_NAME') ||
((_a = envResource.attributes) === null || _a === void 0 ? void 0 : _a[semantic_conventions_1.ATTR_SERVICE_NAME]);
const resourceFactory = options.resourceFactory || ((r) => r);
const resource = resourceFactory((0, resources_1.resourceFromAttributes)(envResource.attributes || {}).merge((0, configuration_1.configGetResource)())).merge((0, resources_1.resourceFromAttributes)({
[semantic_conventions_1.ATTR_SERVICE_NAME]: serviceName || (0, utils_1.defaultServiceName)(),
}));
options.logRecordProcessorFactory =
options.logRecordProcessorFactory || defaultLogRecordProcessorFactory;
return {
serviceName: String(resource.attributes[semantic_conventions_1.ATTR_SERVICE_NAME]),
endpoint: options.endpoint, // will use default collector url if not set
logRecordProcessorFactory: options.logRecordProcessorFactory,
resource,
};
}
const SUPPORTED_EXPORTER_TYPES = ['console', 'otlp', 'none'];
function areValidExporterTypes(types) {
return types.every((t) => SUPPORTED_EXPORTER_TYPES.includes(t));
}
function createExporters(options) {
const logExporters = (0, utils_1.getEnvArray)('OTEL_LOGS_EXPORTER') || ['otlp'];
if (!areValidExporterTypes(logExporters)) {
throw new Error(`Invalid value for OTEL_LOGS_EXPORTER env variable: ${util.inspect(logExporters)}. Choose from ${util.inspect(SUPPORTED_EXPORTER_TYPES, {
compact: true,
})} or leave undefined.`);
}
return logExporters.flatMap((type) => {
switch (type) {
case 'otlp': {
const url = (0, utils_1.ensureResourcePath)(options.endpoint, '/v1/logs');
return new exporter_logs_otlp_http_1.OTLPLogExporter({
url,
});
}
case 'console':
return new sdk_logs_1.ConsoleLogRecordExporter();
default:
return [];
}
});
}
function nameStringValuePairsToRecord(pairs) {
if (pairs === undefined)
return undefined;
const record = {};
for (const pair of pairs) {
if (pair.value !== null) {
record[pair.name] = pair.value;
}
}
return record;
}
function toExporter(configExporter) {
var _a, _b;
if (configExporter.console !== undefined) {
return new sdk_logs_1.ConsoleLogRecordExporter();
}
if (configExporter.otlp_http !== undefined) {
const cxp = configExporter.otlp_http;
const url = (_a = cxp.endpoint) !== null && _a !== void 0 ? _a : undefined;
return new exporter_logs_otlp_http_1.OTLPLogExporter({
url,
headers: nameStringValuePairsToRecord(cxp.headers),
compression: cxp.compression === 'gzip'
? otlp_exporter_base_1.CompressionAlgorithm.GZIP
: otlp_exporter_base_1.CompressionAlgorithm.NONE,
timeoutMillis: (_b = cxp.timeout) !== null && _b !== void 0 ? _b : undefined,
});
}
throw new Error(`Unsupported log exporter requested: ${Object.keys(configExporter)[0]}`);
}
function defaultLogRecordProcessorFactory(options) {
var _a, _b, _c, _d;
const loggerFromConfig = (0, configuration_1.getConfigLogger)();
if (loggerFromConfig === undefined) {
let exporters = createExporters(options);
if (!Array.isArray(exporters)) {
exporters = [exporters];
}
return exporters.map((exporter) => new sdk_logs_1.BatchLogRecordProcessor(exporter, {}));
}
const processors = [];
for (const configProcessor of loggerFromConfig.processors) {
if (configProcessor.batch !== undefined) {
const batch = configProcessor.batch;
processors.push(new sdk_logs_1.BatchLogRecordProcessor(toExporter(batch.exporter), {
maxExportBatchSize: (_a = batch.max_export_batch_size) !== null && _a !== void 0 ? _a : undefined,
scheduledDelayMillis: (_b = batch.schedule_delay) !== null && _b !== void 0 ? _b : undefined,
exportTimeoutMillis: (_c = batch.export_timeout) !== null && _c !== void 0 ? _c : undefined,
maxQueueSize: (_d = batch.max_queue_size) !== null && _d !== void 0 ? _d : undefined,
}));
}
else if (configProcessor.simple !== undefined) {
const simple = configProcessor.simple;
processors.push(new sdk_logs_1.SimpleLogRecordProcessor(toExporter(simple.exporter)));
}
}
return processors;
}
//# sourceMappingURL=index.js.map