Skip to content

fix: otel metric semantic convs #475

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions libs/hooks/open-telemetry/src/lib/constants.ts

This file was deleted.

16 changes: 16 additions & 0 deletions libs/hooks/open-telemetry/src/lib/conventions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// see: https://opentelemetry.io/docs/specs/otel/logs/semantic_conventions/feature-flags/
export const FEATURE_FLAG = 'feature_flag';
export const EXCEPTION_ATTR = 'exception'

export const ACTIVE_COUNT_NAME = `${FEATURE_FLAG}.evaluation_active_count`;
export const REQUESTS_TOTAL_NAME = `${FEATURE_FLAG}.evaluation_requests_total`;
export const SUCCESS_TOTAL_NAME = `${FEATURE_FLAG}.evaluation_success_total`;
export const ERROR_TOTAL_NAME = `${FEATURE_FLAG}.evaluation_error_total`;

export type EvaluationAttributes = {[key: `${typeof FEATURE_FLAG}.${string}`]: string | undefined };
export type ExceptionAttributes = { [EXCEPTION_ATTR]: string };

export const KEY_ATTR: keyof EvaluationAttributes = `${FEATURE_FLAG}.key`;
export const PROVIDER_NAME_ATTR: keyof EvaluationAttributes = `${FEATURE_FLAG}.provider_name`;
export const VARIANT_ATTR: keyof EvaluationAttributes = `${FEATURE_FLAG}.variant`;
export const REASON_ATTR: keyof EvaluationAttributes = `${FEATURE_FLAG}.reason`;
26 changes: 13 additions & 13 deletions libs/hooks/open-telemetry/src/lib/metrics/metrics-hook.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
MetricReader,
ScopeMetrics,
} from '@opentelemetry/sdk-metrics';
import { ACTIVE_COUNT_NAME, ERROR_TOTAL_NAME, REQUESTS_TOTAL_NAME, SUCCESS_TOTAL_NAME } from '../constants';
import { ACTIVE_COUNT_NAME, ERROR_TOTAL_NAME, KEY_ATTR, PROVIDER_NAME_ATTR, REASON_ATTR, REQUESTS_TOTAL_NAME, SUCCESS_TOTAL_NAME, VARIANT_ATTR } from '../conventions';
import { MetricsHook } from './metrics-hook';

// no-op "in-memory" reader
Expand Down Expand Up @@ -53,7 +53,7 @@ describe(MetricsHook.name, () => {
ACTIVE_COUNT_NAME,
0,
(point) =>
point.value === 1 && point.attributes.key === FLAG_KEY && point.attributes.provider === PROVIDER_NAME
point.value === 1 && point.attributes[KEY_ATTR] === FLAG_KEY && point.attributes[PROVIDER_NAME_ATTR] === PROVIDER_NAME
)
).toBeTruthy();
expect(
Expand All @@ -62,7 +62,7 @@ describe(MetricsHook.name, () => {
REQUESTS_TOTAL_NAME,
0,
(point) =>
point.value === 1 && point.attributes.key === FLAG_KEY && point.attributes.provider === PROVIDER_NAME
point.value === 1 && point.attributes[KEY_ATTR] === FLAG_KEY && point.attributes[PROVIDER_NAME_ATTR] === PROVIDER_NAME
)
).toBeTruthy();
});
Expand Down Expand Up @@ -97,10 +97,10 @@ describe(MetricsHook.name, () => {
0,
(point) =>
point.value === 1 &&
point.attributes.key === FLAG_KEY &&
point.attributes.provider === PROVIDER_NAME &&
point.attributes.variant === VARIANT &&
point.attributes.reason === StandardResolutionReasons.STATIC
point.attributes[KEY_ATTR] === FLAG_KEY &&
point.attributes[PROVIDER_NAME_ATTR] === PROVIDER_NAME &&
point.attributes[VARIANT_ATTR] === VARIANT &&
point.attributes[REASON_ATTR] === StandardResolutionReasons.STATIC
)
).toBeTruthy();
});
Expand Down Expand Up @@ -130,10 +130,10 @@ describe(MetricsHook.name, () => {
1,
(point) =>
point.value === 1 &&
point.attributes.key === FLAG_KEY &&
point.attributes.provider === PROVIDER_NAME &&
point.attributes.variant === VALUE.toString() &&
point.attributes.reason === StandardResolutionReasons.STATIC
point.attributes[KEY_ATTR] === FLAG_KEY &&
point.attributes[PROVIDER_NAME_ATTR] === PROVIDER_NAME &&
point.attributes[VARIANT_ATTR] === VALUE.toString() &&
point.attributes[REASON_ATTR] === StandardResolutionReasons.STATIC
)
).toBeTruthy();
});
Expand All @@ -160,7 +160,7 @@ describe(MetricsHook.name, () => {
ACTIVE_COUNT_NAME,
1,
(point) =>
point.value === -1 && point.attributes.key === FLAG_KEY && point.attributes.provider === PROVIDER_NAME
point.value === -1 && point.attributes[KEY_ATTR] === FLAG_KEY && point.attributes[PROVIDER_NAME_ATTR] === PROVIDER_NAME
)
).toBeTruthy();
});
Expand Down Expand Up @@ -188,7 +188,7 @@ describe(MetricsHook.name, () => {
ERROR_TOTAL_NAME,
0,
(point) =>
point.value === 1 && point.attributes.key === FLAG_KEY && point.attributes.provider === PROVIDER_NAME
point.value === 1 && point.attributes[KEY_ATTR] === FLAG_KEY && point.attributes[PROVIDER_NAME_ATTR] === PROVIDER_NAME
)
).toBeTruthy();
});
Expand Down
44 changes: 26 additions & 18 deletions libs/hooks/open-telemetry/src/lib/metrics/metrics-hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,21 @@ import {
type HookContext,
} from '@openfeature/js-sdk';
import { Counter, UpDownCounter, ValueType, metrics } from '@opentelemetry/api';
import { ACTIVE_COUNT_NAME, ERROR_TOTAL_NAME, REQUESTS_TOTAL_NAME, SUCCESS_TOTAL_NAME } from '../constants';
import {
ACTIVE_COUNT_NAME,
EXCEPTION_ATTR,
ERROR_TOTAL_NAME,
EvaluationAttributes,
ExceptionAttributes,
KEY_ATTR,
PROVIDER_NAME_ATTR,
REASON_ATTR,
REQUESTS_TOTAL_NAME,
SUCCESS_TOTAL_NAME,
VARIANT_ATTR
} from '../conventions';

