diff --git a/src/Elastic.Apm.GraphQL.HotChocolate/Extensions/ApmAgentExtensions.cs b/src/Elastic.Apm.GraphQL.HotChocolate/Extensions/ApmAgentExtensions.cs index 381f74f..22b8d27 100644 --- a/src/Elastic.Apm.GraphQL.HotChocolate/Extensions/ApmAgentExtensions.cs +++ b/src/Elastic.Apm.GraphQL.HotChocolate/Extensions/ApmAgentExtensions.cs @@ -3,7 +3,6 @@ using System.Diagnostics; using Elastic.Apm.Api; using IError = HotChocolate.IError; -using LogLevel = Elastic.Apm.Logging.LogLevel; namespace Elastic.Apm.GraphQL.HotChocolate { @@ -12,11 +11,11 @@ internal static class ApmAgentExtensions private static readonly string CaptureErrorFailed = $"{nameof(CaptureError)} failed."; private static readonly string CaptureExceptionFailed = $"{nameof(CaptureException)} failed."; - internal static void CaptureError(this IApmAgent apmAgent, IReadOnlyList errors) + internal static void CaptureError(this ITracer tracer, IReadOnlyList errors) { try { - IExecutionSegment? executionSegment = apmAgent.Tracer.GetExecutionSegment(); + IExecutionSegment? executionSegment = tracer.GetExecutionSegment(); if (executionSegment == null) return; foreach (IError error in errors) @@ -30,31 +29,26 @@ internal static void CaptureError(this IApmAgent apmAgent, IReadOnlyList { executionSegment.CaptureError(error.Message, path, Array.Empty()); } - - var message = $"{error.Message} {path}"; - apmAgent.Logger.Log(LogLevel.Error, message, default, LogFormatter.Nop); } } catch (Exception ex) { - apmAgent.Logger.Log(LogLevel.Error, CaptureErrorFailed, ex, LogFormatter.Nop); + tracer.CaptureErrorLog(new ErrorLog(CaptureErrorFailed), exception: ex); } } - internal static void CaptureException(this IApmAgent apmAgent, Exception exception) + internal static void CaptureException(this ITracer tracer, Exception exception) { try { - IExecutionSegment? executionSegment = apmAgent.Tracer.GetExecutionSegment(); + IExecutionSegment? executionSegment = tracer.GetExecutionSegment(); if (executionSegment == null) return; executionSegment.CaptureException(exception); - - apmAgent.Logger.Log(LogLevel.Error, exception.Message, default, LogFormatter.Nop); } catch (Exception ex) { - apmAgent.Logger.Log(LogLevel.Error, CaptureExceptionFailed, ex, LogFormatter.Nop); + tracer.CaptureErrorLog(new ErrorLog(CaptureExceptionFailed), exception: ex); } } } diff --git a/src/Elastic.Apm.GraphQL.HotChocolate/HotChocolateDiagnosticListener.cs b/src/Elastic.Apm.GraphQL.HotChocolate/HotChocolateDiagnosticListener.cs index fc53c17..3833300 100644 --- a/src/Elastic.Apm.GraphQL.HotChocolate/HotChocolateDiagnosticListener.cs +++ b/src/Elastic.Apm.GraphQL.HotChocolate/HotChocolateDiagnosticListener.cs @@ -2,7 +2,6 @@ using System.Collections.Generic; using Elastic.Apm.Api; using Elastic.Apm.Config; -using Elastic.Apm.Logging; using HotChocolate.Execution; using HotChocolate.Execution.Instrumentation; using HotChocolate.Language; @@ -16,33 +15,33 @@ internal class HotChocolateDiagnosticListener : ExecutionDiagnosticEventListener { private static readonly string ResolveFieldValueFailed = $"{nameof(ResolveFieldValue)} failed."; - private readonly IApmAgent _apmAgent; private readonly IConfigurationReader _configuration; private readonly HotChocolateDiagnosticOptions _options; - private readonly IApmLogger _apmLogger; internal HotChocolateDiagnosticListener( - IApmAgent apmAgent, IConfigurationReader configuration, HotChocolateDiagnosticOptions options) { - _apmAgent = apmAgent; _configuration = configuration; _options = options; - _apmLogger = _apmAgent.Logger; } public override IDisposable ExecuteRequest(IRequestContext context) { - ITransaction? transaction = _apmAgent.Tracer.CurrentTransaction; + if (!Agent.IsConfigured) + { + return EmptyScope; + } + + ITransaction? transaction = Agent.Tracer.CurrentTransaction; return transaction != null - ? new RequestActivityScope(context, transaction, _apmAgent, _options) + ? new RequestActivityScope(context, transaction, _options) : EmptyScope; } public override IDisposable ResolveFieldValue(IMiddlewareContext context) { - if (_configuration.LogLevel > LogLevel.Debug) + if (_configuration.LogLevel > LogLevel.Debug || !Agent.IsConfigured) { return EmptyScope; } @@ -53,7 +52,7 @@ public override IDisposable ResolveFieldValue(IMiddlewareContext context) context.Document.Definitions.Count == 1 && context.Document.Definitions[0] is OperationDefinitionNode { Name: { Value: "exec_batch" } }) { - IExecutionSegment? executionSegment = _apmAgent.Tracer.GetExecutionSegment(); + IExecutionSegment? executionSegment = Agent.Tracer.GetExecutionSegment(); if (executionSegment == null) return EmptyScope; @@ -65,7 +64,7 @@ public override IDisposable ResolveFieldValue(IMiddlewareContext context) } catch (Exception ex) { - _apmLogger.Log(LogLevel.Error, ResolveFieldValueFailed, ex, LogFormatter.Nop); + Agent.Tracer.CaptureErrorLog(new ErrorLog(ResolveFieldValueFailed), exception: ex); } return EmptyScope; @@ -73,12 +72,18 @@ public override IDisposable ResolveFieldValue(IMiddlewareContext context) public override void RequestError(IRequestContext context, Exception exception) { - _apmAgent.CaptureException(exception); + if (Agent.IsConfigured) + { + Agent.Tracer.CaptureException(exception); + } } public override void ValidationErrors(IRequestContext context, IReadOnlyList errors) { - _apmAgent.CaptureError(errors); + if (Agent.IsConfigured) + { + Agent.Tracer.CaptureError(errors); + } } } } diff --git a/src/Elastic.Apm.GraphQL.HotChocolate/RequestExecutorBuilderExtensions.cs b/src/Elastic.Apm.GraphQL.HotChocolate/RequestExecutorBuilderExtensions.cs index 8fed4e1..f683c69 100644 --- a/src/Elastic.Apm.GraphQL.HotChocolate/RequestExecutorBuilderExtensions.cs +++ b/src/Elastic.Apm.GraphQL.HotChocolate/RequestExecutorBuilderExtensions.cs @@ -6,7 +6,7 @@ namespace Elastic.Apm.GraphQL.HotChocolate { /// - /// Report diagnostic events to Elastic + /// Report diagnostic events to Elastic /// public static class RequestExecutorBuilderExtensions { @@ -24,9 +24,8 @@ public static IRequestExecutorBuilder AddObservability( return builder.AddDiagnosticEventListener(sp => { - IApmAgent apmAgent = sp.GetApplicationService(); IConfigurationReader configuration = sp.GetApplicationService(); - return new HotChocolateDiagnosticListener(apmAgent, configuration, options); + return new HotChocolateDiagnosticListener(configuration, options); }); } } diff --git a/src/Elastic.Apm.GraphQL.HotChocolate/Scopes/RequestActivityScope.cs b/src/Elastic.Apm.GraphQL.HotChocolate/Scopes/RequestActivityScope.cs index 5e2c5a2..66ac195 100644 --- a/src/Elastic.Apm.GraphQL.HotChocolate/Scopes/RequestActivityScope.cs +++ b/src/Elastic.Apm.GraphQL.HotChocolate/Scopes/RequestActivityScope.cs @@ -3,7 +3,6 @@ using System.Linq; using System.Text; using Elastic.Apm.Api; -using Elastic.Apm.Logging; using HotChocolate.Execution; using HotChocolate.Execution.Processing; using IError = HotChocolate.IError; @@ -16,19 +15,16 @@ internal class RequestActivityScope : IDisposable private readonly IRequestContext _context; private readonly ITransaction _transaction; - private readonly IApmAgent _apmAgent; private readonly HotChocolateDiagnosticOptions _options; private bool _disposed; internal RequestActivityScope( IRequestContext context, ITransaction transaction, - IApmAgent apmAgent, HotChocolateDiagnosticOptions options) { _context = context; _transaction = transaction; - _apmAgent = apmAgent; _options = options; } @@ -64,19 +60,19 @@ private void EnrichTransaction() if (_context.Result.HasErrors(out IReadOnlyList? errors)) { - _apmAgent.CaptureError(errors); + Agent.Tracer.CaptureError(errors); } if (_context.HasException(out Exception? exception)) { - _apmAgent.CaptureException(exception); + Agent.Tracer.CaptureException(exception); } _options.Enrich?.Invoke(_transaction, operationDetails); } catch (Exception ex) { - _apmAgent.Logger.Log(LogLevel.Error, ExecuteRequestFailed, ex, LogFormatter.Nop); + Agent.Tracer.CaptureErrorLog(new ErrorLog(ExecuteRequestFailed), exception: ex); } } diff --git a/src/Elastic.Apm.Internals/CompositeDisposable.cs b/src/Elastic.Apm.Internals/CompositeDisposable.cs index d8e01f6..f2512e6 100644 --- a/src/Elastic.Apm.Internals/CompositeDisposable.cs +++ b/src/Elastic.Apm.Internals/CompositeDisposable.cs @@ -26,7 +26,7 @@ public void Dispose() _isDisposed = true; - foreach (var d in _disposables) + foreach (IDisposable? d in _disposables) { d.Dispose(); } diff --git a/src/Elastic.Apm.Messaging.MassTransit/MassTransitDiagnosticsSubscriber.cs b/src/Elastic.Apm.Messaging.MassTransit/MassTransitDiagnosticsSubscriber.cs index d753a42..4916857 100644 --- a/src/Elastic.Apm.Messaging.MassTransit/MassTransitDiagnosticsSubscriber.cs +++ b/src/Elastic.Apm.Messaging.MassTransit/MassTransitDiagnosticsSubscriber.cs @@ -18,16 +18,16 @@ public MassTransitDiagnosticsSubscriber(Action? co } /// - public IDisposable Subscribe(IApmAgent components) + public IDisposable Subscribe(IApmAgent apmAgent) { var compositeDisposable = new CompositeDisposable(); - if (!components.ConfigurationReader.Enabled) + if (!apmAgent.ConfigurationReader.Enabled) { return compositeDisposable; } - var initializer = new MassTransitDiagnosticInitializer(components, _options); + var initializer = new MassTransitDiagnosticInitializer(apmAgent, _options); compositeDisposable.Add(initializer); compositeDisposable.Add(DiagnosticListener.AllListeners.Subscribe(initializer));