Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(dotnet): use JsonDocument to parse body for attributes in metrics
Browse files Browse the repository at this point in the history
rhamzeh committed Jul 30, 2024
1 parent 4b2f900 commit 6cb55a1
Showing 1 changed file with 27 additions and 31 deletions.
58 changes: 27 additions & 31 deletions config/clients/dotnet/template/Telemetry/Attributes.cs.mustache
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ using {{packageName}}.ApiClient;
using {{packageName}}.Configuration;
using System.Diagnostics;
using System.Net.Http.Headers;
using System.Text.Json.Nodes;
using System.Text.Json;

namespace {{packageName}}.Telemetry;

@@ -116,39 +116,35 @@ public class Attributes {
else if (apiName is "Check" or "ListObjects" or "Write" or "Expand" or "ListUsers") {
try {
if (requestBuilder.JsonBody != null) {
var apiRequest = JsonNode.Parse(requestBuilder.JsonBody!)!;
try {
var authModelId = (string)apiRequest!["authorization_model_id"]!;
if (!string.IsNullOrEmpty(authModelId)) {
attributes.Add(new KeyValuePair<string, object?>(AttributeRequestModelId,
authModelId));
}
}
catch { }

switch (apiName) {
case "Check": {
var tupleKey = apiRequest!["tuple_key"]!;
var fgaUser = (string)tupleKey!["user"]!;
if (!string.IsNullOrEmpty(fgaUser)) {
attributes.Add(new KeyValuePair<string, object?>(AttributeFgaRequestUser,
fgaUser));
using (var document = JsonDocument.Parse(requestBuilder.JsonBody)) {
var root = document.RootElement;
if (root.TryGetProperty("authorization_model_id", out var authModelId)) {
if (!string.IsNullOrEmpty(authModelId.GetString())) {
attributes.Add(new KeyValuePair<string, object?>(AttributeRequestModelId,
authModelId.GetString()));
}

break;
}
case "ListObjects": {
var fgaUser = (string)apiRequest!["user"]!;
if (!string.IsNullOrEmpty(fgaUser)) {
attributes.Add(new KeyValuePair<string, object?>(AttributeFgaRequestUser,
fgaUser));
switch (apiName) {
case "Check": {
if (root.TryGetProperty("tuple_key", out var tupleKey) &&
tupleKey.TryGetProperty("user", out var fgaUser) &&
!string.IsNullOrEmpty(fgaUser.GetString())) {
attributes.Add(new KeyValuePair<string, object?>(AttributeFgaRequestUser,
fgaUser.GetString()));
}

break;
}
case "ListObjects": {
if (root.TryGetProperty("user", out var fgaUser) &&
!string.IsNullOrEmpty(fgaUser.GetString())) {
attributes.Add(new KeyValuePair<string, object?>(AttributeFgaRequestUser,
fgaUser.GetString()));
}

break;
}
}

break;
}
}
}

0 comments on commit 6cb55a1

Please sign in to comment.