-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tsc-opentelemetry): add new span attributes (#681)
* feat: add new attributes: user_agent、authorization,client_ip and response body * feat: add httpReponse middleware * fix: update
- Loading branch information
1 parent
0cd1946
commit ad9dc58
Showing
7 changed files
with
114 additions
and
11 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
37 changes: 37 additions & 0 deletions
37
...b/StackSdks/Masa.Contrib.StackSdks.Tsc.OpenTelemetry/Middleware/HttpResponseMiddleware.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,37 @@ | ||
// Copyright (c) MASA Stack All rights reserved. | ||
// Licensed under the MIT License. See LICENSE.txt in the project root for license information. | ||
|
||
namespace Masa.Contrib.StackSdks.Tsc.OpenTelemetry; | ||
|
||
internal class HttpResponseMiddleware | ||
{ | ||
private readonly RequestDelegate _next; | ||
|
||
public HttpResponseMiddleware(RequestDelegate next) | ||
{ | ||
_next = next; | ||
} | ||
|
||
public async Task InvokeAsync(HttpContext httpContext) | ||
{ | ||
var httpResponse = httpContext.Response; | ||
using var ms = new MemoryStream(); | ||
var rawStream = httpResponse.Body; | ||
httpResponse.Body = ms; | ||
await _next(httpContext); | ||
ms.Seek(0, SeekOrigin.Begin); | ||
var responseResult = new StreamReader(ms).ReadToEnd(); | ||
ms.Seek(0, SeekOrigin.Begin); | ||
ms.CopyTo(rawStream); | ||
httpResponse.Body = rawStream; | ||
|
||
if (httpResponse.StatusCode - 299 == 0 || httpResponse.StatusCode - 500 >= 0) | ||
{ | ||
Activity.Current?.SetTag(OpenTelemetryAttributeName.Http.RESPONSE_CONTENT_BODY, responseResult); | ||
} | ||
else | ||
{ | ||
OpenTelemetryInstrumentationOptions.Logger.LogInformation("response length: {length}, context: {context}", responseResult.Length, responseResult); | ||
} | ||
} | ||
} |
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