Skip to content

Commit a375425

Browse files
committed
Include client and endpoint in diagnostics
1 parent 89dc1f9 commit a375425

4 files changed

Lines changed: 168 additions & 58 deletions

File tree

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -395,9 +395,10 @@ Common activity tags:
395395
| `hashgate.auth.scheme` | Authentication scheme name |
396396
| `hashgate.auth.result` | `success` or `failure` |
397397
| `hashgate.auth.failure_reason` | Failure reason, such as `invalid_signature`, `invalid_timestamp`, `invalid_content_hash`, `missing_required_signed_headers`, `too_many_signed_headers`, `replayed_signature`, or `authentication_error` |
398+
| `hashgate.client` | HMAC client identifier for grouping API usage by client |
399+
| `hashgate.endpoint` | Endpoint display name or request path for grouping API usage by endpoint |
398400
| `hashgate.replay_protection.enabled` | Whether replay protection is enabled for the scheme |
399401
| `hashgate.replay_protection.result` | Replay protection outcome: `new`, `replay`, or `not_configured` |
400-
| `hashgate.hmac.client` | HMAC client identifier from the request |
401402
| `hashgate.hmac.signed_headers.count` | Number of headers included in the signature |
402403
| `hashgate.rate_limit.policy` | Resolved HashGate rate limit policy name |
403404
| `hashgate.rate_limit.client` | Client or remote IP used for the rate limit partition |
@@ -422,7 +423,7 @@ Authentication activities also add `hashgate.content_hash.validated` when the re
422423
| `hashgate.rate_limit.provider.lookup` | Counter | `{lookup}` | Number of per-client rate limit provider lookups |
423424
| `hashgate.rate_limit.provider.miss` | Counter | `{miss}` | Number of per-client rate limit provider lookups that fell back to default limits |
424425

425-
Authentication metrics include `hashgate.auth.scheme`, `hashgate.auth.result`, and, for failures, `hashgate.auth.failure_reason`. Replay protection metrics include `hashgate.replay_protection.result`. Rate limit metrics include `hashgate.rate_limit.policy`; endpoint request metrics also include `hashgate.hmac.client`; provider lookup metrics also include `hashgate.rate_limit.provider_found`.
426+
Authentication metrics include `hashgate.auth.scheme`, `hashgate.auth.result`, `hashgate.client`, `hashgate.endpoint`, and, for failures, `hashgate.auth.failure_reason`. Use `hashgate.auth.requests` grouped by `hashgate.client` and `hashgate.endpoint` to track which clients are using which API endpoints. Replay protection metrics include `hashgate.replay_protection.result`. Rate limit metrics include `hashgate.rate_limit.policy`; endpoint request metrics also include `hashgate.client` and `hashgate.endpoint`; provider lookup metrics also include `hashgate.rate_limit.provider_found`.
426427

427428
### Custom Key Provider
428429

src/HashGate.AspNetCore/HashGateDiagnostics.cs

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public static class HashGateDiagnostics
1313
/// <summary>
1414
/// The name of the activity source used for HashGate ASP.NET Core tracing.
1515
/// </summary>
16-
public const string ActivitySourceName = "HashGate.AspNetCore";
16+
public const string SourceName = "HashGate.AspNetCore";
1717

1818
/// <summary>
1919
/// The name of the meter used for HashGate ASP.NET Core metrics.
@@ -91,11 +91,6 @@ public static class HashGateDiagnostics
9191
/// </summary>
9292
public const string ReplayProtectionResultTagName = "hashgate.replay_protection.result";
9393

94-
/// <summary>
95-
/// The tag name for the HMAC client identifier.
96-
/// </summary>
97-
public const string HmacClientTagName = "hashgate.hmac.client";
98-
9994
/// <summary>
10095
/// The tag name for the count of signed HMAC headers.
10196
/// </summary>
@@ -156,7 +151,8 @@ public static class HashGateDiagnostics
156151
/// </summary>
157152
public const string ClientTagName = "hashgate.client";
158153

159-
internal static readonly ActivitySource ActivitySource = new(ActivitySourceName, ThisAssembly.FileVersion);
154+
155+
internal static readonly ActivitySource ActivitySource = new(SourceName, ThisAssembly.FileVersion);
160156
internal static readonly Meter Meter = new(MeterName, ThisAssembly.FileVersion);
161157

162158
internal static readonly Counter<long> AuthenticationRequests = Meter.CreateCounter<long>(
@@ -208,7 +204,13 @@ public static class HashGateDiagnostics
208204
description: "Number of per-client rate limit provider lookups that fell back to default limits.");
209205

210206

