Skip to content

Commit

Permalink
refactor: improve OpenTelemetry configuration (#127)
Browse files Browse the repository at this point in the history
  • Loading branch information
evansims authored Sep 22, 2024
2 parents 4eb7bbe + 5afb454 commit bacf16c
Show file tree
Hide file tree
Showing 27 changed files with 2,043 additions and 1,524 deletions.
20 changes: 10 additions & 10 deletions docs/opentelemetry.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ If you configure the OpenTelemetry SDK, these metrics will be exported and sent
| `http.request.method` | string | Yes | HTTP method for the request |
| `http.request.resend_count` | int | Yes | Number of retries attempted, if any |
| `http.response.status_code` | int | Yes | Status code of the response (e.g., `200` for success) |
| `http.server.request.duration` | int | Yes | Time taken by the FGA server to process and evaluate the request, in milliseconds |
| `http.server.request.duration` | int | No | Time taken by the FGA server to process and evaluate the request, in milliseconds |
| `url.scheme` | string | Yes | HTTP scheme of the request (`http`/`https`) |
| `url.full` | string | Yes | Full URL of the request |
| `user_agent.original` | string | Yes | User Agent used in the query |
Expand Down Expand Up @@ -102,15 +102,15 @@ configuration = ClientConfiguration(
store_id=os.getenv("FGA_STORE_ID"),
authorization_model_id=os.getenv("FGA_AUTHORIZATION_MODEL_ID"),

# If you are comfortable with the default configuration outlined in the tables above, you can omit providing your own TelemetryConfiguration object.
telemetry=TelemetryConfiguration(
metrics=TelemetryMetricsConfiguration(
histogram_request_duration=TelemetryMetricConfiguration(
attr_fga_client_request_method=True,
attr_http_response_status_code=True,
),
),
),
# If you are comfortable with the default configuration outlined in the tables above, you can omit providing your own TelemetryConfiguration object, as one will be created for you.
telemetry={
"metrics": {
"fga-client.request.duration": {
"fga-client.request.method": True,
"http.response.status_code": True,
},
},
},
)

fga = OpenFgaClient(configuration)
Expand Down
119 changes: 57 additions & 62 deletions example/opentelemetry/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@
)
from opentelemetry.sdk.resources import SERVICE_NAME, Resource

from openfga_sdk.telemetry.configuration import (
TelemetryConfiguration,
TelemetryMetricConfiguration,
TelemetryMetricsConfiguration,
)

# For usage convenience of this example, we will import the OpenFGA SDK from the parent directory.
sdk_path = os.path.realpath(os.path.join(os.path.abspath(__file__), "..", "..", ".."))
sys.path.insert(0, sdk_path)
Expand All @@ -36,6 +30,7 @@
Credentials,
)
from openfga_sdk.exceptions import FgaValidationException
from openfga_sdk.telemetry.configuration import TelemetryConfiguration


class app:
Expand Down Expand Up @@ -88,62 +83,62 @@ async def fga_client(self, env: dict[str, str] = {}) -> OpenFgaClient:

if not self._telemetry:
# Configure the telemetry metrics to be collected.
# Note: the following represents the default configuration values, so unless you want to change them, you can omit this step.
self._telemetry = TelemetryConfiguration(
metrics=TelemetryMetricsConfiguration(
counter_credentials_request=TelemetryMetricConfiguration(
attr_fga_client_request_client_id=True,
attr_fga_client_request_method=True,
attr_fga_client_request_model_id=True,
attr_fga_client_request_store_id=True,
attr_fga_client_response_model_id=True,
attr_fga_client_user=False,
attr_http_client_request_duration=False,
attr_http_host=True,
attr_http_request_method=True,
attr_http_request_resend_count=True,
attr_http_response_status_code=True,
attr_http_server_request_duration=False,
attr_http_url_scheme=True,
attr_http_url_full=True,
attr_user_agent_original=True,
),
histogram_request_duration=TelemetryMetricConfiguration(
attr_fga_client_request_client_id=True,
attr_fga_client_request_method=True,
attr_fga_client_request_model_id=True,
attr_fga_client_request_store_id=True,
attr_fga_client_response_model_id=True,
attr_fga_client_user=False,
attr_http_client_request_duration=False,
attr_http_host=True,
attr_http_request_method=True,
attr_http_request_resend_count=True,
attr_http_response_status_code=True,
attr_http_server_request_duration=False,
attr_http_url_scheme=True,
attr_http_url_full=True,
attr_user_agent_original=True,
),
histogram_query_duration=TelemetryMetricConfiguration(
attr_fga_client_request_client_id=True,
attr_fga_client_request_method=True,
attr_fga_client_request_model_id=True,
attr_fga_client_request_store_id=True,
attr_fga_client_response_model_id=True,
attr_fga_client_user=False,
attr_http_client_request_duration=False,
attr_http_host=True,
attr_http_request_method=True,
attr_http_request_resend_count=True,
attr_http_response_status_code=True,
attr_http_server_request_duration=False,
attr_http_url_scheme=True,
attr_http_url_full=True,
attr_user_agent_original=True,
),
),
)
# Note: the following represents the default configuration values, so unless you want to customize what's reported, you can omit this.
self._telemetry = {
"metrics": {
"fga-client.credentials.request": {
"fga-client.request.client_id": True,
"fga-client.request.method": True,
"fga-client.request.model_id": True,
"fga-client.request.store_id": True,
"fga-client.response.model_id": True,
"fga-client.user": False,
"http.client.request.duration": False,
"http.host": True,
"http.request.method": True,
"http.request.resend_count": True,
"http.response.status_code": True,
"http.server.request.duration": False,
"url.scheme": True,
"url.full": True,
"user_agent.original": True,
},
"fga-client.request.duration": {
"fga-client.request.client_id": True,
"fga-client.request.method": True,
"fga-client.request.model_id": True,
"fga-client.request.store_id": True,
"fga-client.response.model_id": True,
"fga-client.user": False,
"http.client.request.duration": False,
"http.host": True,
"http.request.method": True,
"http.request.resend_count": True,
"http.response.status_code": True,
"http.server.request.duration": False,
"url.scheme": True,
"url.full": True,
"user_agent.original": True,
},
"fga-client.query.duration": {
"fga-client.request.client_id": True,
"fga-client.request.method": True,
"fga-client.request.model_id": True,
"fga-client.request.store_id": True,
"fga-client.response.model_id": True,
"fga-client.user": False,
"http.client.request.duration": False,
"http.host": True,
"http.request.method": True,
"http.request.resend_count": True,
"http.response.status_code": True,
"http.server.request.duration": False,
"url.scheme": True,
"url.full": True,
"user_agent.original": True,
},
}
}

self._configuration.telemetry = self._telemetry

Expand Down
2 changes: 2 additions & 0 deletions openfga_sdk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@
from openfga_sdk.models.write_request_writes import WriteRequestWrites
from openfga_sdk.telemetry.configuration import (
TelemetryConfiguration,
TelemetryConfigurations,
TelemetryConfigurationType,
TelemetryMetricConfiguration,
TelemetryMetricsConfiguration,
)
Loading

0 comments on commit bacf16c

Please sign in to comment.