Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

feat(telemetry): enhance secret cache metrics with ETag handling and user agent improvements

Victor Hugo dos Santos895d2f787b39
diff --git a/backend/src/lib/telemetry/metrics.ts b/backend/src/lib/telemetry/metrics.ts
index 3526c78b0a..53c6e4cbd6 100644
--- a/backend/src/lib/telemetry/metrics.ts
+++ b/backend/src/lib/telemetry/metrics.ts
@@ -328,8 +328,14 @@ export enum SecretCacheAccessResult {
MISS = "miss"
}
+// Why an If-None-Match request did not 304.
+export enum SecretEtagMissReason {
+ FIELD_ABSENT = "field_absent", // no stored ETag field: churned or expired
+ VALUE_DIFFERS = "value_differs" // field exists, content hash differs: secrets changed
+}
+
export const secretCacheAccessCounter = infisicalCoreMeter.createCounter("infisical.secret.cache.access.count", {
- description: "Secret service-layer cache accesses by outcome (304 not-modified / hit / miss)",
+ description: "Secret cache accesses, labeled by result, whether If-None-Match was sent, and why it missed the 304.",
unit: "{access}"
});
@@ -346,9 +352,15 @@ export const secretCacheOversizeSkipCounter = infisicalCoreMeter.createCounter(
}
);
-export const recordSecretCacheAccessMetric = (result: SecretCacheAccessResult) => {
+export const recordSecretCacheAccessMetric = (
+ result: SecretCacheAccessResult,
+ opts?: { hasIfNoneMatch?: boolean; etagMissReason?: SecretEtagMissReason }
+) => {
if (!isTelemetryEnabled()) return;
- secretCacheAccessCounter.add(1, { "cache.result": result });
+ const attributes: Record<string, string> = { "cache.result": result };
+ if (opts?.hasIfNoneMatch !== undefined) attributes["cache.if_none_match"] = opts.hasIfNoneMatch ? "true" : "false";
+ if (opts?.etagMissReason) attributes["cache.etag_miss_reason"] = opts.etagMissReason;
+ secretCacheAccessCounter.add(1, attributes);
};
export const recordSecretCacheWriteMetric = (params: { bytes: number; stored: boolean }) => {
diff --git a/backend/src/lib/telemetry/telemetry-attributes.ts b/backend/src/lib/telemetry/telemetry-attributes.ts
index e96f0ee210..806276dd71 100644
--- a/backend/src/lib/telemetry/telemetry-attributes.ts
+++ b/backend/src/lib/telemetry/telemetry-attributes.ts
@@ -26,6 +26,8 @@ export const INFISICAL_CORE_METER_ATTRIBUTES = [
"sso.action",
"db.pool.state",
"cache.result",
+ "cache.if_none_match",
+ "cache.etag_miss_reason",
"rate_limit.bucket",
"provider",
"destination",
diff --git a/backend/src/server/plugins/audit-log.ts b/backend/src/server/plugins/audit-log.ts
index f7d478e8fd..31ec61d82e 100644
--- a/backend/src/server/plugins/audit-log.ts
+++ b/backend/src/server/plugins/audit-log.ts
@@ -13,7 +13,8 @@ export const getUserAgentType = (userAgent: string | undefined) => {
if (userAgent === UserAgentType.CLI) {
return UserAgentType.CLI;
}
- if (userAgent === UserAgentType.K8_OPERATOR) {
+ // also match the versioned UA, e.g. "k8-operator/0.11.4"
+ if (userAgent === UserAgentType.K8_OPERATOR || userAgent.startsWith(`${UserAgentType.K8_OPERATOR}/`)) {
return UserAgentType.K8_OPERATOR;
}
if (userAgent === UserAgentType.TERRAFORM) {
diff --git a/backend/src/server/routes/v3/deprecated-secret-router.ts b/backend/src/server/routes/v3/deprecated-secret-router.ts
index ea7a13d37f..8e888ec6f2 100644
--- a/backend/src/server/routes/v3/deprecated-secret-router.ts
+++ b/backend/src/server/routes/v3/deprecated-secret-router.ts
@@ -1069,7 +1069,8 @@ export const registerDeprecatedSecretRouter = async (server: FastifyZodProvider)
// }
const shouldCapture =
- req.query.workspaceId !== "650e71fbae3e6c8572f436d4" && req.headers["user-agent"] !== "k8-operator";
+ req.query.workspaceId !== "650e71fbae3e6c8572f436d4" &&
+ getUserAgentType(req.headers["user-agent"]) !== UserAgentType.K8_OPERATOR;
if (shouldCapture) {
await server.services.telemetry.sendPostHogEvents({
event: PostHogEventTypes.SecretPulled,
diff --git a/backend/src/services/secret-v2-bridge/secret-v2-bridge-service.ts b/backend/src/services/secret-v2-bridge/secret-v2-bridge-service.ts
index d8d7beeea7..168f7618d8 100644
--- a/backend/src/services/secret-v2-bridge/secret-v2-bridge-service.ts
+++ b/backend/src/services/secret-v2-bridge/secret-v2-bridge-service.ts
@@ -45,7 +45,8 @@ import {
recordSecretOperationDuration,
recordSecretReadMetric,
recordSecretWriteMetric,
- SecretCacheAccessResult
+ SecretCacheAccessResult,
+ SecretEtagMissReason
} from "@app/lib/telemetry/metrics";
import { ActorType } from "../auth/auth-type";
@@ -1292,12 +1293,16 @@ export const secretV2BridgeServiceFactory = ({
});
const etagField = `${actorId}:${permissionFingerprint}:${requestParamsHash}`;
+ const hasIfNoneMatch = Boolean(ifNoneMatch);
+ let etagMissReason: SecretEtagMissReason | undefined;
+
if (ifNoneMatch) {
const storedEtag = await keyStore.hashGet(etagRedisKey, etagField);
if (storedEtag && storedEtag === ifNoneMatch) {
- recordSecretCacheAccessMetric(SecretCacheAccessResult.NOT_MODIFIED);
+ recordSecretCacheAccessMetric(SecretCacheAccessResult.NOT_MODIFIED, { hasIfNoneMatch: true });
return { notModified: true, etag: ifNoneMatch, secrets: [], imports: [] };
}
+ etagMissReason = storedEtag ? SecretEtagMissReason.VALUE_DIFFERS : SecretEtagMissReason.FIELD_ABSENT;
}
const { permission } = await permissionService.getProjectPermission({
@@ -1357,7 +1362,7 @@ export const secretV2BridgeServiceFactory = ({
const payload = { secrets, imports };
await keyStore.hashSet(etagRedisKey, etagField, cachedEtag);
await keyStore.setExpiry(etagRedisKey, KeyStoreTtls.SecretEtagInSeconds);
- recordSecretCacheAccessMetric(SecretCacheAccessResult.HIT);
+ recordSecretCacheAccessMetric(SecretCacheAccessResult.HIT, { hasIfNoneMatch, etagMissReason });
return { ...payload, etag: cachedEtag };
} catch (err) {
logger.error(err, "Secret service layer cache miss");
@@ -1617,7 +1622,7 @@ export const secretV2BridgeServiceFactory = ({
await keyStore.setItemWithExpiry(cacheKey, SECRET_DAL_TTL(), encryptedUpdatedCachedSecrets.toString("base64"));
}
recordSecretCacheWriteMetric({ bytes: cacheBytes, stored });
- recordSecretCacheAccessMetric(SecretCacheAccessResult.MISS);
+ recordSecretCacheAccessMetric(SecretCacheAccessResult.MISS, { hasIfNoneMatch, etagMissReason });
await keyStore.hashSet(etagRedisKey, etagField, computedEtag);
await keyStore.setExpiry(etagRedisKey, KeyStoreTtls.SecretEtagInSeconds);
return { ...payload, etag: computedEtag };
@@ -1708,7 +1713,7 @@ export const secretV2BridgeServiceFactory = ({
await keyStore.setItemWithExpiry(cacheKey, SECRET_DAL_TTL(), encryptedUpdatedCachedSecrets.toString("base64"));
}
recordSecretCacheWriteMetric({ bytes: cacheBytes, stored });
- recordSecretCacheAccessMetric(SecretCacheAccessResult.MISS);
+ recordSecretCacheAccessMetric(SecretCacheAccessResult.MISS, { hasIfNoneMatch, etagMissReason });
await keyStore.hashSet(etagRedisKey, etagField, computedEtag);
await keyStore.setExpiry(etagRedisKey, KeyStoreTtls.SecretEtagInSeconds);
return { ...payload, etag: computedEtag };