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 | 13x 13x 1x 1x 12x 13x 9x 9x 4x 4x 12x 1x 1x 11x 11x 11x 10x 10x 5x 5x 2x 1x 1x 1x 2x 1x 1x 1x 3x 1x 1x 2x 2x 1x 1x 1x 1x 3x 2x 2x 2x 1x | // SPDX-FileCopyrightText: 2025 Anaconda, Inc
// SPDX-License-Identifier: Apache-2.0
import { Configuration } from './config.js';
import { ResourceAttributes } from './attributes.js';
import { AnacondaMetrics, CounterArgs, HistogramArgs } from './metrics.js';
import { AnacondaTrace, type ASpan, TraceArgs } from './traces.js';
import { localTimeString as lts } from './common.js';
import {
__initialized,
__metrics,
__tracing,
__noopASpan,
__setInitialized,
__setMetrics,
__setTracing,
} from './signals-state.js';
/**
* Possible signals used in this API.
*/
export type Signal = 'metrics' | 'tracing';
/**
* Initializes telemetry signals such as metrics and traces based on the provided configuration.
*
* @param config - The telemetry configuration object.
* @param attributes - Resource attributes to associate with telemetry data.
* @param signalTypes - An array of signal types to initialize (e.g., "metrics", "tracing"). Defaults to ["metrics"].
*
* @returns true is successful, otherwise false.
*
* @remarks
* - If telemetry has already been initialized, this function does nothing.
* - Currently, only metrics initialization is implemented; tracing is a placeholder.
* - Unknown signal types will trigger a warning in the console.
*/
export function initializeTelemetry(config: Configuration,
attributes: ResourceAttributes,
signalTypes: Array<Signal> = ["metrics"]): boolean {
console.debug(`${lts()} > *** initializeTelemetry called...`)
if (__initialized) {
console.debug(`${lts()} > *** already initialized, returning true.`)
return true // If already initialized, do nothing.
}
for (const signalType of signalTypes) {
switch (signalType) {
case "metrics":
__setMetrics(new AnacondaMetrics(config, attributes))
break;
case "tracing":
__setTracing(new AnacondaTrace(config, attributes))
break
// default:
// console.warn(`${lts()} > *** WARNING: Unknown signal type: ${signalType}`)
// break
}
}
if (!__metrics && !__tracing) {
console.warn(`${lts()} > *** WARNING: No telemetry signals initialized. Ensure at least one signal type is specified.`)
return false
}
__setInitialized(true) // Mark as initialized
console.debug(`${lts()} > *** initializeTelemetry finished.`)
return true
}
/**
* Function used to change the endpoint or authorization token or both for a signal type.
*
* @param signal - Either 'metrics' or 'tracing' to select which connection to change.
* @param endpoint - The optional new endpoint for the specific signal.
* @param authToken - The optional new authorization token for the connection to use.
* @param certFile - The optional certificate file for mTLS.
*
* @returns - true if successful, false if it failed.
*
* @remarks
* - Should be called from an async method with await, if not consequences are:
* * If changing endpoints data around the change MAY end up in either endpoint or both.
* * Order of other telemetry may be different than expected.
* - This method does not throw.
*/
export async function changeSignalConnection(signal: Signal, endpoint: URL | undefined,
authToken: string | undefined = undefined,
certFile: string | undefined = undefined): Promise<boolean> {
Iif (!__initialized) {
return false
}
if (signal === 'metrics') {
return await __metrics?.changeConnection(endpoint, authToken, certFile) ?? false
} else {
return await __tracing?.changeConnection(endpoint, authToken, certFile) ?? false
}
}
/**
* Records a value in a histogram metric with optional attributes.
*
* @param args - An argument list object where the `name` field is required.
*
* The args is an object defined by (in any order):
* ```
* {
* name: string = ""; Required; Not supplying a name will result in no value being recorded.
* value: number = 0; Required; This field will be recorded as the value.
* attributes?: AttrMap = {}; Optional; Attributes for the value metric.
* }
* ```
*
* @returns `true` if the histogram was recorded successfully, `false` if metrics are not initialized.
*
* @example
* ```typescript
* recordHistogram({name: "validName", value: 42.0, attributes: { "name": "Value" }})
* ```
*/
export function recordHistogram(args: HistogramArgs): boolean {
if (!__metrics) {
console.warn("*** WARNING: Metrics not initialized. Call initializeTelemetry first.")
return false
}
return __metrics.recordHistogram(args); // Call the recordHistogram method on the AnacondaMetrics instance
}
/**
* Increments a named counter by a specified value and optional attributes.
*
* @param args - An argument list object where the `name` field is required.
*
* The args is an object defined by (in any order):
*
* ```
* {
* name: string = ""; Required; Not supplying a name will result in no value being recorded.
* by?: number = 1; Optional; Not supplying this field will result in the counter being incremented by `1`.
* forceUpDownCounter?: boolean = false; Optional; Counters by default are incrementing only, set to `true` to force an updown counter.
* attributes?: AttrMap = {}; Optional; Attributes for the counter metric.
* }
* ```
*
* @returns `true` if the counter was incremented successfully, `false` if metrics are not initialized.
*
* @example
* ```typescript
* // Creates a incrementing only counter set to `1` or increments an existing counter by `1`.
* incrementCounter({name: "validName"})
*
* // Creates a incrementing and decrementing counter set to `1` or increments an existing counter by `1`.
* incrementCounter({forceUpDown: true, name: "upDownValidName", attributes: {"name": "value"}})
*
* // Creates a incrementing and decrementing counter set to `5` or increments an existing counter by `5`.
* incrementCounter({by: 5, name: "newCounter", forceUpDown: true})
* ```
*/
export function incrementCounter(args: CounterArgs): boolean {
if (!__metrics) {
console.warn("*** WARNING: Metrics not initialized. Call initializeTelemetry first.")
return false
}
return __metrics.incrementCounter(args); // Call the incrementCounter method on the AnacondaMetrics instance
}
/**
* Decrements the specified counter by a given value.
*
* @param args - An argument list object where the `name` field is required.
*
* The args is an object defined by (in any order):
*
* ```
* {
* name: string = ""; Required; Not supplying a name will result in no value being recorded.
* by?: number = 1; Optional; Not supplying this field will result in the counter being incremented by `1`.
* forceUpDownCounter?: boolean = true; Optional; Reguardless of this value, an up down counter will be created.
* attributes?: AttrMap = {}; Optional; Attributes for the counter metric.
* }
* ```
*
* @returns `true` if the counter was decremented successfully, `false` if metrics are not initialized.
*
* @example
* ```typescript
* // Creates an up down counter set to -2, or decrements the already created counter by `2`.
* decrementCounter({name: "validName", by: 2, attributes: {"name": "value"}})
* ```
*/
export function decrementCounter(args: CounterArgs): boolean {
if (!__metrics) {
console.warn("*** WARNING: Metrics not initialized. Call initializeTelemetry first.")
return false
}
return __metrics.decrementCounter(args); // Call the decrementCounter method on the AnacondaMetrics instance
}
/**
* Executes a block of code (async) within a tracing context, optionally attaching
* attributes and a carrier.
*
* @param args - An argument list object where the `name` field is required.
*
* The args is an object defined by (in any order):
*
* ```
* {
* name: string = ""; Required; Not supplying a name will result in no value being recorded.
* attributes?: AttrMap = {}; Optional; Attributes for the counter metric.
* carrier?: CarrierMap = {}; Optional; Used to create a context for the trace block.
* }
* ```
*
* @remarks
* - If tracing is not initialized, a warning is logged and the block is executed with a no-op span.
* - ___IMPORTANT___: Calling `reinitializeTelemetry` from within a traceBlock will result in an
* exception (Error) being thrown!
* - This call should be awaited and the code block will be async.
*
* @example
* ```typescript
* await traceBlock({name: "myTraceBlock", attributes: { key: "value" }}) { aspan =>
* aspan.addAttributes({ additional: "attributes" });
* // do some async code here with awaits...
*
* aspan.addEvent("eventName", { "attr": "value" });
*
* // do some more code with awaits...
*
* aspan.addException(new Error("An error occurred"));
* aspan.setErrorStatus("An error occurred");
*
* // finish the code block
* })
* ```
*/
export async function traceBlockAsync(args: TraceArgs, block: (aspan: ASpan) => Promise<void>): Promise<void> {
if (!__tracing) {
console.warn("*** WARNING: Tracing not initialized. Call initializeTelemetry with 'tracing' signal type first.");
await block(__noopASpan); // Call the block with undefined to maintain API consistency
return
}
return await __tracing.traceBlockAsync(args, block); // Call the traceBlock method on the AnacondaTrace instance
}
/**
* Executes a block of code (async) within a tracing context, optionally attaching
* attributes and a carrier.
*
* @param args - An argument list object where the `name` field is required.
*
* The args is an object defined by (in any order):
*
* ```
* {
* name: string = ""; Required; Not supplying a name will result in no value being recorded.
* attributes?: AttrMap = {}; Optional; Attributes for the counter metric.
* carrier?: CarrierMap = {}; Optional; Used to create a context for the trace block.
* }
* ```
*
* @remarks
* - If tracing is not initialized, a warning is logged and the block is executed with a no-op span.
* - ___IMPORTANT___: Calling `reinitializeTelemetry` from within a traceBlock will result in an
* exception (Error) being thrown!
* - This call should be awaited and the code block will be async.
*
* @example
* ```typescript
* await traceBlock({name: "myTraceBlock", attributes: { key: "value" }}) { aspan =>
* aspan.addAttributes({ additional: "attributes" });
* // do some async code here with awaits...
*
* aspan.addEvent("eventName", { "attr": "value" });
*
* // do some more code with awaits...
*
* aspan.addException(new Error("An error occurred"));
* aspan.setErrorStatus("An error occurred");
*
* // finish the code block
* })
* ```
*/
export function traceBlock(args: TraceArgs, block: (aspan: ASpan) => void): void {
if (!__tracing) {
console.warn("*** WARNING: Tracing not initialized. Call initializeTelemetry with 'tracing' signal type first.");
block(__noopASpan); // Call the block with undefined to maintain API consistency
return
}
return __tracing.traceBlock(args, block); // Call the traceBlock method on the AnacondaTrace instance
}
|