Skip to content

Commit

Permalink
fix(telemetry): http.request.resend_count no longer counts original…
Browse files Browse the repository at this point in the history
… request
  • Loading branch information
rhamzeh committed Jul 30, 2024
1 parent 3244346 commit 88a083e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 39 deletions.
34 changes: 17 additions & 17 deletions OpenTelemetry.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,23 @@ In cases when metrics events are sent, they will not be viewable outside of infr

### Supported attributes

| Attribute Name | Type | Description |
|--------------------------------|----------|-------------------------------------------------------------------------------------------------------------------------|
| `fga-client.response.model_id` | `string` | The authorization model ID that the FGA server used |
| `fga-client.request.method` | `string` | The FGA method/action that was performed (e.g. `Check`, `ListObjects`, ...) in TitleCase |
| `fga-client.request.store_id` | `string` | The store ID that was sent as part of the request |
| `fga-client.request.model_id` | `string` | The authorization model ID that was sent as part of the request, if any |
| `fga-client.request.client_id` | `string` | The client ID associated with the request, if any |
| `fga-client.user` | `string` | The user that is associated with the action of the request for check and list objects |
| `http.request.resend_count` | `int` | The number of retries attempted (starting from 1 for the original request) |
| `http.response.status_code` | `int` | The status code of the response |
| `http.request.method` | `string` | The HTTP method for the request |
| `http.host` | `string` | Host identifier of the origin the request was sent to |
| `url.scheme` | `string` | HTTP Scheme of the request (`http`/`https`) |
| `url.full` | `string` | Full URL of the request |
| `user_agent.original` | `string` | User Agent used in the query |
| `http.client.request.duration` | `int` | The total request time for FGA requests |
| `http.server.request.duration` | `int` | The amount of time the FGA server took to internally process nd evaluate the request |
| Attribute Name | Type | Description |
|--------------------------------|----------|-------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `fga-client.response.model_id` | `string` | The authorization model ID that the FGA server used |
| `fga-client.request.method` | `string` | The FGA method/action that was performed (e.g. `Check`, `ListObjects`, ...) in TitleCase |
| `fga-client.request.store_id` | `string` | The store ID that was sent as part of the request |
| `fga-client.request.model_id` | `string` | The authorization model ID that was sent as part of the request, if any |
| `fga-client.request.client_id` | `string` | The client ID associated with the request, if any |
| `fga-client.user` | `string` | The user that is associated with the action of the request for check and list objects |
| `http.request.resend_count` | `int` | The number of retries attempted (Only sent if the request was retried. Count of `1` means the request was retried once in addition to the original request) |
| `http.response.status_code` | `int` | The status code of the response |
| `http.request.method` | `string` | The HTTP method for the request |
| `http.host` | `string` | Host identifier of the origin the request was sent to |
| `url.scheme` | `string` | HTTP Scheme of the request (`http`/`https`) |
| `url.full` | `string` | Full URL of the request |
| `user_agent.original` | `string` | User Agent used in the query |
| `http.client.request.duration` | `int` | The total request time for FGA requests |
| `http.server.request.duration` | `int` | The amount of time the FGA server took to internally process nd evaluate the request |

## Configuration

Expand Down
10 changes: 5 additions & 5 deletions src/OpenFga.Sdk/ApiClient/ApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,19 +134,19 @@ await _baseClient.SendRequestAsync<TReq, object>(requestBuilder, additionalHeade
}

private async Task<ResponseWrapper<TResult>> Retry<TResult>(Func<Task<ResponseWrapper<TResult>>> retryable) {
var numRetries = 0;
var requestCount = 0;
while (true) {
try {
numRetries++;
requestCount++;

var response = await retryable();

response.retryCount = numRetries;
response.retryCount = requestCount - 1; // OTEL spec specifies that the original request is not included in the count

return response;
}
catch (FgaApiRateLimitExceededError err) {
if (numRetries > _configuration.MaxRetry) {
if (requestCount > _configuration.MaxRetry) {
throw;
}

Expand All @@ -157,7 +157,7 @@ private async Task<ResponseWrapper<TResult>> Retry<TResult>(Func<Task<ResponseWr
await Task.Delay(waitInMs);
}
catch (FgaApiError err) {
if (!err.ShouldRetry || numRetries > _configuration.MaxRetry) {
if (!err.ShouldRetry || requestCount > _configuration.MaxRetry) {
throw;
}

Expand Down
37 changes: 20 additions & 17 deletions src/OpenFga.Sdk/Telemetry/Attributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public class Attributes {
// User Agent used in the query
private const string AttributeHttpUserAgent = "user_agent.original";

// The number of retries attempted (starting from 1 for the original request)
// The number of retries attempted (Only sent if the request was retried. Count of `1` means the request was retried once in addition to the original request)
private const string AttributeRequestRetryCount = "http.request.resend_count";

private static string? GetHeaderValueIfValid(HttpResponseHeaders headers, string headerName) {
Expand Down Expand Up @@ -141,26 +141,26 @@ public static TagList buildAttributesForResponse<T>(string apiName,

switch (apiName) {
case "Check": {
var tupleKey = apiRequest!["tuple_key"]!;
var fgaUser = (string)tupleKey!["user"]!;
var tupleKey = apiRequest!["tuple_key"]!;
var fgaUser = (string)tupleKey!["user"]!;

if (!string.IsNullOrEmpty(fgaUser)) {
attributes.Add(new KeyValuePair<string, object?>(AttributeFgaRequestUser,
fgaUser));
}

break;
if (!string.IsNullOrEmpty(fgaUser)) {
attributes.Add(new KeyValuePair<string, object?>(AttributeFgaRequestUser,
fgaUser));
}
case "ListObjects": {
var fgaUser = (string)apiRequest!["user"]!;

if (!string.IsNullOrEmpty(fgaUser)) {
attributes.Add(new KeyValuePair<string, object?>(AttributeFgaRequestUser,
fgaUser));
}
break;
}
case "ListObjects": {
var fgaUser = (string)apiRequest!["user"]!;

break;
if (!string.IsNullOrEmpty(fgaUser)) {
attributes.Add(new KeyValuePair<string, object?>(AttributeFgaRequestUser,
fgaUser));
}

break;
}
}
}
}
Expand Down Expand Up @@ -218,7 +218,10 @@ public static TagList buildAttributesForResponse<T>(string apiName,
attributes.Add(new KeyValuePair<string, object?>(AttributeHttpClientRequestDuration,
requestDuration.ElapsedMilliseconds));

attributes.Add(new KeyValuePair<string, object?>(AttributeRequestRetryCount, retryCount));
// OTEL specifies that this value should be conditionally sent if a retry occurred
if (retryCount > 0) {
attributes.Add(new KeyValuePair<string, object?>(AttributeRequestRetryCount, retryCount));
}

return attributes;
}
Expand Down

0 comments on commit 88a083e

Please sign in to comment.