All files / packages/telemetry/src metrics.ts

95.77% Statements 136/142
75% Branches 12/16
91.48% Functions 43/47
97.01% Lines 130/134

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 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584                                                                                                1044x                           34x 34x 34x 34x                                             34x 34x       34x 34x 34x 34x 34x 34x 34x       1020x 34x 34x 34x                 34x                             34x                         34x                                 34x                   34x                 34x             34x       34x                 34x             34x           34x       34x         34x             34x             34x             34x       34x             34x 20x 3x 3x       3x         20x 7x 7x 7x       7x       7x       7x 7x 7x     34x             34x   34x                       646x 646x 646x 646x   646x                     374x               374x 374x   1032x 1032x     1031x 1031x 1031x 1029x 1029x 1029x   1031x       2x 2x 2x 2x 2x 2x 2x         2x       374x 374x   374x           34x 238x 1021x                   34x       2x     2x     2x           34x       3x       34x       1x                 34x       4x                       34x   7x     34x       3x 3x       34x     1x     34x         34x               34x   1x 1x           34x     5x     34x         34x   4x     34x       3x 3x             34x   1x     34x         34x   3x     34x   1x     34x       1x                 34x       1x             34x   1x           34x   3x                 34x       2x                  
import { channel as plainChannel, tracingChannel } from 'node:diagnostics_channel'
 
import type {
	BatchObservableResult,
	Counter,
	Histogram,
	Meter,
	MetricAttributes,
	ObservableGauge,
	ObservableUpDownCounter,
} from '@opentelemetry/api'
import { ATTR_DB_OPERATION_NAME } from '@opentelemetry/semantic-conventions'
 
import type { DriverIdentity } from '@ydbjs/core'
 
import { LEAF_OPERATIONS } from './operations.js'
import { ConnectionPoolRegistry } from './state/connection-pool.js'
import { SessionPoolRegistry } from './state/session-pool.js'
import {
	ATTR_YDB_AUTH_PROVIDER,
	ATTR_YDB_CONNECTION_STATE,
	ATTR_YDB_IDEMPOTENT,
	ATTR_YDB_RETRY_OUTCOME,
	ATTR_YDB_SESSION_CLOSE_REASON,
	ATTR_YDB_SESSION_STATE,
	BASE_ATTRIBUTES,
	METRIC_DB_CLIENT_OPERATION_DURATION,
	METRIC_YDB_AUTH_TOKEN_EXPIRATIONS,
	METRIC_YDB_AUTH_TOKEN_FETCH_DURATION,
	METRIC_YDB_AUTH_TOKEN_FETCH_FAILURES,
	METRIC_YDB_AUTH_TOKEN_REFRESHES,
	METRIC_YDB_DRIVER_CONNECTION_COUNT,
	METRIC_YDB_DRIVER_CONNECTION_PESSIMIZATIONS,
	METRIC_YDB_QUERY_SESSION_ACQUIRE_DURATION,
	METRIC_YDB_QUERY_SESSION_ACQUIRE_FAILURES,
	METRIC_YDB_QUERY_SESSION_ACQUIRE_PENDING,
	METRIC_YDB_QUERY_SESSION_CLOSED,
	METRIC_YDB_QUERY_SESSION_COUNT,
	METRIC_YDB_QUERY_SESSION_CREATE_DURATION,
	METRIC_YDB_QUERY_SESSION_MAX,
	METRIC_YDB_QUERY_SESSION_MIN,
	METRIC_YDB_RETRY_ATTEMPTS,
	METRIC_YDB_RETRY_DURATION,
	identityAttrs,
	recordErrorAttributes,
} from './semconv/index.js'
 
function baseFor(driver: DriverIdentity | undefined): MetricAttributes {
	return { ...BASE_ATTRIBUTES, ...identityAttrs(driver) }
}
 
type DurationCtx = { driver?: DriverIdentity }
 
