All files logging.ts

92.5% Statements 111/120
88.46% Branches 46/52
100% Functions 24/24
94.06% Lines 111/118

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307                              3x     3x     3x                                       1x 1x     3x 3x 3x 3x 3x 3x 3x 3x     3x                 3x                           1x 1x 1x                           32x   32x 32x     32x 32x 4x 4x   28x       1x       1x       1x       1x       1x       1x       6x       1x 1x       2x       1x 1x                   6x 2x 2x   4x 4x 1x   4x 2x   4x 1x   4x 4x 1x   4x 4x 4x 4x 4x     4x 4x 4x 4x       7x 7x 7x 7x 7x 7x             7x       28x 2x   28x 28x       28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x               28x 28x             37x 37x 4x 4x 3x   1x     37x         28x 28x     28x 28x             32x 32x 32x 32x 9x 9x         23x 8x       15x 5x 10x 10x       32x               1x       3x       1x      
// SPDX-FileCopyrightText: 2026 Anaconda, Inc
// SPDX-License-Identifier: Apache-2.0
 
import { type AttrMap } from './types.js';
import { Configuration, type EndpointTuple } from './config.js';
import { ResourceAttributes } from './attributes.js';
import { AnacondaCommon } from "./common.js";
import { LogRecordExporterShim } from './exporter_shims.js';
 
 
// ----- your value imports (keep as-is) -----
import { diag, DiagConsoleLogger, DiagLogLevel } from '@opentelemetry/api';
 
import { logs, SeverityNumber, type Logger } from '@opentelemetry/api-logs';
import * as httpNS from '@opentelemetry/exporter-logs-otlp-http';
const { OTLPLogExporter: OTLPLogExporterHTTP } = httpNS;
 
import * as grpcExporterNS from '@opentelemetry/exporter-logs-otlp-grpc';
const { OTLPLogExporter: OTLPLogExporterGRPC } = grpcExporterNS;
 
import grpc from '@grpc/grpc-js';
const { ChannelCredentials } = grpc;
 
import type { ChannelCredentials as _ChannelCredentials } from '@grpc/grpc-js';
 
import {
    LoggerProvider,
    type LogRecordExporter,
    type ReadableLogRecord,
    ConsoleLogRecordExporter,
    BatchLogRecordProcessor,
 } from '@opentelemetry/sdk-logs';
import { type ExportResult, ExportResultCode } from '@opentelemetry/core';
 
// ----- local type aliases that REUSE the value names -----
type ChannelCredentials = _ChannelCredentials;
 
/**
 * Arguments for calls to any log methods. See documentation for ATelLogger.
 */
export class LogArgs {
    message: string = "<No Log Message>";
    attributes?: AttrMap = {};
};
 
enum LogLevel {
    UNSPECIFIED = 0,
    TRACE = 1,
    DEBUG = 5,
    INFO = 9,
    WARN = 13,
    ERROR = 17,
    FATAL = 21
};
 
const _conversionMap: Record<LogLevel,SeverityNumber> = {
    [LogLevel.UNSPECIFIED]: SeverityNumber.UNSPECIFIED,
    [LogLevel.TRACE]: SeverityNumber.TRACE,
    [LogLevel.DEBUG]: SeverityNumber.DEBUG,
    [LogLevel.INFO]: SeverityNumber.INFO,
    [LogLevel.WARN]: SeverityNumber.WARN,
    [LogLevel.ERROR]: SeverityNumber.ERROR,
    [LogLevel.FATAL]: SeverityNumber.FATAL
};
const _nameMap: Record<LogLevel,string> = {
    [LogLevel.UNSPECIFIED]: "UNSPECIFIED",
    [LogLevel.TRACE]: "TRACE",
    [LogLevel.DEBUG]: "DEBUG",
    [LogLevel.INFO]: "INFO",
    [LogLevel.WARN]: "WARN",
    [LogLevel.ERROR]: "ERROR",
    [LogLevel.FATAL]: "FATAL"
};
 
/**
 * Arguments for calls to sendEvent(). See sendEvent documentation.
 */
export class EventArgs {
    eventName: string = "<missing_event_name>>";
    payload: Record<string,any> = {};
    attributes?: AttrMap = {};
};
 
