open-feature / python-sdk-contrib

Community contributions for hooks and reference providers in python
https://openfeature.dev
11 stars 11 forks source link

`api.add_hooks(TracingHook())` doesn't work #67

Closed julianocosta89 closed 7 months ago

julianocosta89 commented 7 months ago

Hello all,

I was following those steps to add the hook to the OTel demo: https://github.com/open-feature/python-sdk-contrib/tree/main/hooks/openfeature-hooks-opentelemetry#usage

But when using: api.add_hooks(TracingHook())

I got the following error:

TypeError: can only concatenate list (not "TracingHook") to list
Traceback (most recent call last):
  File "/usr/src/app/recommendation_server.py", line 131, in <module>
    api.add_hooks(TracingHook())
  File "/usr/local/lib/python3.12/site-packages/openfeature/api.py", line 55, in add_hooks
    _hooks = _hooks + hooks
             ~~~~~~~^~~~~~~

Then I've tried using client.add_hooks(TracingHook()) and got the following one:

2024-03-15 11:48:19,126 ERROR [grpc._server] [_server.py:583] [trace_id=0 span_id=0 resource.service.name=recommendationservice trace_sampled=False] - Exception calling application: can only concatenate list (not "TracingHook") to list
Traceback (most recent call last):
  File "/usr/local/lib/python3.12/site-packages/grpc/_server.py", line 555, in _call_behavior
    response_or_iterator = behavior(argument, context)
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/opentelemetry/instrumentation/grpc/_server.py", line 320, in telemetry_interceptor
    raise error
  File "/usr/local/lib/python3.12/site-packages/opentelemetry/instrumentation/grpc/_server.py", line 311, in telemetry_interceptor
    return behavior(request_or_iterator, context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/app/recommendation_server.py", line 44, in ListRecommendations
    prod_list = get_product_list(request.product_ids)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/app/recommendation_server.py", line 78, in get_product_list
    if check_feature_flag("recommendationCache"):
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/app/recommendation_server.py", line 127, in check_feature_flag
    api.add_hooks(TracingHook())
  File "/usr/local/lib/python3.12/site-packages/openfeature/api.py", line 55, in add_hooks
    _hooks = _hooks + hooks
             ~~~~~~~^~~~~~~
julianocosta89 commented 7 months ago

Also, it seems that the environment variables are not working properly. I've set FLAGD_HOST and FLAGD_PORT, but when running the service I got the following error:

debug_error_string = "UNKNOWN:Error received from peer  {created_time:"2024-03-15T12:06:02.432495513+00:00", grpc_status:14, grpc_message:"failed to connect to all addresses; last error: UNKNOWN: ipv4:127.0.0.1:8013: Failed to connect to remote host: Connection refused"}"

Looks like it is not using the env vars.

federicobond commented 7 months ago

Hi @julianocosta89, thanks for the report! This is a mistake in our docs. add_hooks should be called with a list of hooks, like this:

api.add_hooks([TracingHook()])

As for the environment variables in the Flagd, we have not yet released the version that supports them, but we should do so very soon.

federicobond commented 7 months ago

Opened https://github.com/open-feature/python-sdk-contrib/pull/68 to fix the docs.