| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- import { SpanKind } from '../enums';
- /**
- * Telemetry interface
- *
- * This interface allows third-party libraries to integrate their own telemetry
- * system. The interface is heavily inspired by OpenTelemetry but it's not
- * limited to it.
- *
- */
- export interface Telemetry<Context = any> {
- /**
- * Tracer instance
- *
- * The tracer is responsible for creating spans and propagating the context
- * across the application.
- */
- tracer: Tracer<Context>;
- /**
- * Context manager instance
- *
- * The context manager is responsible for managing the context and propagating
- * it across the application.
- */
- contextManager: ContextManager;
- /**
- * Meter instance (optional)
- *
- * The meter is responsible for creating and managing metrics instruments
- * such as counters, histograms, etc.
- */
- meter?: Meter;
- }
- /**
- * Meter interface
- *
- * The meter is responsible for creating metric instruments.
- */
- export interface Meter {
- /**
- * Creates a new Counter metric instrument.
- *
- * @param name - the name of the counter
- * @param options - optional configuration for the counter
- * @returns a Counter instance
- */
- createCounter(name: string, options?: MetricOptions): Counter;
- /**
- * Creates a new Histogram metric instrument.
- *
- * @param name - the name of the histogram
- * @param options - optional configuration for the histogram
- * @returns a Histogram instance
- */
- createHistogram(name: string, options?: MetricOptions): Histogram;
- /**
- * Creates a new Gauge metric instrument.
- *
- * A gauge is a metric that represents a single numerical value that can
- * arbitrarily go up and down. Gauges are typically used for measured values
- * like queue sizes.
- *
- * @param name - the name of the gauge
- * @param options - optional configuration for the gauge
- * @returns a Gauge instance
- */
- createGauge?(name: string, options?: MetricOptions): Gauge;
- }
- /**
- * Options for creating metric instruments
- */
- export interface MetricOptions {
- /**
- * Human-readable description of the metric
- */
- description?: string;
- /**
- * Unit of measurement for the metric (e.g., 'ms', 'bytes', '1')
- */
- unit?: string;
- }
- /**
- * Counter metric interface
- *
- * A counter is a cumulative metric that represents a single monotonically
- * increasing value. Counters are typically used to count requests, completed
- * tasks, errors, etc.
- */
- export interface Counter {
- /**
- * Adds a value to the counter.
- *
- * @param value - the value to add (must be non-negative)
- * @param attributes - optional attributes to associate with this measurement
- */
- add(value: number, attributes?: Attributes): void;
- }
- /**
- * Histogram metric interface
- *
- * A histogram is a metric that samples observations and counts them in
- * configurable buckets. Typically used for measuring durations or sizes.
- */
- export interface Histogram {
- /**
- * Records a value in the histogram.
- *
- * @param value - the value to record
- * @param attributes - optional attributes to associate with this measurement
- */
- record(value: number, attributes?: Attributes): void;
- }
- /**
- * Gauge metric interface
- *
- * A gauge is a synchronous instrument which can be used to record
- * non-additive value(s) (e.g. the current queue size) when changes occur.
- */
- export interface Gauge {
- /**
- * Records a value for the gauge.
- *
- * @param value - the value to record
- * @param attributes - optional attributes to associate with this measurement
- */
- record(value: number, attributes?: Attributes): void;
- }
- /**
- * Context manager interface
- *
- * The context manager is responsible for managing the context and propagating
- * it across the application.
- */
- export interface ContextManager<Context = any> {
- /**
- * Creates a new context and sets it as active for the fn passed as last argument
- *
- * @param context - the context to set as active
- * @param fn - the function to execute with the context
- */
- with<A extends (...args: any[]) => any>(context: Context, fn: A): ReturnType<A>;
- /**
- * Returns the active context
- */
- active(): Context;
- /**
- * Returns a serialized version of the current context. The metadata
- * is the mechanism used to propagate the context across a distributed
- * application.
- *
- * @param context - the current context
- */
- getMetadata(context: Context): string;
- /**
- * Creates a new context from a serialized version effectively
- * linking the new context to the parent context.
- *
- * @param activeContext - the current active context
- * @param metadata - the serialized version of the context
- */
- fromMetadata(activeContext: Context, metadata: string): Context;
- }
- /**
- * Tracer interface
- *
- */
- export interface Tracer<Context = any> {
- /**
- * startSpan creates a new Span with the given name and options on an optional
- * context. If the context is not provided, the current active context should be
- * used.
- *
- * @param name - span name
- * @param options - span options
- * @param context - optional context
- * @returns - the created span
- */
- startSpan(name: string, options?: SpanOptions, context?: Context): Span;
- }
- export interface SpanOptions {
- kind: SpanKind;
- }
- /**
- * Span interface
- */
- export interface Span<Context = any> {
- /**
- * setSpanOnContext sets the span on the context. This is useful when you want
- * to propagate the span across the application.
- *
- * @param ctx - context to set the span on
- * @returns - the context with the span set on it
- */
- setSpanOnContext(ctx: Context): Context;
- /**
- * setAttribute sets an attribute on the span.
- *
- * @param key - attribute key
- * @param value - attribute value
- */
- setAttribute(key: string, value: AttributeValue): void;
- /**
- * setAttributes sets multiple attributes on the span.
- *
- * @param attributes - attributes to set
- */
- setAttributes(attributes: Attributes): void;
- /**
- * addEvent adds an event to the span.
- *
- * @param name - event name
- * @param attributes - event attributes
- */
- addEvent(name: string, attributes?: Attributes): void;
- /**
- * recordException records an exception on the span.
- *
- * @param exception - exception to record
- * @param time - time to record the exception
- */
- recordException(exception: Exception, time?: Time): void;
- /**
- * end ends the span.
- *
- * Note: spans must be ended so that they can be exported.
- */
- end(): void;
- }
- export interface Attributes {
- [attribute: string]: AttributeValue | undefined;
- }
- export type AttributeValue = string | number | boolean | Array<null | undefined | string> | Array<null | undefined | number> | Array<null | undefined | boolean>;
- export type Exception = string | ExceptionType;
- export type ExceptionType = CodeException | MessageException | NameException;
- interface CodeException {
- code: string | number;
- name?: string;
- message?: string;
- stack?: string;
- }
- interface MessageException {
- code?: string | number;
- name?: string;
- message: string;
- stack?: string;
- }
- interface NameException {
- code?: string | number;
- name: string;
- message?: string;
- stack?: string;
- }
- export type Time = HighResolutionTime | number | Date;
- type HighResolutionTime = [number, number];
- export {};
|