UNPKG

@azure/monitor-opentelemetry

Version:
59 lines 1.92 kB
// Copyright (c) Microsoft Corporation. // Licensed under the MIT License. import { trace } from "@opentelemetry/api"; import { Logger } from "../shared/logging/index.js"; import { MAIN_AGENT_TARGET_ATTRIBUTES } from "../utils/genaiAttributes.js"; /** * Azure Monitor LogRecord Processor. * @internal */ export class AzureLogRecordProcessor { _metricHandler; constructor(metricHandler) { this._metricHandler = metricHandler; } onEmit(logRecord, context) { try { this._propagateMainAgentAttributesFromActiveSpan(logRecord, context); } catch (error) { Logger.getInstance().warn("Error while propagating main agent attributes onEmit", error); } try { this._metricHandler.recordLog(logRecord); } catch (error) { Logger.getInstance().warn("Error while recording log", error); } } _propagateMainAgentAttributesFromActiveSpan(logRecord, context) { if (!context) { return; } const span = trace.getSpan(context); if (!span) { return; } // The Span returned by trace.getSpan may be a non-recording span or a // foreign implementation that does not expose `attributes`. const spanAttributes = span.attributes; if (!spanAttributes) { return; } // Copy only the spec-defined main-agent attributes rather than scanning // every attribute on the span by prefix. for (const key of MAIN_AGENT_TARGET_ATTRIBUTES) { const value = spanAttributes[key]; if (value !== undefined) { logRecord.setAttribute(key, value); } } } forceFlush() { return Promise.resolve(); } shutdown() { return Promise.resolve(); } } //# sourceMappingURL=logRecordProcessor.js.map