Skip to content

Commit

Permalink
調整 plugin 內容
Browse files Browse the repository at this point in the history
  • Loading branch information
YuChia-Wei committed Nov 1, 2023
1 parent 634892f commit 7932e5e
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System.Diagnostics;
using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.Http;
using OpenTelemetry.Trace;

namespace OpenTelemetry.AutoInstrumentation.AspNetCore.Plugins;

public static class ActivitySourceExtenstion
{
public static Action<Activity, HttpResponse> EnrichHttpResponse()
{
return (activity, response) =>
{
var exceptionHandlerPathFeature = response.HttpContext.Features.Get<IExceptionHandlerPathFeature>();
if (exceptionHandlerPathFeature == null)
{
return;
}

var exception = exceptionHandlerPathFeature.Error;
activity.SetStatus(ActivityStatusCode.Error, exception.Message);
activity.RecordException(exception);
};
}

public static Action<Activity, HttpRequest> EnrichHttpRequest()
{
return (activity, request) =>
{
// 記錄使用者 IP
var clientIp = request.Headers["X-Forwarded-For"].FirstOrDefault();

if (string.IsNullOrWhiteSpace(clientIp))
{
var remoteIp = request.HttpContext.Connection.RemoteIpAddress;

if (remoteIp != null)
{
clientIp = remoteIp.MapToIPv4().ToString();

if (clientIp.StartsWith("::ffff:", StringComparison.OrdinalIgnoreCase))
{
clientIp = clientIp.ToLower().Replace("::ffff:", "");
}
}
}

activity.SetTag("http.client.ip", clientIp);
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,28 +46,12 @@ public void ConfigureTracesOptions(AspNetCoreInstrumentationOptions options)
options.Filter = context => HttpRequestUserAgentChecker.IsValidUser(context.Request.Headers.UserAgent);

// via. https://github.com/open-telemetry/opentelemetry-dotnet/tree/main/src/OpenTelemetry.Instrumentation.AspNetCore#enrich
options.EnrichWithHttpRequest = (activity, request) =>
{
// 記錄使用者 IP
var clientIp = request.Headers["X-Forwarded-For"].FirstOrDefault();

if (string.IsNullOrWhiteSpace(clientIp))
{
var remoteIp = request.HttpContext.Connection.RemoteIpAddress;

if (remoteIp != null)
{
clientIp = remoteIp.MapToIPv4().ToString();

if (clientIp.StartsWith("::ffff:", StringComparison.OrdinalIgnoreCase))
{
clientIp = clientIp.ToLower().Replace("::ffff:", "");
}
}
}
options.EnrichWithHttpRequest = ActivitySourceExtenstion.EnrichHttpRequest();

activity.SetTag("http.client.ip", clientIp);
};
//如果在服務中有使用 UseExceptionHandling() 或是在 ActionFilter 的時候把 Exception 處理過的話,可能會導致 otel 無法正確輸出例外資料
//所以這邊另外檢查回應內容並另外打包給 AspNetCoreInstrumentation 工具
//TODO: 需要多測試效果
options.EnrichWithHttpResponse = ActivitySourceExtenstion.EnrichHttpResponse();
}

/// <summary>
Expand Down Expand Up @@ -103,4 +87,4 @@ public void ConfigureTracesOptions(SqlClientInstrumentationOptions options)
public void Initializing()
{
}
}
}

0 comments on commit 7932e5e

Please sign in to comment.