/**
 * Metrics pipeline. Owns OTel instruments and subscribes `diagnostics_channel`
 * events into them. State for observable instruments is split per domain —
 * connection-pool vs session-pool — and is rebuilt from channel events only,
 * never by reaching into pool internals. Late subscribers therefore miss the
 * initial state of an already-running driver.
 */
export class YdbMetricsPipeline {
	#meter: Meter
	#connectionState = new ConnectionPoolRegistry()
	#sessionState = new SessionPoolRegistry()
	#subs: Disposable[] = []
	#observableSubs: Disposable[] = []
 
	#dbClientOperationDuration!: Histogram
	#sessionCreateDuration!: Histogram
	#sessionAcquireDuration!: Histogram
	#authTokenFetchDuration!: Histogram
	#retryDuration!: Histogram
 
	#connectionPessimizations!: Counter
	#sessionClosed!: Counter
	#sessionAcquireFailures!: Counter
	#authTokenFetchFailures!: Counter
	#authTokenRefreshes!: Counter
	#authTokenExpirations!: Counter
	#retryAttempts!: Counter
 
	#connectionCount!: ObservableUpDownCounter
	#sessionCount!: ObservableUpDownCounter
	#sessionAcquirePending!: ObservableUpDownCounter
	#sessionMax!: ObservableGauge
	#sessionMin!: ObservableGauge
 
	constructor(meter: Meter) {
		this.#meter = meter
		this.#registerInstruments()
	}
 