export interface ATelLogger {
    trace(args: LogArgs): void;
    debug(args: LogArgs): void;
    info(args: LogArgs): void;
    warn(args: LogArgs): void;
    error(args: LogArgs): void;
    fatal(args: LogArgs): void;
    sendEvent(args: EventArgs): void;
};
 
export class AnacondaLogging extends AnacondaCommon implements ATelLogger {
    provider: LoggerProvider | null = null
    private processor: BatchLogRecordProcessor | undefined
    private _logger: Logger | undefined = undefined
    parentExporter: LogRecordExporterShim | undefined = undefined
 
    constructor(config: Configuration, attributes: ResourceAttributes) {
        super(config, attributes);
        if (this.isValidOtelUrl(this.config.getLoggingEndpointTuple()[0].href) === false) {
            console.error(`The logs endpoint URL is not valid: ${this.config.getLoggingEndpointTuple()[0].href}`)
            return
        }
        this.setup()
    }
 
    trace(args: LogArgs): void {
        this.log(LogLevel.TRACE, args)
    }
 
    debug(args: LogArgs): void {
        this.log(LogLevel.DEBUG, args)
    }
 
    info(args: LogArgs): void {
        this.log(LogLevel.INFO, args)
    }
 
    warn(args: LogArgs): void {
        this.log(LogLevel.WARN, args)
    }
 
    error(args: LogArgs): void {
        this.log(LogLevel.ERROR, args)
    }
 
    fatal(args: LogArgs): void {
        this.log(LogLevel.FATAL, args)
    }
 
    private log(level: LogLevel, args: LogArgs): void {
        this.internalSendEvent("__LOG__", level, args.message, args.attributes)
    }
 
    sendEvent(args: EventArgs): void {
        let json = JSON.stringify(args.payload);
        this.internalSendEvent(args.eventName, LogLevel.UNSPECIFIED, json, args.attributes)
    }
 
    getLogger(): ATelLogger {
        return this
    }
 
    async flush(): Promise<void> {
        try {
            await this.processor?.forceFlush()
        } catch (error) {
            // Log export failures instead of crashing the application
            // This matches Python SDK behavior where export failures are logged
            this._warn(`Logging export failed: ${this.errorMessage(error)}`)
        }
    }
 
    async changeConnection(endpoint: URL | undefined, authToken: string | undefined,
                           certFile: string | undefined, userId: string | undefined): Promise<boolean> {
        if (endpoint && this.isValidOtelUrl(endpoint!.href) === false) {
            console.error(`The logs endpoint URL is not valid: ${endpoint!.href}`)
            return false
        }
        let [url, token, cert] = this.config.getLoggingEndpointTuple()
        if (endpoint !== url && endpoint !== undefined) {
            this.config.loggingEndpoint![0] = endpoint
        }
        if (authToken !== token) {
            this.config.loggingEndpoint![1] = authToken
        }
        if (certFile !== cert) {
            this.config.loggingEndpoint![2] = certFile
        }
        let id = userId?.trim()
        if (typeof id === 'string' && id.length > 0) {
            this.attributes.userId = id
        }
        var [scheme, ep] = this.transformURL(this.config.loggingEndpoint![0])
        var creds: ChannelCredentials | undefined = this.readCredentials(scheme, this.config.loggingEndpoint![2])
        var headers = this.makeHeaders(scheme, authToken)
        var exporter = this.makeExporter(scheme, ep, headers, creds)
        Iif (exporter === undefined) {
            return false
        }
        await this.processor?.forceFlush()
        var oldExporter = await this.parentExporter?.swapExporter(exporter!)
        await oldExporter?.shutdown()
        return true
    }
 
    private internalSendEvent(name: string, level: LogLevel, payloadStr: string, attributes?: AttrMap): void {
        Iif (this._logger === undefined) { console.warn("### ERROR: No Logger!!!"); return }
        let severity = _conversionMap[level]
        let severityName = _nameMap[level]
        let updatedAttributes = this.makeEventAttributes(attributes)
        updatedAttributes['log.event.name'] = name
        let record = {
            severityNumber: severity,
            severityText: severityName,
            eventName: name,
            body: payloadStr,
            attributes: updatedAttributes
        }
        this._logger?.emit(record)
    }
 
