Skip to content
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

feat: support exporter metadata in python provider #2976

Merged
merged 1 commit into from
Jan 20, 2025
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
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
import datetime
import threading
import time
from http import HTTPStatus
from typing import Optional
from urllib.parse import urljoin

import urllib3
from openfeature.flag_evaluation import FlagEvaluationDetails, Reason
from openfeature.hook import Hook, HookContext

from gofeatureflag_python_provider.options import GoFeatureFlagOptions
from gofeatureflag_python_provider.request_data_collector import (
FeatureEvent,
RequestDataCollector,
)
from http import HTTPStatus
from openfeature.flag_evaluation import FlagEvaluationDetails, Reason
from openfeature.hook import Hook, HookContext
from typing import Optional
from urllib.parse import urljoin

default_targeting_key = "undefined-targetingKey"

Expand All @@ -31,6 +29,8 @@ class DataCollectorHook(Hook):
_http_client: urllib3.PoolManager = None
# _data_event_queue is the list of data to collect
_event_queue: list[FeatureEvent] = []
# _exporter_metadata is the metadata we send to the GO Feature Flag relay proxy when we report the evaluation data usage.
_exporter_metadata: dict = {}

def __init__(self, options: GoFeatureFlagOptions, http_client: urllib3.PoolManager):
self._http_client = http_client
Expand All @@ -39,6 +39,9 @@ def __init__(self, options: GoFeatureFlagOptions, http_client: urllib3.PoolManag
self._data_collector_endpoint = urljoin(
str(self._options.endpoint), "/v1/data/collector"
)
self._exporter_metadata = options.exporter_metadata
self._exporter_metadata["provider"] = "python"
self._exporter_metadata["openfeature"] = True

def after(
self, hook_context: HookContext, details: FlagEvaluationDetails, hints: dict
Expand Down Expand Up @@ -106,7 +109,7 @@ def _collect_data(self):
if len(self._event_queue) > 0:
try:
goff_request = RequestDataCollector(
meta={"provider": "open-feature-python-sdk"},
meta=self._exporter_metadata,
events=self._event_queue,
)
headers = {"Content-Type": "application/json"}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,10 @@ class GoFeatureFlagOptions(BaseModel):
# an API Key to the provider. Please ask the administrator of the relay proxy to provide an API Key.
# Default: None
api_key: typing.Optional[str] = None

# ExporterMetadata (optional) is the metadata we send to the GO Feature Flag relay proxy when we report the
# evaluation data usage.
#
# ‼️Important: If you are using a GO Feature Flag relay proxy before version v1.41.0, the information of this
# field will not be added to your feature events.
exporter_metadata: typing.Optional[dict] = {}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from typing import Optional, Any

from pydantic import Field, SkipValidation

from gofeatureflag_python_provider.options import BaseModel
from pydantic import Field, SkipValidation
from typing import Optional, Any, Union


class FeatureEvent(BaseModel):
Expand Down Expand Up @@ -47,7 +45,7 @@ class FeatureEvent(BaseModel):

class RequestDataCollector(BaseModel):
# Meta are the extra information added to identify who is calling the endpoint.
meta: Optional[dict[str, str]] = None
meta: Optional[dict[str, Union[str, int, float, bool]]] = None

# Events is the list of the event we send in the payload
events: list[FeatureEvent] = []
Loading
Loading