211-
internal static void RecordAuthentication(string scheme, string result, string? failureReason, long elapsedTicks)
207+
internal static void RecordAuthentication(
208+
string scheme,
209+
string result,
210+
string? failureReason,
211+
long elapsedTicks,
212+
string? endpoint,
213+
string? client)
212214
{
213215
if (!AuthenticationRequests.Enabled &&
214216
!AuthenticationFailures.Enabled &&
@@ -217,26 +219,28 @@ internal static void RecordAuthentication(string scheme, string result, string?
217219
return;
218220
}
219221

220-
KeyValuePair<string, object?>[] tags = failureReason is null
221-
?
222-
[
223-
new(AuthenticationSchemeTagName, scheme),
224-
new(AuthenticationResultTagName, result)
225-
]
226-
:
227-
[
228-
new(AuthenticationSchemeTagName, scheme),
229-
new(AuthenticationResultTagName, result),
230-
new(AuthenticationFailureReasonTagName, failureReason)
231-
];
232-
233-
AuthenticationRequests.Add(1, tags);
222+
TagList tags =
223+
[
224+
new(AuthenticationSchemeTagName, scheme),
225+
new(AuthenticationResultTagName, result)
226+
];
227+
228+
if (failureReason is not null)
229+
tags.Add(new(AuthenticationFailureReasonTagName, failureReason));
230+
231+
if (!string.IsNullOrWhiteSpace(endpoint))
232+
tags.Add(new(EndpointTagName, endpoint));
233+
234+
if (!string.IsNullOrWhiteSpace(client))
235+
tags.Add(new(ClientTagName, client));
236+
237+
AuthenticationRequests.Add(1, in tags);
234238

235239
if (failureReason is not null)
236-
AuthenticationFailures.Add(1, tags);
240+
AuthenticationFailures.Add(1, in tags);
237241

238242
var elapsed = GetElapsedMilliseconds(elapsedTicks);
239-
AuthenticationDuration.Record(elapsed, tags);
243+
AuthenticationDuration.Record(elapsed, in tags);
240244
}
241245

242246
internal static void RecordReplayProtectionCheck(string result, double ttlMilliseconds)
@@ -247,43 +251,43 @@ internal static void RecordReplayProtectionCheck(string result, double ttlMillis
247251
return;
248252
}
249253

250-
KeyValuePair<string, object?>[] tags =
254+
TagList tags =
251255
[
252256
new(ReplayProtectionResultTagName, result)
253257
];
254258

255-
ReplayProtectionChecks.Add(1, tags);
259+
ReplayProtectionChecks.Add(1, in tags);
256260

257261
if (result == "replay")
258-
ReplayProtectionReplays.Add(1, tags);
262+
ReplayProtectionReplays.Add(1, in tags);
259263
}
260264

261265
internal static void RecordEndpointRequest(string policy, string endpoint, string client)
262266
{
263267
if (!EndpointRequests.Enabled)
264268
return;
265269

266-
KeyValuePair<string, object?>[] tags =
270+
TagList tags =
267271
[
268272
new(RateLimitPolicyTagName, policy),
269273
new(EndpointTagName, endpoint),
270274
new(ClientTagName, client)
271275
];
272276

273-
EndpointRequests.Add(1, tags);
277+
EndpointRequests.Add(1, in tags);
274278
}
275279

276280
internal static void RecordRateLimitRejection(string policy, double retryAfterMilliseconds)
277281
{
278282
if (!RateLimitRejections.Enabled)
279283
return;
280284

281-
KeyValuePair<string, object?>[] tags =
285+
TagList tags =
282286
[
283287
new(RateLimitPolicyTagName, policy)
284288
];
285289

286-
RateLimitRejections.Add(1, tags);
290+
RateLimitRejections.Add(1, in tags);
287291
}
288292

289293
internal static void RecordRateLimitProviderLookup(string policy, bool found)
@@ -294,16 +298,16 @@ internal static void RecordRateLimitProviderLookup(string policy, bool found)
294298
return;
295299
}
296300

297-
KeyValuePair<string, object?>[] tags =
301+
TagList tags =
298302
[
299303
new(RateLimitPolicyTagName, policy),
300304
new(RateLimitProviderFoundTagName, found)
301305
];
302306

303-
RateLimitProviderLookups.Add(1, tags);
307+
RateLimitProviderLookups.Add(1, in tags);
304308

305309
if (!found)
306-
RateLimitProviderMisses.Add(1, tags);
310+
RateLimitProviderMisses.Add(1, in tags);
307311
}
308312

309313
private static double GetElapsedMilliseconds(long startTimestamp)

0 commit comments

Comments
 (0)