All files traces.ts

99.11% Statements 112/113
93.47% Branches 43/46
100% Functions 22/22
99.11% Lines 112/113

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                                2x     2x     2x     2x     2x     2x                                       1x 1x 1x                                                                                       4x 4x       2x       2x       2x       4x 2x           31x   31x       31x 31x       4x 4x 2x   4x 2x   4x 2x   4x 4x 4x 4x 4x 1x   3x 3x 3x 3x       2x 1x   1x 1x 1x 1x 1x 1x   1x 1x 1x 1x       2x 1x   1x 1x 1x 1x 1x 1x   1x 1x 1x 1x         35x 35x 35x 9x 9x         26x 8x       18x 6x 12x 7x   5x   35x         31x 31x 4x   27x 27x       40x 40x 4x 4x 3x   1x     40x       31x     31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 27x 27x       27x             2x         3x   3x 1x       6x     1x 1x     1x 1x             1x       3x       1x      
// SPDX-FileCopyrightText: 2025 Anaconda, Inc
// SPDX-License-Identifier: Apache-2.0
 
import * as fs from 'fs';
 
import { type AttrMap, type CarrierMap } from './types.js'
import { Configuration, type EndpointTuple } from './config.js'
import { ResourceAttributes } from './attributes.js'
import { AnacondaCommon } from "./common.js"
import { __noopASpan } from './signals-state.js'
import { SpanExporterShim } from './exporter_shims.js';
 
// ----- values -----
import { diag, DiagConsoleLogger, DiagLogLevel } from '@opentelemetry/api';
 
import * as otlpTraceHttpNS from '@opentelemetry/exporter-trace-otlp-http';
const { OTLPTraceExporter: OTLPTraceExporterHTTP } = otlpTraceHttpNS;
 
import * as otlpTraceGrpcNS from '@opentelemetry/exporter-trace-otlp-grpc';
const { OTLPTraceExporter: OTLPTraceExporterGRPC } = otlpTraceGrpcNS;
 
import * as sdkTraceBaseNS from '@opentelemetry/sdk-trace-base';
const { ConsoleSpanExporter, BatchSpanProcessor } = sdkTraceBaseNS;
 
import * as sdkTraceNodeNS from '@opentelemetry/sdk-trace-node';
const { NodeTracerProvider } = sdkTraceNodeNS;
 
import * as apiNS from '@opentelemetry/api';
const { SpanStatusCode, trace } = apiNS;
 
import grpc from '@grpc/grpc-js';
const { ChannelCredentials } = grpc;
 
