|
3 | 3 | from typing import Any, Iterable |
4 | 4 |
|
5 | 5 | from django.http import HttpRequest |
| 6 | +from fastuaparser import parse_ua # type: ignore[import-untyped] |
6 | 7 | from influxdb_client.client.flux_table import FluxTable |
7 | 8 | from pydantic import Field, create_model |
8 | 9 | from pydantic.type_adapter import TypeAdapter |
|
14 | 15 | AnnotatedAPIUsageBucket, |
15 | 16 | AnnotatedAPIUsageKey, |
16 | 17 | FeatureEvaluationCacheKey, |
| 18 | + InputLabels, |
17 | 19 | Labels, |
18 | 20 | TrackFeatureEvaluationsByEnvironmentData, |
19 | 21 | TrackFeatureEvaluationsByEnvironmentKwargs, |
@@ -112,19 +114,37 @@ def map_flux_tables_to_feature_evaluation_data( |
112 | 114 | ] |
113 | 115 |
|
114 | 116 |
|
| 117 | +def map_input_labels_to_labels(input_labels: InputLabels) -> Labels: |
| 118 | + labels: Labels = {} |
| 119 | + for label, value in input_labels.items(): |
| 120 | + if label == "sdk_user_agent": |
| 121 | + labels["user_agent"] = value |
| 122 | + continue |
| 123 | + elif label == "user_agent": |
| 124 | + # fastuaparser classifies unrecognized UAs as "Other" — assume these to |
| 125 | + # represent server-side SDKs. |
| 126 | + parsed_ua_string: str = parse_ua(value) |
| 127 | + is_server_side_sdk = parsed_ua_string.startswith("Other") |
| 128 | + # Skip browser SDKs that don't send the special header. |
| 129 | + if not is_server_side_sdk: |
| 130 | + continue |
| 131 | + labels[label] = value |
| 132 | + return labels |
| 133 | + |
| 134 | + |
115 | 135 | def map_request_to_labels(request: HttpRequest) -> Labels: |
116 | 136 | if not ( |
117 | 137 | get_client("local", local_eval=True) |
118 | 138 | .get_environment_flags() |
119 | 139 | .is_feature_enabled("sdk_metrics_labels") |
120 | 140 | ): |
121 | 141 | return {} |
122 | | - result: Labels = _RequestHeaderLabelsModel.model_validate( |
| 142 | + labels: InputLabels = _RequestHeaderLabelsModel.model_validate( |
123 | 143 | request.headers, |
124 | 144 | ).model_dump( |
125 | 145 | exclude_unset=True, |
126 | 146 | ) |
127 | | - return result |
| 147 | + return map_input_labels_to_labels(labels) |
128 | 148 |
|
129 | 149 |
|
130 | 150 | def map_feature_evaluation_cache_to_track_feature_evaluations_by_environment_kwargs( |
|
0 commit comments