-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added OpenTelemetryTraceId, and Request Response middleware
- Loading branch information
1 parent
48e39fe
commit 88f1e11
Showing
15 changed files
with
110 additions
and
40 deletions.
There are no files selected for viewing
This file contains 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 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
19 changes: 19 additions & 0 deletions
19
src/BuildingBlocks/Common.OpenTelemetry/Middlewares/OpenTelemetryTraceIdMiddleware.cs
This file contains 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,19 @@ | ||
using System.Diagnostics; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Common.OpenTelemetry.Middlewares; | ||
|
||
public class OpenTelemetryTraceIdMiddleware(RequestDelegate next) | ||
{ | ||
public async Task InvokeAsync(HttpContext context) | ||
{ | ||
var logger = context.RequestServices.GetRequiredService<ILogger<OpenTelemetryTraceIdMiddleware>>(); | ||
var traceId = Activity.Current?.TraceId.ToString(); | ||
using (logger.BeginScope("{@OpenTelemetryTraceId}", traceId)) | ||
{ | ||
await next(context); | ||
} | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/BuildingBlocks/Common.OpenTelemetry/Middlewares/RequestAndResponseActivityMiddleware.cs
This file contains 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,44 @@ | ||
using System.Diagnostics; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.IO; | ||
|
||
namespace Common.OpenTelemetry.Middlewares; | ||
|
||
public class RequestAndResponseActivityMiddleware(RequestDelegate next) | ||
{ | ||
private readonly RecyclableMemoryStreamManager _recyclableMemoryStreamManager = new(); | ||
|
||
public async Task InvokeAsync(HttpContext context) | ||
{ | ||
await AddRequestBodyContentToActivityTags(context); | ||
await AddResponseBodyContentToActivityTags(context); | ||
} | ||
|
||
private async Task AddRequestBodyContentToActivityTags(HttpContext context) | ||
{ | ||
context.Request.EnableBuffering(); | ||
var requestBodyStreamReader = new StreamReader(context.Request.Body); | ||
var requestBodyContent = await requestBodyStreamReader.ReadToEndAsync(); | ||
|
||
Activity.Current?.SetTag("http.request.body", requestBodyContent); | ||
context.Request.Body.Position = 0; | ||
} | ||
private async Task AddResponseBodyContentToActivityTags(HttpContext context) | ||
{ | ||
var originalResponse = context.Response.Body; | ||
await using var responseBodyMemoryStream = _recyclableMemoryStreamManager.GetStream(); | ||
context.Response.Body = responseBodyMemoryStream; | ||
|
||
await next(context); | ||
|
||
responseBodyMemoryStream.Position = 0; | ||
|
||
var responseBodyStreamReader = new StreamReader(responseBodyMemoryStream); | ||
var responseBodyContent = await responseBodyStreamReader.ReadToEndAsync(); | ||
Activity.Current?.SetTag("http.response.body", responseBodyContent); | ||
|
||
responseBodyMemoryStream.Position = 0; | ||
|
||
await responseBodyMemoryStream.CopyToAsync(originalResponse); | ||
} | ||
} |
This file contains 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 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 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 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 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 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 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 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 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 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 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