type EvaluationAttributes = Pick<EvaluationDetails<FlagValue>, 'variant' | 'reason'> & {
key: string;
provider: string;
};
type ErrorEvaluationAttributes = EvaluationAttributes & { exception: string };
type ErrorEvaluationAttributes = EvaluationAttributes & ExceptionAttributes;

const METER_NAME = 'js.openfeature.dev';

Expand Down Expand Up @@ -49,35 +57,35 @@ export class MetricsHook implements Hook {
}

before(hookContext: BeforeHookContext) {
const attributes = {
key: hookContext.flagKey,
provider: hookContext.providerMetadata.name,
const attributes: EvaluationAttributes = {
[KEY_ATTR]: hookContext.flagKey,
[PROVIDER_NAME_ATTR]: hookContext.providerMetadata.name,
};
this.evaluationActiveUpDownCounter.add(1, attributes);
this.evaluationRequestCounter.add(1, attributes);
}

after(hookContext: Readonly<HookContext<FlagValue>>, evaluationDetails: EvaluationDetails<FlagValue>) {
this.evaluationSuccessCounter.add(1, {
key: hookContext.flagKey,
provider: hookContext.providerMetadata.name,
variant: evaluationDetails.variant ?? evaluationDetails.value?.toString(),
reason: evaluationDetails.reason ?? StandardResolutionReasons.UNKNOWN,
[KEY_ATTR]: hookContext.flagKey,
[PROVIDER_NAME_ATTR]: hookContext.providerMetadata.name,
[VARIANT_ATTR]: evaluationDetails.variant ?? evaluationDetails.value?.toString(),
[REASON_ATTR]: evaluationDetails.reason ?? StandardResolutionReasons.UNKNOWN,
});
}

error(hookContext: Readonly<HookContext<FlagValue>>, error: unknown) {
this.evaluationErrorCounter.add(1, {
key: hookContext.flagKey,
provider: hookContext.providerMetadata.name,
exception: (error as Error)?.message || 'Unknown error',
[KEY_ATTR]: hookContext.flagKey,
[PROVIDER_NAME_ATTR]: hookContext.providerMetadata.name,
[EXCEPTION_ATTR]: (error as Error)?.message || 'Unknown error',
});
}

finally(hookContext: Readonly<HookContext<FlagValue>>) {
this.evaluationActiveUpDownCounter.add(-1, {
key: hookContext.flagKey,
provider: hookContext.providerMetadata.name,
[KEY_ATTR]: hookContext.flagKey,
[PROVIDER_NAME_ATTR]: hookContext.providerMetadata.name,
});
}
}
14 changes: 4 additions & 10 deletions libs/hooks/open-telemetry/src/lib/traces/tracing-hook.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import { Hook, HookContext, EvaluationDetails, FlagValue } from '@openfeature/js-sdk';
import { trace } from '@opentelemetry/api';
import { FEATURE_FLAG } from '../constants';

const spanEventProperties = Object.freeze({
FLAG_KEY: `${FEATURE_FLAG}.key`,
PROVIDER_NAME: `${FEATURE_FLAG}.provider_name`,
VARIANT: `${FEATURE_FLAG}.variant`,
});
import { FEATURE_FLAG, KEY_ATTR, PROVIDER_NAME_ATTR, VARIANT_ATTR } from '../conventions';

export class TracingHook implements Hook {
after(hookContext: HookContext, evaluationDetails: EvaluationDetails<FlagValue>) {
Expand All @@ -23,9 +17,9 @@ export class TracingHook implements Hook {
}

currentTrace.addEvent(FEATURE_FLAG, {
[spanEventProperties.FLAG_KEY]: hookContext.flagKey,
[spanEventProperties.PROVIDER_NAME]: hookContext.providerMetadata.name,
[spanEventProperties.VARIANT]: variant,
[KEY_ATTR]: hookContext.flagKey,
[PROVIDER_NAME_ATTR]: hookContext.providerMetadata.name,
[VARIANT_ATTR]: variant,
});
}
}
Expand Down