	enable(): void {
		Iif (this.#subs.length > 0) return
		this.#subscribeLeafDurations()
		this.#subscribeConnectionEvents()
		this.#subscribeSessionEvents()
		this.#subscribeAuthEvents()
		this.#subscribeRetryEvents()
		this.#registerObservableCallbacks()
	}
 
	disable(): void {
		for (let s of this.#subs) s[Symbol.dispose]()
		this.#subs.length = 0
		for (let s of this.#observableSubs) s[Symbol.dispose]()
		this.#observableSubs.length = 0
	}
 
	#registerInstruments(): void {
		// Suggested bucket boundaries (seconds). Users can override via OTel
		// Views — these are just defaults so the out-of-the-box histograms are
		// usable. Without them the SDK falls back to its global ms-oriented
		// default ([0, 5, 10, 25, ..., 10000]) which silently buckets every YDB
		// op into the first slot.
		this.#dbClientOperationDuration = this.#meter.createHistogram(
			METRIC_DB_CLIENT_OPERATION_DURATION,
			{
				description: 'Duration of each client-side YDB operation attempt.',
				unit: 's',
				advice: {
					// Dense middle (1ms–1s) where the bulk of YDB ops live, tail
					// out to 60s for overload-backoff + slow queries.
					explicitBucketBoundaries: [
						0.0005, 0.001, 0.0025, 0.005, 0.0075, 0.01, 0.025, 0.05, 0.075, 0.1, 0.25,
						0.5, 0.75, 1, 2.5, 5, 7.5, 10, 30, 60,
					],
				},
			}
		)
		this.#sessionCreateDuration = this.#meter.createHistogram(
			METRIC_YDB_QUERY_SESSION_CREATE_DURATION,
			{
				description:
					'Time to create a query session (CreateSession + AttachStream first message).',
				unit: 's',
				advice: {
					explicitBucketBoundaries: [
						0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10,
					],
				},
			}
		)
		this.#sessionAcquireDuration = this.#meter.createHistogram(
			METRIC_YDB_QUERY_SESSION_ACQUIRE_DURATION,
			{
				description: 'Time to acquire a session lease from the pool.',
				unit: 's',
				advice: {
					// Warm pool resolves in microseconds — keep sub-ms granularity at
					// the low end. When the pool has capacity but no idle session,
					// acquire encapsulates a full `session.create`, so the upper
					// boundaries must cover create's tail (10s) and then some — 30s
					// is reserved for genuine pool starvation under waiter timeout.
					explicitBucketBoundaries: [
						0.0001, 0.001, 0.005, 0.01, 0.05, 0.1, 0.5, 1, 2.5, 5, 10, 30,
					],
				},
			}
		)
		this.#authTokenFetchDuration = this.#meter.createHistogram(
			METRIC_YDB_AUTH_TOKEN_FETCH_DURATION,
			{
				description: 'Duration of a token fetch / refresh.',
				unit: 's',
				advice: {
					explicitBucketBoundaries: [0.01, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10, 30],
				},
			}
		)
		this.#retryDuration = this.#meter.createHistogram(METRIC_YDB_RETRY_DURATION, {
			description: 'End-to-end duration of a retry loop including backoffs.',
			unit: 's',
			advice: {
				// Long tail — overload backoff can push a loop into minutes.
				explicitBucketBoundaries: [0.01, 0.05, 0.1, 0.5, 1, 5, 10, 30, 60, 300],
			},
		})
 
		this.#connectionPessimizations = this.#meter.createCounter(
			METRIC_YDB_DRIVER_CONNECTION_PESSIMIZATIONS,
			{
				description: 'Count of connection pessimization events.',
				unit: '{event}',
			}
		)
		this.#sessionClosed = this.#meter.createCounter(METRIC_YDB_QUERY_SESSION_CLOSED, {
			description: 'Count of session removals, tagged by close reason.',
			unit: '{session}',
		})
		this.#sessionAcquireFailures = this.#meter.createCounter(
			METRIC_YDB_QUERY_SESSION_ACQUIRE_FAILURES,
			{
				description:
					'Failed session acquires (pool full / pool-internal timeout / pool closed), tagged by error.type. ' +
					'Caller-aborted acquires are not counted as failures — see ydb:query.session.acquire.failed.',
				unit: '{failure}',
			}
		)
		this.#authTokenFetchFailures = this.#meter.createCounter(
			METRIC_YDB_AUTH_TOKEN_FETCH_FAILURES,
			{
				description: 'Failed token fetch / refresh attempts.',
				unit: '{failure}',
			}
		)
		this.#authTokenRefreshes = this.#meter.createCounter(METRIC_YDB_AUTH_TOKEN_REFRESHES, {
			description:
				'Successful token refreshes. Direct rate signal; complements ' +
				'ydb.auth.token.fetch.duration which records both successes and failures.',
			unit: '{refresh}',
		})
		this.#authTokenExpirations = this.#meter.createCounter(METRIC_YDB_AUTH_TOKEN_EXPIRATIONS, {
			description: 'Incidents where a stale (past hard expiry buffer) token was served.',
			unit: '{expiration}',
		})
		this.#retryAttempts = this.#meter.createCounter(METRIC_YDB_RETRY_ATTEMPTS, {
			description: 'Count of retry attempts tagged with outcome.',
			unit: '{attempt}',
		})
 
		this.#connectionCount = this.#meter.createObservableUpDownCounter(
			METRIC_YDB_DRIVER_CONNECTION_COUNT,
			{
				description: 'Current count of pooled gRPC connections, by state.',
				unit: '{connection}',
			}
		)
		this.#sessionCount = this.#meter.createObservableUpDownCounter(
			METRIC_YDB_QUERY_SESSION_COUNT,
			{
				description: 'Current count of live query sessions in the pool, by state.',
				unit: '{session}',
			}
		)
		this.#sessionAcquirePending = this.#meter.createObservableUpDownCounter(
			METRIC_YDB_QUERY_SESSION_ACQUIRE_PENDING,
			{
				description: 'Callers currently waiting for a session lease.',
				unit: '{request}',
			}
		)
		this.#sessionMax = this.#meter.createObservableGauge(METRIC_YDB_QUERY_SESSION_MAX, {
			description: 'Configured maxSize of the session pool.',
			unit: '{session}',
		})
		this.#sessionMin = this.#meter.createObservableGauge(METRIC_YDB_QUERY_SESSION_MIN, {
			description: 'Configured minSize of the session pool.',
			unit: '{session}',
		})
	}
 
	#registerObservableCallbacks(): void {
		let cb = (observable: BatchObservableResult) => {
			for (let [driver, state] of this.#connectionState.connections()) {
				let base = baseFor(driver)
				observable.observe(this.#connectionCount, state.live, {
					...base,
					[ATTR_YDB_CONNECTION_STATE]: 'live',
				})
				observable.observe(this.#connectionCount, state.pessimized, {
					...base,
					[ATTR_YDB_CONNECTION_STATE]: 'pessimized',
				})
			}
			for (let [driver, state] of this.#sessionState.sessions()) {
				let base = baseFor(driver)
				let idle = Math.max(0, state.total - state.acquired)
				observable.observe(this.#sessionCount, idle, {
					...base,
					[ATTR_YDB_SESSION_STATE]: 'idle',
				})
				observable.observe(this.#sessionCount, state.acquired, {
					...base,
					[ATTR_YDB_SESSION_STATE]: 'acquired',
				})
				observable.observe(this.#sessionCount, state.creating, {
					...base,
					[ATTR_YDB_SESSION_STATE]: 'creating',
				})
				observable.observe(this.#sessionAcquirePending, state.waiters, base)
				observable.observe(this.#sessionMax, state.maxSize, base)
				observable.observe(this.#sessionMin, state.minSize, base)
			}
		}
		this.#meter.addBatchObservableCallback(cb, [
			this.#connectionCount,
			this.#sessionCount,
			this.#sessionAcquirePending,
			this.#sessionMax,
			this.#sessionMin,
		])
		this.#observableSubs.push({
			[Symbol.dispose]: () => {
				this.#meter.removeBatchObservableCallback(cb, [
					this.#connectionCount,
					this.#sessionCount,
					this.#sessionAcquirePending,
					this.#sessionMax,
					this.#sessionMin,
				])
			},
		})
	}
 
	#subPlain<T>(name: string, fn: (msg: T) => void): Disposable {
		let ch = plainChannel(name)
		let handler = (msg: unknown) => fn(msg as T)
		ch.subscribe(handler)
		return {
			[Symbol.dispose]() {
				ch.unsubscribe(handler)
			},
		}
	}
 
	#subDuration(
		channelName: string,
		instrument: Histogram,
		makeAttrs: (ctx: DurationCtx) => MetricAttributes,
		hooks?: { onStart?: (ctx: DurationCtx) => void; onEnd?: (ctx: DurationCtx) => void }
	): Disposable {
		let ch = tracingChannel<DurationCtx, DurationCtx>(channelName)
		// Per-subscription WeakMap, not a pipeline-wide one. The same channel
		// can be subscribed twice (e.g. session.create feeds both the generic
		// `db.client.operation.duration` and the specific
		// `ydb.query.session.create.duration` instruments). Both subscriptions
		// see the same ctx in `start` and `asyncEnd`; a shared map would let
		// whichever handler runs first delete the entry before the other reads
		// it, and the second instrument would silently drop the recording.
		let starts = new WeakMap<object, number>()
		let handlers = {
			start: (ctx: DurationCtx) => {
				starts.set(ctx, performance.now())
				hooks?.onStart?.(ctx)
			},
			asyncEnd: (ctx: DurationCtx) => {
				try {
					let started = starts.get(ctx)
					if (started === undefined) return
					starts.delete(ctx)
					let durationMs = performance.now() - started
					instrument.record(durationMs / 1000, makeAttrs(ctx))
				} finally {
					hooks?.onEnd?.(ctx)
				}
			},
			error: (ctx: DurationCtx & { error?: unknown }) => {
				try {
					let started = starts.get(ctx)
					Iif (started === undefined) return
					starts.delete(ctx)
					let durationMs = performance.now() - started
					let attrs = makeAttrs(ctx)
					instrument.record(durationMs / 1000, {
						...attrs,
						...recordErrorAttributes(ctx.error),
					})
				} finally {
					hooks?.onEnd?.(ctx)
				}
			},
		}
		ch.subscribe(handlers as Parameters<typeof ch.subscribe>[0])
		return {
			[Symbol.dispose]() {
				ch.unsubscribe(handlers as Parameters<typeof ch.unsubscribe>[0])
			},
		}
	}
 
	#subscribeLeafDurations(): void {
		for (let { channel, operation } of LEAF_OPERATIONS) {
			this.#subs.push(
				this.#subDuration(channel, this.#dbClientOperationDuration, (ctx) => ({
					...baseFor(ctx.driver),
					[ATTR_DB_OPERATION_NAME]: operation,
				}))
			)
		}
 
		// `session.create.duration` shares its source channel with the generic
		// `db.client.operation.duration` so dashboards can pivot on either
		// without filtering. The hooks also drive the `creating` observable.
		this.#subs.push(
			this.#subDuration(
				'tracing:ydb:query.session.create',
				this.#sessionCreateDuration,
				(ctx) => baseFor(ctx.driver),
				{
					onStart: (ctx) => {
						if (ctx.driver) this.#sessionState.createStarted(ctx.driver)
					},
					onEnd: (ctx) => {
						if (ctx.driver) this.#sessionState.createEnded(ctx.driver)
					},
				}
			)
		)
 
		this.#subs.push(
			this.#subDuration(
				'tracing:ydb:query.session.acquire',
				this.#sessionAcquireDuration,
				(ctx) => baseFor(ctx.driver)
			)
		)
 
		this.#subs.push(
			this.#subDuration(
				'tracing:ydb:auth.token.fetch',
				this.#authTokenFetchDuration,
				(ctx: DurationCtx & { provider?: string }) => ({
					...baseFor(ctx.driver),
					...(ctx.provider !== undefined
						? { [ATTR_YDB_AUTH_PROVIDER]: ctx.provider }
						: {}),
				})
			)
		)
 
		this.#subs.push(
			this.#subDuration(
				'tracing:ydb:retry.run',
				this.#retryDuration,
				(ctx: DurationCtx & { idempotent?: boolean; outcome?: string }) => ({
					...BASE_ATTRIBUTES,
					...(ctx.idempotent !== undefined
						? { [ATTR_YDB_IDEMPOTENT]: ctx.idempotent }
						: {}),
					...(ctx.outcome !== undefined ? { [ATTR_YDB_RETRY_OUTCOME]: ctx.outcome } : {}),
				})
			)
		)
	}
 
	#subscribeConnectionEvents(): void {
		this.#subs.push(
			this.#subPlain<{ driver: DriverIdentity }>('ydb:driver.connection.added', (msg) =>
				this.#connectionState.connectionAdded(msg.driver)
			)
		)
		this.#subs.push(
			this.#subPlain<{ driver: DriverIdentity }>(
				'ydb:driver.connection.pessimized',
				(msg) => {
					this.#connectionState.connectionPessimized(msg.driver)
					this.#connectionPessimizations.add(1, baseFor(msg.driver))
				}
			)
		)
		this.#subs.push(
			this.#subPlain<{ driver: DriverIdentity }>(
				'ydb:driver.connection.unpessimized',
				(msg) => this.#connectionState.connectionUnpessimized(msg.driver)
			)
		)
		this.#subs.push(
			this.#subPlain<{ driver: DriverIdentity }>('ydb:driver.connection.retired', (msg) =>
				this.#connectionState.connectionRemoved(msg.driver)
			)
		)
		this.#subs.push(
			this.#subPlain<{ driver: DriverIdentity }>('ydb:driver.connection.removed', (msg) =>
				this.#connectionState.connectionRemoved(msg.driver)
			)
		)
		// Driver close is the safety net for the session registry: if the
		// caller skipped `pool.close()` and let the driver dispose first, this
		// keeps state from leaking.
		this.#subs.push(
			this.#subPlain<{ driver: DriverIdentity }>('ydb:driver.closed', (msg) => {
				this.#connectionState.driverClosed(msg.driver)
				this.#sessionState.driverClosed(msg.driver)
			})
		)
	}
 
	#subscribeSessionEvents(): void {
		this.#subs.push(
			this.#subPlain<{ driver: DriverIdentity; maxSize: number; minSize: number }>(
				'ydb:query.session.pool.opened',
				(msg) => this.#sessionState.poolOpened(msg.driver, msg.maxSize, msg.minSize)
			)
		)
		this.#subs.push(
			this.#subPlain<{ driver: DriverIdentity }>('ydb:query.session.pool.closed', (msg) =>
				this.#sessionState.poolClosed(msg.driver)
			)
		)
		this.#subs.push(
			this.#subPlain<{ driver: DriverIdentity }>('ydb:query.session.created', (msg) =>
				this.#sessionState.created(msg.driver)
			)
		)
		this.#subs.push(
			this.#subPlain<{ driver: DriverIdentity; reason: string }>(
				'ydb:query.session.closed',
				(msg) => {
					this.#sessionState.closed(msg.driver)
					this.#sessionClosed.add(1, {
						...baseFor(msg.driver),
						[ATTR_YDB_SESSION_CLOSE_REASON]: msg.reason,
					})
				}
			)
		)
		this.#subs.push(
			this.#subPlain<{ driver: DriverIdentity }>('ydb:query.session.acquired', (msg) =>
				this.#sessionState.acquired(msg.driver)
			)
		)
		this.#subs.push(
			this.#subPlain<{ driver: DriverIdentity }>('ydb:query.session.released', (msg) =>
				this.#sessionState.released(msg.driver)
			)
		)
		this.#subs.push(
			this.#subPlain<{ driver: DriverIdentity }>('ydb:query.session.waiter.enqueued', (msg) =>
				this.#sessionState.waiterEnqueued(msg.driver)
			)
		)
		this.#subs.push(
			this.#subPlain<{ driver: DriverIdentity }>('ydb:query.session.waiter.dequeued', (msg) =>
				this.#sessionState.waiterDequeued(msg.driver)
			)
		)
		this.#subs.push(
			this.#subPlain<{ driver: DriverIdentity; error: unknown }>(
				'ydb:query.session.acquire.failed',
				(msg) =>
					this.#sessionAcquireFailures.add(1, {
						...baseFor(msg.driver),
						...recordErrorAttributes(msg.error),
					})
			)
		)
	}
 
	#subscribeAuthEvents(): void {
		this.#subs.push(
			this.#subPlain<{ provider: string; error: unknown }>(
				'ydb:auth.provider.failed',
				(msg) =>
					this.#authTokenFetchFailures.add(1, {
						...BASE_ATTRIBUTES,
						[ATTR_YDB_AUTH_PROVIDER]: msg.provider,
						...recordErrorAttributes(msg.error),
					})
			)
		)
		this.#subs.push(
			this.#subPlain<{ provider: string }>('ydb:auth.token.expired', (msg) =>
				this.#authTokenExpirations.add(1, {
					...BASE_ATTRIBUTES,
					[ATTR_YDB_AUTH_PROVIDER]: msg.provider,
				})
			)
		)
		this.#subs.push(
			this.#subPlain<{ provider: string }>('ydb:auth.token.refreshed', (msg) =>
				this.#authTokenRefreshes.add(1, {
					...BASE_ATTRIBUTES,
					[ATTR_YDB_AUTH_PROVIDER]: msg.provider,
				})
			)
		)
	}
 
	#subscribeRetryEvents(): void {
		this.#subs.push(
			this.#subPlain<{ attempt: number; idempotent: boolean; outcome: string }>(
				'ydb:retry.attempt.completed',
				(msg) =>
					this.#retryAttempts.add(1, {
						...BASE_ATTRIBUTES,
						[ATTR_YDB_IDEMPOTENT]: msg.idempotent,
						[ATTR_YDB_RETRY_OUTCOME]: msg.outcome,
					})
			)
		)
	}
}