autotel
Version:
Write Once, Observe Anywhere
196 lines (194 loc) • 6.4 kB
JavaScript
import { getLogger } from './chunk-W35FVJBC.js';
import './chunk-3SDILILG.js';
import { TailSamplingSpanProcessor } from './chunk-A4E5AQFK.js';
import './chunk-WGWSHJ2N.js';
import './chunk-GYR5K654.js';
import './chunk-TDNKIHKT.js';
import './chunk-6UQRVUN3.js';
import './chunk-3QXBFGKP.js';
import { requireModule } from './chunk-33WTKH7X.js';
import './chunk-DPSA4QLA.js';
import './chunk-55ER2KD5.js';
import './chunk-J5QENANM.js';
import './chunk-HA2WBOGQ.js';
import './chunk-DGUM43GV.js';
import { NodeSDK } from '@opentelemetry/sdk-node';
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http';
import { OTLPLogExporter } from '@opentelemetry/exporter-logs-otlp-http';
import { BatchLogRecordProcessor } from '@opentelemetry/sdk-logs';
import { BatchSpanProcessor } from '@opentelemetry/sdk-trace-base';
import { processDetector, hostDetector, detectResources, resourceFromAttributes } from '@opentelemetry/resources';
import { ATTR_SERVICE_VERSION, ATTR_SERVICE_NAME } from '@opentelemetry/semantic-conventions/incubating';
import { OTLPMetricExporter } from '@opentelemetry/exporter-metrics-otlp-http';
import { PeriodicExportingMetricReader } from '@opentelemetry/sdk-metrics';
function parseOtlpHeaders(headersString) {
if (!headersString) return {};
const headers = {};
const pairs = headersString.split(",");
for (const pair of pairs) {
const [key, ...valueParts] = pair.split("=");
if (key && valueParts.length > 0) {
headers[key.trim()] = valueParts.join("=").trim();
}
}
return headers;
}
function parseResourceAttributes(attributesString) {
if (!attributesString) return {};
const attributes = {};
const pairs = attributesString.split(",");
for (const pair of pairs) {
const [key, ...valueParts] = pair.split("=");
if (key && valueParts.length > 0) {
attributes[key.trim()] = valueParts.join("=").trim();
}
}
return attributes;
}
var currentSDK = null;
var shutdownHandlerRegistered = false;
async function shutdownInstrumentation(sdk) {
const sdkToShutdown = sdk || currentSDK;
if (!sdkToShutdown) {
getLogger().warn({}, "No SDK to shutdown");
return;
}
try {
await sdkToShutdown.shutdown();
getLogger().info({}, "OpenTelemetry terminated successfully");
if (sdkToShutdown === currentSDK) {
currentSDK = null;
}
} catch (error) {
getLogger().error(
{
err: error instanceof Error ? error : void 0
},
"Error terminating OpenTelemetry"
);
throw error;
}
}
async function initInstrumentation(config) {
if (currentSDK) {
getLogger().info(
{},
"Shutting down existing OpenTelemetry SDK before reinitializing..."
);
await shutdownInstrumentation(currentSDK);
}
const otlpHeaders = parseOtlpHeaders(config.headers);
const customResourceAttributes = parseResourceAttributes(
config.resourceAttributes
);
let resource;
const detectors = [processDetector, hostDetector];
try {
const awsDetectors = await import('@opentelemetry/resource-detector-aws');
detectors.push(
awsDetectors.awsEc2Detector,
awsDetectors.awsEcsDetector,
awsDetectors.awsEksDetector
);
} catch {
}
try {
const gcpDetectors = await import('@opentelemetry/resource-detector-gcp');
detectors.push(gcpDetectors.gcpDetector);
} catch {
}
try {
const containerDetectors = await import('@opentelemetry/resource-detector-container');
detectors.push(containerDetectors.containerDetector);
} catch {
}
if (config.detectResources) {
const detectedResource = await detectResources({
detectors
});
resource = detectedResource.merge(
resourceFromAttributes({
[ATTR_SERVICE_NAME]: config.serviceName,
[ATTR_SERVICE_VERSION]: config.serviceVersion || "1.0.0",
"deployment.environment": config.deploymentEnvironment || "development",
...customResourceAttributes
// Merge custom resource attributes
})
);
} else {
resource = resourceFromAttributes({
[ATTR_SERVICE_NAME]: config.serviceName,
[ATTR_SERVICE_VERSION]: config.serviceVersion || "1.0.0",
"deployment.environment": config.deploymentEnvironment || "development",
...customResourceAttributes
// Merge custom resource attributes
});
}
let instrumentations = config.instrumentations || [];
if (config.selectiveInstrumentation === false) {
const mod = requireModule("@opentelemetry/auto-instrumentations-node");
instrumentations = [mod.getNodeAutoInstrumentations()];
}
const traceExporter = new OTLPTraceExporter({
url: `${config.otlpEndpoint || "http://localhost:4318"}/v1/traces`,
headers: otlpHeaders
});
const spanProcessor = new TailSamplingSpanProcessor(
new BatchSpanProcessor(traceExporter)
);
const sdk = new NodeSDK({
resource,
spanProcessor,
// Use our wrapped processor instead of traceExporter directly
metricReader: new PeriodicExportingMetricReader({
exporter: new OTLPMetricExporter({
url: `${config.otlpEndpoint || "http://localhost:4318"}/v1/metrics`,
headers: otlpHeaders
})
}),
logRecordProcessors: [
new BatchLogRecordProcessor(
new OTLPLogExporter({
url: `${config.otlpEndpoint || "http://localhost:4318"}/v1/logs`,
headers: otlpHeaders
})
)
],
instrumentations
});
try {
await sdk.start();
getLogger().info({}, "OpenTelemetry instrumentation started successfully");
} catch (error) {
getLogger().error(
{
err: error instanceof Error ? error : void 0
},
"Failed to start OpenTelemetry SDK"
);
throw error;
}
currentSDK = sdk;
if (!shutdownHandlerRegistered) {
shutdownHandlerRegistered = true;
const shutdownHandler = () => {
shutdownInstrumentation().then(() => {
process.exit(0);
}).catch((error) => {
getLogger().error(
{
err: error instanceof Error ? error : void 0
},
"Shutdown error"
);
process.exit(1);
});
};
process.on("SIGTERM", shutdownHandler);
process.on("SIGINT", shutdownHandler);
}
return sdk;
}
export { initInstrumentation, shutdownInstrumentation };
//# sourceMappingURL=instrumentation.js.map
//# sourceMappingURL=instrumentation.js.map