-
Notifications
You must be signed in to change notification settings - Fork 285
2554 Enhance REST OTEL instrumentation with custom metrics and traces #2617
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
RubenCerna2079
merged 45 commits into
Azure:main
from
tommasodotNET:features/2554-enh-otel
Apr 23, 2025
Merged
Changes from 43 commits
Commits
Show all changes
45 commits
Select commit
Hold shift + click to select a range
343e62c
initialize custom metrics/traces for REST endpoint
tommasodotNET f807727
initialize custom traces on sql db
tommasodotNET ef47e17
traces refactor
tommasodotNET 6b2d1f7
fixes otel logging
tommasodotNET 970360c
fixes otel logging resource name
tommasodotNET 2199d68
cleans up pr
tommasodotNET 0176cfd
Update src/Service/Controllers/RestController.cs
tommasodotNET 1393def
Update src/Service/Controllers/RestController.cs
tommasodotNET 5db6e92
adding null check on route
tommasodotNET 5a4848b
Merge branch 'features/2554-enh-otel' of github.com:tommasodotNET/dat…
tommasodotNET fc44a21
Update src/Service/Controllers/RestController.cs
tommasodotNET e5ff447
Update src/Service/Controllers/RestController.cs
tommasodotNET 1bb648a
Update src/Service/Telemetry/TelemetryTracesHelper.cs
tommasodotNET eb23e32
Update src/Service/Telemetry/TelemetryTracesHelper.cs
tommasodotNET 0d07c55
Update src/Service/Telemetry/TelemetryTracesHelper.cs
tommasodotNET 5859684
Update src/Service/Telemetry/TelemetryTracesHelper.cs
tommasodotNET a87f54d
adds docs in TelemetryMetricsHelper
tommasodotNET 4c3e8e9
adds doc on TelemetryTracesHelper
tommasodotNET ebea19d
adds check queryString is not null
tommasodotNET 2e3f011
adds comments on activities
tommasodotNET 96deeaf
removes unnecessary usings in program.cs
tommasodotNET 0dd2a92
fixes missing meter name
tommasodotNET 3e80105
uses updowncounter for active requests
tommasodotNET 7ff76ce
removes check on route
tommasodotNET 9bc5822
Update src/Service/Telemetry/TelemetryTracesHelper.cs
tommasodotNET 0cad4a8
Update src/Service/Controllers/RestController.cs
tommasodotNET ba993e3
Merge branch 'main' into features/2554-enh-otel
RubenCerna2079 1f66f8f
Merge branch 'main' into features/2554-enh-otel
RubenCerna2079 c6414b6
checks route split
tommasodotNET 8e5d481
Merge branch 'features/2554-enh-otel' of github.com:tommasodotNET/dat…
tommasodotNET 30c61ca
removes activity disposal
tommasodotNET 045e5fa
fixes typo on request finished with exception tracking
tommasodotNET f8a4b20
Update src/Service/Controllers/RestController.cs
tommasodotNET 18dd1f3
handle userRole with X-MS-API-ROLE and add check for nullability
tommasodotNET d0065c8
Merge branch 'features/2554-enh-otel' of github.com:tommasodotNET/dat…
tommasodotNET d2a9cff
fixes logs
tommasodotNET cf1b821
removes commented otel logs
tommasodotNET b5d3f7b
adds asp net core base logging
tommasodotNET e0f06a8
Merge branch 'main' into features/2554-enh-otel
aaronburtle 0a21b84
Changes to Tomaso Branch
RubenCerna2079 3d09aca
Fix Unit Test errors
RubenCerna2079 d674495
Merge branch 'main' into features/2554-enh-otel
RubenCerna2079 de36de5
Fixed Unit Test Failure
RubenCerna2079 be44074
Merge branch 'main' into features/2554-enh-otel
Aniruddh25 8780c39
Changes based on comments
RubenCerna2079 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Diagnostics.Metrics; | ||
| using System.Net; | ||
| using Azure.DataApiBuilder.Config.ObjectModel; | ||
| using Kestral = Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpMethod; | ||
|
|
||
| namespace Azure.DataApiBuilder.Service.Telemetry | ||
| { | ||
| /// <summary> | ||
| /// Helper class for tracking telemetry metrics such as active requests, errors, total requests, | ||
| /// and request durations using the .NET Meter and Counter APIs. | ||
| /// </summary> | ||
| public static class TelemetryMetricsHelper | ||
| { | ||
| public static readonly string MeterName = "DataApiBuilder.Metrics"; | ||
| private static readonly Meter _meter = new(MeterName); | ||
| private static readonly UpDownCounter<long> _activeRequests = _meter.CreateUpDownCounter<long>("active_requests"); | ||
| private static readonly Counter<long> _errorCounter = _meter.CreateCounter<long>("total_errors"); | ||
| private static readonly Counter<long> _totalRequests = _meter.CreateCounter<long>("total_requests"); | ||
| private static readonly Histogram<double> _requestDuration = _meter.CreateHistogram<double>("request_duration", "ms"); | ||
|
|
||
| public static void IncrementActiveRequests(ApiType kind) => _activeRequests.Add(1, new KeyValuePair<string, object?>("api_type", kind)); | ||
|
|
||
| public static void DecrementActiveRequests(ApiType kind) => _activeRequests.Add(-1, new KeyValuePair<string, object?>("api_type", kind)); | ||
|
|
||
| /// <summary> | ||
| /// Tracks a request by incrementing the total requests counter and associating it with metadata. | ||
| /// </summary> | ||
| /// <param name="method">The HTTP method of the request (e.g., GET, POST).</param> | ||
| /// <param name="statusCode">The HTTP status code of the response.</param> | ||
| /// <param name="endpoint">The endpoint being accessed.</param> | ||
| /// <param name="apiType">The type of API being used (e.g., REST, GraphQL).</param> | ||
| public static void TrackRequest(Kestral method, HttpStatusCode statusCode, string endpoint, ApiType apiType) | ||
| { | ||
| _totalRequests.Add(1, | ||
| new("method", method), | ||
| new("status_code", statusCode), | ||
| new("endpoint", endpoint), | ||
| new("api_type", apiType)); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Tracks an error by incrementing the error counter and associating it with metadata. | ||
| /// </summary> | ||
| /// <param name="method">The HTTP method of the request (e.g., GET, POST).</param> | ||
| /// <param name="statusCode">The HTTP status code of the response.</param> | ||
| /// <param name="endpoint">The endpoint being accessed.</param> | ||
| /// <param name="apiType">The type of API being used (e.g., REST, GraphQL).</param> | ||
| /// <param name="ex">The exception that occurred.</param> | ||
| public static void TrackError(Kestral method, HttpStatusCode statusCode, string endpoint, ApiType apiType, Exception ex) | ||
| { | ||
| _errorCounter.Add(1, | ||
| new("method", method), | ||
| new("status_code", statusCode), | ||
| new("endpoint", endpoint), | ||
| new("api_type", apiType), | ||
| new("error_type", ex.GetType().Name)); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Tracks the duration of a request by recording it in a histogram and associating it with metadata. | ||
| /// </summary> | ||
| /// <param name="method">The HTTP method of the request (e.g., GET, POST).</param> | ||
| /// <param name="statusCode">The HTTP status code of the response.</param> | ||
| /// <param name="endpoint">The endpoint being accessed.</param> | ||
| /// <param name="apiType">The type of API being used (e.g., REST, GraphQL).</param> | ||
| /// <param name="duration">The duration of the request in milliseconds.</param> | ||
| public static void TrackRequestDuration(Kestral method, HttpStatusCode statusCode, string endpoint, ApiType apiType, TimeSpan duration) | ||
| { | ||
| _requestDuration.Record(duration.TotalMilliseconds, | ||
| new("method", method), | ||
| new("status_code", statusCode), | ||
| new("endpoint", endpoint), | ||
| new("api_type", apiType)); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.