-
Notifications
You must be signed in to change notification settings - Fork 28
feat: add metrics hook #344
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
Open
danjuv
wants to merge
7
commits into
open-feature:main
Choose a base branch
from
danjuv:feat/add-metrics-hook
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+403
−81
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
48c5a74
feat: add metrics hook
danjuv f10820c
fix: use attribute name from constants
danjuv a6bd8c6
fix: add reason attribute to feature_flag.evaluation_success metric
danjuv 6e657af
fix: rename active_total -> active_count
danjuv 5f330c7
feat: add parameter to extract extra attributes from FlagMetadata
danjuv f9fb358
fix: move extra attribute retrieval to after hook
danjuv 1a86d27
chore: rename extra attributes method
danjuv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
79 changes: 3 additions & 76 deletions
79
hooks/openfeature-hooks-opentelemetry/src/openfeature/contrib/hook/opentelemetry/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,77 +1,4 @@ | ||
| import json | ||
| from .metric import MetricsHook | ||
| from .trace import TracingHook | ||
|
|
||
| from openfeature.exception import ErrorCode | ||
| from openfeature.flag_evaluation import FlagEvaluationDetails, Reason | ||
| from openfeature.hook import Hook, HookContext, HookHints | ||
| from opentelemetry import trace | ||
| from opentelemetry.semconv.attributes.error_attributes import ERROR_TYPE | ||
|
|
||
| OTEL_EVENT_NAME = "feature_flag.evaluation" | ||
|
|
||
|
|
||
| class EventAttributes: | ||
| KEY = "feature_flag.key" | ||
| RESULT_VALUE = "feature_flag.result.value" | ||
| RESULT_VARIANT = "feature_flag.result.variant" | ||
| CONTEXT_ID = "feature_flag.context.id" | ||
| PROVIDER_NAME = "feature_flag.provider.name" | ||
| RESULT_REASON = "feature_flag.result.reason" | ||
| SET_ID = "feature_flag.set.id" | ||
| VERSION = "feature_flag.version" | ||
|
|
||
|
|
||
| class TracingHook(Hook): | ||
| def __init__(self, exclude_exceptions: bool = False): | ||
| self.exclude_exceptions = exclude_exceptions | ||
|
|
||
| def finally_after( | ||
| self, | ||
| hook_context: HookContext, | ||
| details: FlagEvaluationDetails, | ||
| hints: HookHints, | ||
| ) -> None: | ||
| current_span = trace.get_current_span() | ||
|
|
||
| event_attributes = { | ||
| EventAttributes.KEY: details.flag_key, | ||
| EventAttributes.RESULT_VALUE: json.dumps(details.value), | ||
| EventAttributes.RESULT_REASON: str( | ||
| details.reason or Reason.UNKNOWN | ||
| ).lower(), | ||
| } | ||
|
|
||
| if details.variant: | ||
| event_attributes[EventAttributes.RESULT_VARIANT] = details.variant | ||
|
|
||
| if details.reason == Reason.ERROR: | ||
| error_type = str(details.error_code or ErrorCode.GENERAL).lower() | ||
| event_attributes[ERROR_TYPE] = error_type | ||
| if details.error_message: | ||
| event_attributes["error.message"] = details.error_message | ||
|
|
||
| context = hook_context.evaluation_context | ||
| if context.targeting_key: | ||
| event_attributes[EventAttributes.CONTEXT_ID] = context.targeting_key | ||
|
|
||
| if hook_context.provider_metadata: | ||
| event_attributes[EventAttributes.PROVIDER_NAME] = ( | ||
| hook_context.provider_metadata.name | ||
| ) | ||
|
|
||
| current_span.add_event(OTEL_EVENT_NAME, event_attributes) | ||
|
|
||
| def error( | ||
| self, hook_context: HookContext, exception: Exception, hints: HookHints | ||
| ) -> None: | ||
| if self.exclude_exceptions: | ||
| return | ||
| attributes = { | ||
| EventAttributes.KEY: hook_context.flag_key, | ||
| EventAttributes.RESULT_VALUE: json.dumps(hook_context.default_value), | ||
| } | ||
| if hook_context.provider_metadata: | ||
| attributes[EventAttributes.PROVIDER_NAME] = ( | ||
| hook_context.provider_metadata.name | ||
| ) | ||
| current_span = trace.get_current_span() | ||
| current_span.record_exception(exception, attributes) | ||
| __all__ = ["MetricsHook", "TracingHook"] |
19 changes: 19 additions & 0 deletions
19
...s/openfeature-hooks-opentelemetry/src/openfeature/contrib/hook/opentelemetry/constants.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| class Attributes: | ||
| OTEL_CONTEXT_ID = "feature_flag.context.id" | ||
| OTEL_EVENT_NAME = "feature_flag.evaluation" | ||
| OTEL_ERROR_TYPE = "error.type" | ||
| OTEL_ERROR_MESSAGE = "error.message" | ||
| OTEL_FLAG_KEY = "feature_flag.key" | ||
| OTEL_FLAG_VARIANT = "feature_flag.result.variant" | ||
| OTEL_PROVIDER_NAME = "feature_flag.provider.name" | ||
| OTEL_RESULT_VALUE = "feature_flag.result.value" | ||
| OTEL_RESULT_REASON = "feature_flag.result.reason" | ||
| OTEL_SET_ID = "feature_flag.set.id" | ||
| OTEL_VERSION = "feature_flag.version" | ||
|
|
||
|
|
||
| class Metrics: | ||
| ACTIVE_COUNT = "feature_flag.evaluation.active_count" | ||
| SUCCESS_TOTAL = "feature_flag.evaluation.success_total" | ||
| REQUEST_TOTAL = "feature_flag.evaluation.request_total" | ||
| ERROR_TOTAL = "feature_flag.evaluation.error_total" |
98 changes: 98 additions & 0 deletions
98
hooks/openfeature-hooks-opentelemetry/src/openfeature/contrib/hook/opentelemetry/metric.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| import typing | ||
|
|
||
| from openfeature.flag_evaluation import FlagEvaluationDetails, FlagMetadata, Reason | ||
| from openfeature.hook import Hook, HookContext, HookHints | ||
| from opentelemetry import metrics | ||
| from opentelemetry.util.types import AttributeValue | ||
|
|
||
| from .constants import Attributes, Metrics | ||
|
|
||
|
|
||
| class MetricsHook(Hook): | ||
| def __init__(self, extra_attributes: typing.Optional[list[str]] = None) -> None: | ||
| self.extra_attributes = extra_attributes or [] | ||
| meter: metrics.Meter = metrics.get_meter("openfeature.hooks.opentelemetry") | ||
| self.evaluation_active_count = meter.create_up_down_counter( | ||
| Metrics.ACTIVE_COUNT, "active flag evaluations" | ||
| ) | ||
| self.evaluation_error_total = meter.create_counter( | ||
| Metrics.ERROR_TOTAL, "error flag evaluations" | ||
| ) | ||
| self.evaluation_success_total = meter.create_counter( | ||
| Metrics.SUCCESS_TOTAL, "success flag evaluations" | ||
| ) | ||
| self.evaluation_request_total = meter.create_counter( | ||
| Metrics.REQUEST_TOTAL, "request flag evaluations" | ||
| ) | ||
|
|
||
| def before(self, hook_context: HookContext, hints: HookHints) -> None: | ||
| attributes: dict[str, AttributeValue] = { | ||
| Attributes.OTEL_FLAG_KEY: hook_context.flag_key, | ||
| } | ||
| if hook_context.provider_metadata: | ||
| attributes[Attributes.OTEL_PROVIDER_NAME] = ( | ||
| hook_context.provider_metadata.name | ||
| ) | ||
| self.evaluation_active_count.add(1, attributes) | ||
| self.evaluation_request_total.add(1, attributes) | ||
|
|
||
| def after( | ||
| self, | ||
| hook_context: HookContext, | ||
| details: FlagEvaluationDetails, | ||
| hints: HookHints, | ||
| ) -> None: | ||
| attributes: dict[str, AttributeValue] = { | ||
| Attributes.OTEL_FLAG_KEY: details.flag_key, | ||
| Attributes.OTEL_RESULT_REASON: str( | ||
| details.reason or Reason.UNKNOWN | ||
| ).lower(), | ||
| } | ||
| if details.variant: | ||
| attributes[Attributes.OTEL_FLAG_VARIANT] = details.variant | ||
| if hook_context.provider_metadata: | ||
| attributes[Attributes.OTEL_PROVIDER_NAME] = ( | ||
| hook_context.provider_metadata.name | ||
| ) | ||
| attributes = attributes | get_extra_attributes( | ||
| self.extra_attributes, details.flag_metadata | ||
| ) | ||
| self.evaluation_success_total.add(1, attributes) | ||
|
|
||
| def error( | ||
| self, hook_context: HookContext, exception: Exception, hints: HookHints | ||
| ) -> None: | ||
| attributes: dict[str, AttributeValue] = { | ||
| Attributes.OTEL_FLAG_KEY: hook_context.flag_key, | ||
| "exception": str(exception).lower(), | ||
| } | ||
| if hook_context.provider_metadata: | ||
| attributes[Attributes.OTEL_PROVIDER_NAME] = ( | ||
| hook_context.provider_metadata.name | ||
| ) | ||
| self.evaluation_error_total.add(1, attributes) | ||
|
|
||
| def finally_after( | ||
| self, | ||
| hook_context: HookContext, | ||
| details: FlagEvaluationDetails, | ||
| hints: HookHints, | ||
| ) -> None: | ||
| attributes: dict[str, AttributeValue] = { | ||
| Attributes.OTEL_FLAG_KEY: hook_context.flag_key, | ||
| } | ||
| if hook_context.provider_metadata: | ||
| attributes[Attributes.OTEL_PROVIDER_NAME] = ( | ||
| hook_context.provider_metadata.name | ||
| ) | ||
| self.evaluation_active_count.add(-1, attributes) | ||
|
|
||
|
|
||
| def get_extra_attributes( | ||
| extra_attributes: list[str], metadata: FlagMetadata | ||
| ) -> dict[str, AttributeValue]: | ||
| attributes: dict[str, AttributeValue] = {} | ||
| for attribute in extra_attributes: | ||
| if (attr := metadata.get(attribute)) is not None: | ||
| attributes[attribute] = attr | ||
| return attributes | ||
65 changes: 65 additions & 0 deletions
65
hooks/openfeature-hooks-opentelemetry/src/openfeature/contrib/hook/opentelemetry/trace.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| import json | ||
|
|
||
| from openfeature.exception import ErrorCode | ||
| from openfeature.flag_evaluation import FlagEvaluationDetails, Reason | ||
| from openfeature.hook import Hook, HookContext, HookHints | ||
| from opentelemetry import trace | ||
|
|
||
| from .constants import Attributes | ||
|
|
||
|
|
||
| class TracingHook(Hook): | ||
| def __init__(self, exclude_exceptions: bool = False): | ||
| self.exclude_exceptions = exclude_exceptions | ||
|
|
||
| def finally_after( | ||
| self, | ||
| hook_context: HookContext, | ||
| details: FlagEvaluationDetails, | ||
| hints: HookHints, | ||
| ) -> None: | ||
| current_span = trace.get_current_span() | ||
|
|
||
| event_attributes = { | ||
| Attributes.OTEL_FLAG_KEY: details.flag_key, | ||
| Attributes.OTEL_RESULT_VALUE: json.dumps(details.value), | ||
| Attributes.OTEL_RESULT_REASON: str( | ||
| details.reason or Reason.UNKNOWN | ||
| ).lower(), | ||
| } | ||
|
|
||
| if details.variant: | ||
| event_attributes[Attributes.OTEL_FLAG_VARIANT] = details.variant | ||
|
|
||
| if details.reason == Reason.ERROR: | ||
| error_type = str(details.error_code or ErrorCode.GENERAL).lower() | ||
| event_attributes[Attributes.OTEL_ERROR_TYPE] = error_type | ||
| if details.error_message: | ||
| event_attributes[Attributes.OTEL_ERROR_MESSAGE] = details.error_message | ||
|
|
||
| context = hook_context.evaluation_context | ||
| if context.targeting_key: | ||
| event_attributes[Attributes.OTEL_CONTEXT_ID] = context.targeting_key | ||
|
|
||
| if hook_context.provider_metadata: | ||
| event_attributes[Attributes.OTEL_PROVIDER_NAME] = ( | ||
| hook_context.provider_metadata.name | ||
| ) | ||
|
|
||
| current_span.add_event(Attributes.OTEL_EVENT_NAME, event_attributes) | ||
|
|
||
| def error( | ||
| self, hook_context: HookContext, exception: Exception, hints: HookHints | ||
| ) -> None: | ||
| if self.exclude_exceptions: | ||
| return | ||
| attributes = { | ||
| Attributes.OTEL_FLAG_KEY: hook_context.flag_key, | ||
| Attributes.OTEL_RESULT_VALUE: json.dumps(hook_context.default_value), | ||
| } | ||
| if hook_context.provider_metadata: | ||
| attributes[Attributes.OTEL_PROVIDER_NAME] = ( | ||
| hook_context.provider_metadata.name | ||
| ) | ||
| current_span = trace.get_current_span() | ||
| current_span.record_exception(exception, attributes) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
str(exception)as a metric attribute value can lead to high cardinality, which is an anti-pattern for metrics and can cause issues with your monitoring backend (e.g., performance, cost). The OpenFeature Enhancement Proposal (OFEP) for metric hooks suggests usingerror.typewith a low-cardinality value.To align with best practices and the OFEP, I suggest using the exception's type name instead of its string representation. This will keep the cardinality of the
error.typeattribute low, addressing the concern you raised in the PR description.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with this in principle, but the OFEP specifically states to use the message
@toddbaert @beeme1mr can you confirm?