// ----- types -----
import type { Span, Context } from '@opentelemetry/api';
import type {
  SpanExporter as _SpanExporter,
  ReadableSpan as _ReadableSpan,
  BatchSpanProcessor as _BatchSpanProcessor,
} from '@opentelemetry/sdk-trace-base';
import type { NodeTracerProvider as _NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
import type { ChannelCredentials as _ChannelCredentials } from '@grpc/grpc-js';
 
// ----- local type aliases (reuse runtime names) -----
type SpanExporter = _SpanExporter;
type ReadableSpan = _ReadableSpan;
type BatchSpanProcessor = _BatchSpanProcessor;
type NodeTracerProvider = _NodeTracerProvider;
type ChannelCredentials = _ChannelCredentials;
 
export class TraceArgs {
    name: string = "";
    attributes?: AttrMap = {};
    carrier?: CarrierMap = {};
}
 
/**
 * Represents a span in a tracing system, providing methods to record events, exceptions, errors, and attributes.
 *
 * @remarks
 * This interface is typically used to instrument code for distributed tracing, allowing you to annotate spans with additional information.
 */
export interface ASpan {
    /**
     * Adds an event with the specified name and optional attributes.
     *
     * @param name - The name of the event to add.
     * @param attributes - Optional key-value pairs providing additional information about the event.
     */
    addEvent(name: string, attributes?: AttrMap): void;
 
    /**
     * Adds an exception to the current context.
     *
     * @param exception - The error object to be added as an exception.
     */
    addException(exception: Error): void;
 
    /**
     * Sets the error status for the current signal.
     *
     * @param msg - Optional error message describing the reason for the error status.
     */
    setErrorStatus(msg?: string): void;
 
    /**
     * Adds the specified attributes to the current object.
     *
     * @param attributes - A record containing key-value pairs of attributes to add.
     */
    addAttributes(attributes: AttrMap): void;
}
 
export class ASpanImpl implements ASpan {
    private span: Span
 
    constructor(span: Span) {
        this.span = span
        this.span.setStatus({code: SpanStatusCode.OK, message: ""})
    }
 
    addEvent(name: string, attributes: AttrMap): void {
        this.span.addEvent(name, attributes)
    }
 
    addException(exception: Error): void {
        this.span.recordException(exception)
    }
 
    setErrorStatus(msg?: string): void {
        this.span.setStatus({code: SpanStatusCode.ERROR, message: "The trace code block recorded an error."})
    }
 
    addAttributes(attributes: AttrMap): void {
        for (let key of Object.keys(attributes)) {
            this.span.setAttribute(key, attributes[key])
        }
    }
}
 
export class AnacondaTrace extends AnacondaCommon {
    provider: NodeTracerProvider | null = null
    private processor: BatchSpanProcessor | undefined
    private depth: number = 0
    parentExporter:SpanExporterShim | undefined
 
    constructor(config: Configuration, attributes: ResourceAttributes) {
        super(config, attributes)
        this.setup()
    }
 
    async changeConnection(endpoint: URL | undefined, authToken: string | undefined, certFile: string | undefined): Promise<boolean> {
        let [url, token, cert] = this.config.getTraceEndpointTuple()
        if (endpoint !== url && endpoint !== undefined) {
            this.config.traceEndpoint![0] = endpoint
        }
        if (authToken !== token) {
            this.config.traceEndpoint![1] = authToken
        }
        if (certFile !== cert) {
            this.config.traceEndpoint![2] = certFile
        }
        var [scheme, ep] = this.transformURL(this.config.traceEndpoint![0])
        var creds: ChannelCredentials | undefined = this.readCredentials(scheme, this.config.traceEndpoint![2])
        var headers = this.makeHeaders(scheme, authToken)
        var exporter = this.makeExporter(scheme, ep, headers, creds)
        if (exporter === undefined) {
            return false
        }
        await this.processor?.forceFlush()
        var oldExporter = await this.parentExporter?.swapExporter(exporter!)
        await oldExporter?.shutdown()
        return true
    }
 
    traceBlock(args: TraceArgs, block: (span: ASpan) => void): void {
        if (!this.isValidName(args.name)) {
            throw Error(`Trace name '${args.name}' is not a valid name (^[A-Za-z][A-Za-z_0-9]+$).`)
        }
        const tracer = trace.getTracer(this.serviceName, this.serviceVersion);
        const context: Context = this.convertToContext(args.carrier!)
        const span = tracer.startSpan(args.name, undefined, context)
        this.depth++
        for (let key of Object.keys(args.attributes ? args.attributes : {})) {
            span.setAttribute(key, args.attributes![key])
        }
        block(new ASpanImpl(span))
        span.end()
        this.depth--
        this.provider!.forceFlush()
    }
 
    async traceBlockAsync(args: TraceArgs, block: (span: ASpan) => Promise<void>): Promise<void> {
        if (!this.isValidName(args.name)) {
            throw Error(`Trace name '${args.name}' is not a valid name (^[A-Za-z][A-Za-z_0-9]+$).`)
        }
        const tracer = trace.getTracer(this.serviceName, this.serviceVersion);
        const context: Context = this.convertToContext(args.carrier!)
        const span = tracer.startSpan(args.name, undefined, context)
        this.depth++
        for (let key of Object.keys(args.attributes ? args.attributes : {})) {
            span.setAttribute(key, args.attributes![key])
        }
        block(new ASpanImpl(span))
        span.end()
        this.depth--
        await this.provider!.forceFlush()
    }
 
    private makeExporter(scheme: string, url: URL, httpHeaders: Record<string,string>,
                         creds?: ChannelCredentials): SpanExporter | undefined {
        var exporter: SpanExporter | undefined = undefined
        var urlStr = url.href
        if (scheme === 'grpc:' || scheme === 'grpcs:') {
            urlStr = `${url.hostname}:${url.port}`
            exporter = new OTLPTraceExporterGRPC({
                url: urlStr,
                headers: httpHeaders,
                 credentials: creds
            });
        } else if (scheme === 'http:' || scheme === 'https:') {
            exporter = new OTLPTraceExporterHTTP({
                url: urlStr,
                headers: httpHeaders
            });
        } else if (scheme === 'console:') {
            exporter = new ConsoleSpanExporter()
        } else if (scheme === 'devnull:') {
            exporter = new NoopSpanExporter()
        } else {
            this.warn(`Received bad scheme for tracing: ${scheme}!`)
        }
        return exporter
    }
 
    makeBatchProcessor(scheme: string, url: URL, httpHeaders: Record<string,string>,
                       creds?: ChannelCredentials): BatchSpanProcessor | undefined {
        var exporter = this.makeExporter(scheme, url, httpHeaders, creds)
        if (exporter === undefined) {
            return undefined
        }
        this.parentExporter = new SpanExporterShim(exporter!)
        return new BatchSpanProcessor(this.parentExporter!)
    }
 
    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
    }
 
    private setup(): void {
        Iif (this.config.useDebug) {
            diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.DEBUG);
        }
        var [endpoint, authToken, certFile] = this.config.getTraceEndpointTuple()
        const scheme = endpoint.protocol
        const ep = new URL(endpoint.href)
        this.debug(`Connecting to traces 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: BatchSpanProcessor | undefined = this.makeBatchProcessor(scheme, ep, headers, creds)
        if (processor) {
            this.processor = processor
            this.provider = new NodeTracerProvider({
                spanProcessors: [this.processor],
                resource: this.resources
            })
            this.provider!.register()
        } else {
 
        }
    }
 
    private convertToContext(carrier: CarrierMap): Context {
        return new LocalContext(carrier)
    }
}
 
export class LocalContext implements Context {
    map: Record<symbol, unknown> = {}
    constructor(carrier: CarrierMap) {
        for (let key of Object.keys(carrier ? carrier : {})) {
            this.map[Symbol(key)] = carrier[key]
        }
    }
    getValue(key: symbol): unknown {
        return this.map[key]
    }
    setValue(key: symbol, value: unknown): Context {
        this.map[key] = value
        return this
    }
    deleteValue(key: symbol): Context {
        delete this.map[key]
        return this
    }
}
 
export class NoopSpanExporter implements SpanExporter {
    export(_spans: ReadableSpan[], resultCallback: (result: { code: number }) => void): void {
        // Immediately report success without doing anything
        resultCallback({ code: 0 });
    }
 
    shutdown(): Promise<void> {
        return Promise.resolve();
    }
 
    forceFlush(): Promise<void> {
        return Promise.resolve();
    }
}