    private setup(): void {
        if (this.config.useDebug) {
            diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.DEBUG);
        }
        var [endpoint, authToken, certFile] = this.config.getLoggingEndpointTuple()
        Iif (!this.isValidOtelUrl(endpoint.href)) {
            console.error(`The logs endpoint URL is not valid: ${endpoint.href}`)
            return
        }
        const scheme = endpoint.protocol
        const ep = new URL(endpoint.href)
        this._debug(`Connecting to logging endpoint '${ep.href}'.`)
        ep.protocol = ep.protocol.replace("grpcs:", "https:")
        ep.protocol = ep.protocol.replace("grpc:", "http:")
        var creds: ChannelCredentials | undefined = this.readCredentials(scheme, certFile)
        const headers: Record<string,string> = authToken ? { 'Authorization': `Bearer ${authToken}` } : {}
        const processor: BatchLogRecordProcessor | undefined = this.makeBatchProcessor(scheme, ep, headers, creds)
        if (processor) {
            this.processor = processor
            this.provider = new LoggerProvider({
                resource: this.resources,
                processors: [this.processor],
                logRecordLimits: {
                    attributeCountLimit: 150, // no more than 150 keys in attributes...
                    attributeValueLengthLimit: 4096, // A key's value must not exceed this in bytes.
                }
            })
            logs.setGlobalLoggerProvider(this.provider!);
            this._logger = logs.getLogger(this.attributes.getServiceName())
        } else E{
            console.warn('Failed to create a batch processor for logging!')
        }
    }
 
    /* private */ readCredentials(scheme: string, certFile?: string): ChannelCredentials | undefined {
        var creds: ChannelCredentials | undefined = undefined
        if (certFile !== undefined && scheme === ("grpcs:")) {
            const certContent = this.readCertFile(certFile)
            if (certContent) {
                creds = ChannelCredentials.createSsl(Buffer.from(certContent))
            } else {
                this._warn(`Failed to read certificate file: ${certFile}`)
            }
        }
        return creds
    }
 
    makeBatchProcessor(scheme: string, url: URL, httpHeaders: Record<string,string>,
                       creds?: ChannelCredentials): BatchLogRecordProcessor | undefined {
        var exporter = this.makeExporter(scheme, url, httpHeaders, creds)
        Iif (exporter === undefined) {
            return undefined
        }
        this.parentExporter = new LogRecordExporterShim(exporter!)
        return new BatchLogRecordProcessor(this.parentExporter!, {
            scheduledDelayMillis: this.config.getLoggingExportIntervalMs()
        })
    }
 
    private makeExporter(scheme: string, url: URL, httpHeaders: Record<string,string>,
                         creds?: ChannelCredentials): LogRecordExporter | undefined {
        var exporter: LogRecordExporter | undefined = undefined
        var urlStr = url.href
        this._debug(`Creating log exporter at endpoint ${urlStr}`)
        if (scheme === 'grpc:' || scheme === 'grpcs:') {
            urlStr = `${url.hostname}:${url.port}`
            exporter = new OTLPLogExporterGRPC({
                url: urlStr,
                headers: httpHeaders,
                credentials: creds
            });
        } else if (scheme === 'http:' || scheme === 'https:') {
            exporter = new OTLPLogExporterHTTP({
                url: urlStr,
                headers: httpHeaders
            });
        } else if (scheme === 'console:') {
            exporter = new ConsoleLogRecordExporter()
        } else if (scheme === 'devnull:') {
            exporter = new NoopLogExporter()
        } else E{
            this._warn(`Received bad scheme for logging: ${scheme}!`)
        }
        return exporter
    }
}
 
export class NoopLogExporter implements LogRecordExporter {
    constructor(_options?: any) {}
 
    export(logs: ReadableLogRecord[], resultCallback: (result: ExportResult) => void): void {
        resultCallback({ code: 0 });
    }
 
    async shutdown(): Promise<void> {
        return Promise.resolve();
    }
 
    async forceFlush(): Promise<void> {
        return Promise.resolve();
    }
}