Skip to content

Commit 186836c

Browse files
feat: support exporter metadata in python provider (#2976)
Signed-off-by: Thomas Poignant <[email protected]>
1 parent 54ea164 commit 186836c

File tree

5 files changed

+250
-153
lines changed

5 files changed

+250
-153
lines changed

openfeature/providers/python-provider/gofeatureflag_python_provider/data_collector_hook.py

+11-8
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
import datetime
22
import threading
33
import time
4-
from http import HTTPStatus
5-
from typing import Optional
6-
from urllib.parse import urljoin
7-
84
import urllib3
9-
from openfeature.flag_evaluation import FlagEvaluationDetails, Reason
10-
from openfeature.hook import Hook, HookContext
11-
125
from gofeatureflag_python_provider.options import GoFeatureFlagOptions
136
from gofeatureflag_python_provider.request_data_collector import (
147
FeatureEvent,
158
RequestDataCollector,
169
)
10+
from http import HTTPStatus
11+
from openfeature.flag_evaluation import FlagEvaluationDetails, Reason
12+
from openfeature.hook import Hook, HookContext
13+
from typing import Optional
14+
from urllib.parse import urljoin
1715

1816
default_targeting_key = "undefined-targetingKey"
1917

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

3535
def __init__(self, options: GoFeatureFlagOptions, http_client: urllib3.PoolManager):
3636
self._http_client = http_client
@@ -39,6 +39,9 @@ def __init__(self, options: GoFeatureFlagOptions, http_client: urllib3.PoolManag
3939
self._data_collector_endpoint = urljoin(
4040
str(self._options.endpoint), "/v1/data/collector"
4141
)
42+
self._exporter_metadata = options.exporter_metadata
43+
self._exporter_metadata["provider"] = "python"
44+
self._exporter_metadata["openfeature"] = True
4245

4346
def after(
4447
self, hook_context: HookContext, details: FlagEvaluationDetails, hints: dict
@@ -106,7 +109,7 @@ def _collect_data(self):
106109
if len(self._event_queue) > 0:
107110
try:
108111
goff_request = RequestDataCollector(
109-
meta={"provider": "open-feature-python-sdk"},
112+
meta=self._exporter_metadata,
110113
events=self._event_queue,
111114
)
112115
headers = {"Content-Type": "application/json"}

openfeature/providers/python-provider/gofeatureflag_python_provider/options.py

+7
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,10 @@ class GoFeatureFlagOptions(BaseModel):
5050
# an API Key to the provider. Please ask the administrator of the relay proxy to provide an API Key.
5151
# Default: None
5252
api_key: typing.Optional[str] = None
53+
54+
# ExporterMetadata (optional) is the metadata we send to the GO Feature Flag relay proxy when we report the
55+
# evaluation data usage.
56+
#
57+
# ‼️Important: If you are using a GO Feature Flag relay proxy before version v1.41.0, the information of this
58+
# field will not be added to your feature events.
59+
exporter_metadata: typing.Optional[dict] = {}

openfeature/providers/python-provider/gofeatureflag_python_provider/request_data_collector.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
from typing import Optional, Any
2-
3-
from pydantic import Field, SkipValidation
4-
51
from gofeatureflag_python_provider.options import BaseModel
2+
from pydantic import Field, SkipValidation
3+
from typing import Optional, Any, Union
64

75

86
class FeatureEvent(BaseModel):
@@ -47,7 +45,7 @@ class FeatureEvent(BaseModel):
4745

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

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

0 commit comments

Comments
 (0)