UNPKG

@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.

96 lines 4.14 kB
"use strict"; /* * 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 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 resource_1 = require("../resource"); function startLogging(options) { const loggerProvider = new sdk_logs_1.LoggerProvider({ resource: options.resource, }); let processors = options.logRecordProcessorFactory(options); if (!Array.isArray(processors)) { processors = [processors]; } processors.forEach((processor) => loggerProvider.addLogRecordProcessor(processor)); logsAPI.logs.setGlobalLoggerProvider(loggerProvider); return { stop: () => { return loggerProvider.shutdown(); }, }; } function _setDefaultOptions(options = {}) { const envResource = (0, resource_1.getDetectedResource)(); const serviceName = options.serviceName || (0, utils_1.getNonEmptyEnvVar)('OTEL_SERVICE_NAME') || envResource.attributes[semantic_conventions_1.ATTR_SERVICE_NAME]; const resourceFactory = options.resourceFactory || ((r) => r); const resource = resourceFactory(envResource).merge(new resources_1.Resource({ [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((0, utils_1.getNonEmptyEnvVar)('OTEL_LOGS_EXPORTER'))}. 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 defaultlogRecordProcessorFactory(options) { let exporters = createExporters(options); if (!Array.isArray(exporters)) { exporters = [exporters]; } return exporters.map((exporter) => new sdk_logs_1.BatchLogRecordProcessor(exporter, {})); } //# sourceMappingURL=index.js.map