有沒有辦法將 Azure App Insights 與 Hotchoclate graphql 框架集成?目前,有多種方法可以將其組合在一起。
像使用 REST api 一樣,通過應用洞察中的查詢將所有內容放入應用洞察中的最佳方法是什么?
uj5u.com熱心網友回復:
在 HC 中,您需要連接到診斷事件偵聽器以獲取管道的句柄,然后您可以從那里連接到各種事件并記錄遙測。這里的關鍵是確保您將查詢與查詢名稱區分開來,以便它在應用程式洞察中正確顯示,否則一切都將在 /graphql 端點上
public class AppInsightsDiagnosticEventListener : ExecutionDiagnosticEventListener
{
private readonly TelemetryClient _telemetryClient;
public AppInsightsDiagnosticEventListener(TelemetryClient telemetryClient) => _telemetryClient = telemetryClient;
public override IDisposable ExecuteRequest(IRequestContext context)
{
var httpContext = GetHttpContextFrom(context);
if (httpContext == null)
return EmptyScope;
//During debugging every playground action will come here so we want this while debugging
#if DEBUG
if (context.Request.OperationName == "IntrospectionQuery")
return EmptyScope;
#endif
//Create a new telemetry request
var operationPath = $"{context.Request.OperationName ?? "UnknownOperation"} - {context.Request.QueryHash}";
var requestTelemetry = new RequestTelemetry()
{
Name = $"/graphql{operationPath}",
Url = new Uri(httpContext.Request.GetUri().AbsoluteUri operationPath),
};
requestTelemetry.Context.Operation.Name = $"POST /graphql/{operationPath}";
requestTelemetry.Context.Operation.Id = GetOperationIdFrom(httpContext);
requestTelemetry.Context.Operation.ParentId = GetOperationIdFrom(httpContext);
requestTelemetry.Context.User.AuthenticatedUserId = httpContext.User.Identity?.Name ?? "Not authenticated";
if (context.Request.Query != null)
requestTelemetry.Properties.Add("GraphQL Query", context.Request.Query.ToString());
var operation = _telemetryClient.StartOperation(requestTelemetry);
return new ScopeWithEndAction(() => OnEndRequest(context, operation));
}
private void OnEndRequest(IRequestContext context, IOperationHolder<RequestTelemetry> operation)
{
var httpContext = GetHttpContextFrom(context);
operation.Telemetry.Success = httpContext.Response.StatusCode is >= 200 and <= 299;
operation.Telemetry.ResponseCode = httpContext.Response.StatusCode.ToString();
if (context.Exception != null)
{
operation.Telemetry.Success = false;
operation.Telemetry.ResponseCode = "500";
_telemetryClient.TrackException(context.Exception);
}
if (context.ValidationResult?.HasErrors ?? false)
{
operation.Telemetry.Success = false;
operation.Telemetry.ResponseCode = "400";
}
if (context.Result?.Errors != null)
{
foreach (var error in context.Result.Errors)
{
if (error.Exception != null)
{
operation.Telemetry.Success = false;
_telemetryClient.TrackException(error.Exception);
}
}
}
_telemetryClient.StopOperation(operation);
}
public override void RequestError(IRequestContext context, Exception exception)
{
_telemetryClient.TrackException(exception);
base.RequestError(context, exception);
}
public override void ValidationErrors(IRequestContext context, IReadOnlyList<IError> errors)
{
foreach (var error in errors)
{
_telemetryClient.TrackTrace("GraphQL validation error: " error.Message, SeverityLevel.Warning);
}
base.ValidationErrors(context, errors);
}
private HttpContext GetHttpContextFrom(IRequestContext context)
{
// This method is used to enable start/stop events for query.
if (!context.ContextData.ContainsKey("HttpContext"))
return null;
return context.ContextData["HttpContext"] as HttpContext;
}
private string GetOperationIdFrom(HttpContext context) => context.TraceIdentifier;
}
internal class ScopeWithEndAction : IDisposable
{
private readonly Action _disposeAction;
public ScopeWithEndAction(Action disposeAction) => _disposeAction = disposeAction;
public void Dispose() => _disposeAction.Invoke();
}
并且在啟動時
services.AddApplicationInsightsTelemetry();
services.AddGraphQLServer()
.AddDiagnosticEventListener<AppInsightsDiagnosticEventListener>((sp) => new AppInsightsDiagnosticEventListener(sp.GetService<TelemetryClient>()));
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/371695.html
標籤:。网 天蓝色 azure-application-insights 热可可
上一篇:在AzureFunction中運行控制臺應用程式的program.cs檔案?
下一篇:如何在ReactNativeyoutubeiframe中切換到全屏(點擊YouTube播放器gui上的全屏按鈕時旋轉到橫向)?
