Files
Zack Kanter 264f813cab feat(instrumentation): instrument cache lifecycle with OTEL (#41922)
* fix(cache): instrument repository cache load and save

Wrap repository cache `load()` and `save()` calls with the existing
`instrument()` helper to create OpenTelemetry spans for cache lifecycle
operations. This gives operators visibility into repository cache
performance, especially useful with S3 backends.

Refs #41760

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* fix(cache): instrument package cache init and destroy

Wrap package cache `init()` and `destroy()` with `instrument()` to
create OpenTelemetry spans, matching the repository cache instrumentation
from the previous commit. Covers redis, sqlite, and file backends.

Refs #41760

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* docs: document cache lifecycle tracing in OpenTelemetry guide

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* Move destroy() outside init instrument span

Avoids nesting the package-cache.destroy span inside
package-cache.init, making them sibling spans instead.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* Use 'verb NounClass' span naming convention

Rename instrument span names from dot-notation (e.g.
'package-cache.init') to 'verb NounClass' pattern (e.g.
'init PackageCache') per reviewer feedback.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-20 05:54:43 +00:00

69 lines
1.8 KiB
TypeScript

import type { AllConfig } from '../../../config/types.ts';
import { instrument } from '../../../instrumentation/index.ts';
import { getEnv } from '../../env.ts';
import type { PackageCacheBase } from './impl/base.ts';
import { PackageCacheFile } from './impl/file.ts';
import { PackageCacheRedis } from './impl/redis.ts';
import { PackageCacheSqlite } from './impl/sqlite.ts';
import type { PackageCacheNamespace } from './types.ts';
let cacheProxy: PackageCacheBase | undefined;
let cacheType: 'redis' | 'sqlite' | 'file' | undefined;
export function getCacheType(): typeof cacheType {
return cacheType;
}
export async function init(config: AllConfig): Promise<void> {
await destroy();
await instrument('init PackageCache', async () => {
if (config.redisUrl) {
cacheProxy = await PackageCacheRedis.create(
config.redisUrl,
config.redisPrefix,
);
cacheType = 'redis';
return;
}
if (getEnv().RENOVATE_X_SQLITE_PACKAGE_CACHE && config.cacheDir) {
cacheProxy = await PackageCacheSqlite.create(config.cacheDir);
cacheType = 'sqlite';
return;
}
if (config.cacheDir) {
cacheProxy = PackageCacheFile.create(config.cacheDir);
cacheType = 'file';
return;
}
});
}
export async function get<T = unknown>(
namespace: PackageCacheNamespace,
key: string,
): Promise<T | undefined> {
return await cacheProxy?.get<T>(namespace, key);
}
export async function set(
namespace: PackageCacheNamespace,
key: string,
value: unknown,
hardTtlMinutes: number,
): Promise<void> {
await cacheProxy?.set(namespace, key, value, hardTtlMinutes);
}
export async function destroy(): Promise<void> {
await instrument('destroy PackageCache', async () => {
cacheType = undefined;
try {
await cacheProxy?.destroy();
} finally {
cacheProxy = undefined;
}
});
}