telemetry.d.ts 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. import { SpanKind } from '../enums';
  2. /**
  3. * Telemetry interface
  4. *
  5. * This interface allows third-party libraries to integrate their own telemetry
  6. * system. The interface is heavily inspired by OpenTelemetry but it's not
  7. * limited to it.
  8. *
  9. */
  10. export interface Telemetry<Context = any> {
  11. /**
  12. * Tracer instance
  13. *
  14. * The tracer is responsible for creating spans and propagating the context
  15. * across the application.
  16. */
  17. tracer: Tracer<Context>;
  18. /**
  19. * Context manager instance
  20. *
  21. * The context manager is responsible for managing the context and propagating
  22. * it across the application.
  23. */
  24. contextManager: ContextManager;
  25. /**
  26. * Meter instance (optional)
  27. *
  28. * The meter is responsible for creating and managing metrics instruments
  29. * such as counters, histograms, etc.
  30. */
  31. meter?: Meter;
  32. }
  33. /**
  34. * Meter interface
  35. *
  36. * The meter is responsible for creating metric instruments.
  37. */
  38. export interface Meter {
  39. /**
  40. * Creates a new Counter metric instrument.
  41. *
  42. * @param name - the name of the counter
  43. * @param options - optional configuration for the counter
  44. * @returns a Counter instance
  45. */
  46. createCounter(name: string, options?: MetricOptions): Counter;
  47. /**
  48. * Creates a new Histogram metric instrument.
  49. *
  50. * @param name - the name of the histogram
  51. * @param options - optional configuration for the histogram
  52. * @returns a Histogram instance
  53. */
  54. createHistogram(name: string, options?: MetricOptions): Histogram;
  55. /**
  56. * Creates a new Gauge metric instrument.
  57. *
  58. * A gauge is a metric that represents a single numerical value that can
  59. * arbitrarily go up and down. Gauges are typically used for measured values
  60. * like queue sizes.
  61. *
  62. * @param name - the name of the gauge
  63. * @param options - optional configuration for the gauge
  64. * @returns a Gauge instance
  65. */
  66. createGauge?(name: string, options?: MetricOptions): Gauge;
  67. }
  68. /**
  69. * Options for creating metric instruments
  70. */
  71. export interface MetricOptions {
  72. /**
  73. * Human-readable description of the metric
  74. */
  75. description?: string;
  76. /**
  77. * Unit of measurement for the metric (e.g., 'ms', 'bytes', '1')
  78. */
  79. unit?: string;
  80. }
  81. /**
  82. * Counter metric interface
  83. *
  84. * A counter is a cumulative metric that represents a single monotonically
  85. * increasing value. Counters are typically used to count requests, completed
  86. * tasks, errors, etc.
  87. */
  88. export interface Counter {
  89. /**
  90. * Adds a value to the counter.
  91. *
  92. * @param value - the value to add (must be non-negative)
  93. * @param attributes - optional attributes to associate with this measurement
  94. */
  95. add(value: number, attributes?: Attributes): void;
  96. }
  97. /**
  98. * Histogram metric interface
  99. *
  100. * A histogram is a metric that samples observations and counts them in
  101. * configurable buckets. Typically used for measuring durations or sizes.
  102. */
  103. export interface Histogram {
  104. /**
  105. * Records a value in the histogram.
  106. *
  107. * @param value - the value to record
  108. * @param attributes - optional attributes to associate with this measurement
  109. */
  110. record(value: number, attributes?: Attributes): void;
  111. }
  112. /**
  113. * Gauge metric interface
  114. *
  115. * A gauge is a synchronous instrument which can be used to record
  116. * non-additive value(s) (e.g. the current queue size) when changes occur.
  117. */
  118. export interface Gauge {
  119. /**
  120. * Records a value for the gauge.
  121. *
  122. * @param value - the value to record
  123. * @param attributes - optional attributes to associate with this measurement
  124. */
  125. record(value: number, attributes?: Attributes): void;
  126. }
  127. /**
  128. * Context manager interface
  129. *
  130. * The context manager is responsible for managing the context and propagating
  131. * it across the application.
  132. */
  133. export interface ContextManager<Context = any> {
  134. /**
  135. * Creates a new context and sets it as active for the fn passed as last argument
  136. *
  137. * @param context - the context to set as active
  138. * @param fn - the function to execute with the context
  139. */
  140. with<A extends (...args: any[]) => any>(context: Context, fn: A): ReturnType<A>;
  141. /**
  142. * Returns the active context
  143. */
  144. active(): Context;
  145. /**
  146. * Returns a serialized version of the current context. The metadata
  147. * is the mechanism used to propagate the context across a distributed
  148. * application.
  149. *
  150. * @param context - the current context
  151. */
  152. getMetadata(context: Context): string;
  153. /**
  154. * Creates a new context from a serialized version effectively
  155. * linking the new context to the parent context.
  156. *
  157. * @param activeContext - the current active context
  158. * @param metadata - the serialized version of the context
  159. */
  160. fromMetadata(activeContext: Context, metadata: string): Context;
  161. }
  162. /**
  163. * Tracer interface
  164. *
  165. */
  166. export interface Tracer<Context = any> {
  167. /**
  168. * startSpan creates a new Span with the given name and options on an optional
  169. * context. If the context is not provided, the current active context should be
  170. * used.
  171. *
  172. * @param name - span name
  173. * @param options - span options
  174. * @param context - optional context
  175. * @returns - the created span
  176. */
  177. startSpan(name: string, options?: SpanOptions, context?: Context): Span;
  178. }
  179. export interface SpanOptions {
  180. kind: SpanKind;
  181. }
  182. /**
  183. * Span interface
  184. */
  185. export interface Span<Context = any> {
  186. /**
  187. * setSpanOnContext sets the span on the context. This is useful when you want
  188. * to propagate the span across the application.
  189. *
  190. * @param ctx - context to set the span on
  191. * @returns - the context with the span set on it
  192. */
  193. setSpanOnContext(ctx: Context): Context;
  194. /**
  195. * setAttribute sets an attribute on the span.
  196. *
  197. * @param key - attribute key
  198. * @param value - attribute value
  199. */
  200. setAttribute(key: string, value: AttributeValue): void;
  201. /**
  202. * setAttributes sets multiple attributes on the span.
  203. *
  204. * @param attributes - attributes to set
  205. */
  206. setAttributes(attributes: Attributes): void;
  207. /**
  208. * addEvent adds an event to the span.
  209. *
  210. * @param name - event name
  211. * @param attributes - event attributes
  212. */
  213. addEvent(name: string, attributes?: Attributes): void;
  214. /**
  215. * recordException records an exception on the span.
  216. *
  217. * @param exception - exception to record
  218. * @param time - time to record the exception
  219. */
  220. recordException(exception: Exception, time?: Time): void;
  221. /**
  222. * end ends the span.
  223. *
  224. * Note: spans must be ended so that they can be exported.
  225. */
  226. end(): void;
  227. }
  228. export interface Attributes {
  229. [attribute: string]: AttributeValue | undefined;
  230. }
  231. export type AttributeValue = string | number | boolean | Array<null | undefined | string> | Array<null | undefined | number> | Array<null | undefined | boolean>;
  232. export type Exception = string | ExceptionType;
  233. export type ExceptionType = CodeException | MessageException | NameException;
  234. interface CodeException {
  235. code: string | number;
  236. name?: string;
  237. message?: string;
  238. stack?: string;
  239. }
  240. interface MessageException {
  241. code?: string | number;
  242. name?: string;
  243. message: string;
  244. stack?: string;
  245. }
  246. interface NameException {
  247. code?: string | number;
  248. name: string;
  249. message?: string;
  250. stack?: string;
  251. }
  252. export type Time = HighResolutionTime | number | Date;
  253. type HighResolutionTime = [number, number];
  254. export {};