Skip to content

Commit

Permalink
Remove IApmAgent dependecy (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
glucaci authored Jan 18, 2022
1 parent 4d019f9 commit 087b17f
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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<IError> errors)
internal static void CaptureError(this ITracer tracer, IReadOnlyList<IError> errors)
{
try
{
IExecutionSegment? executionSegment = apmAgent.Tracer.GetExecutionSegment();
IExecutionSegment? executionSegment = tracer.GetExecutionSegment();
if (executionSegment == null) return;

foreach (IError error in errors)
Expand All @@ -30,31 +29,26 @@ internal static void CaptureError(this IApmAgent apmAgent, IReadOnlyList<IError>
{
executionSegment.CaptureError(error.Message, path, Array.Empty<StackFrame>());
}

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);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -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;

Expand All @@ -65,20 +64,26 @@ 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;
}

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<IError> errors)
{
_apmAgent.CaptureError(errors);
if (Agent.IsConfigured)
{
Agent.Tracer.CaptureError(errors);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
namespace Elastic.Apm.GraphQL.HotChocolate
{
/// <summary>
/// Report diagnostic events to Elastic <see cref="IApmAgent"/>
/// Report diagnostic events to Elastic
/// </summary>
public static class RequestExecutorBuilderExtensions
{
Expand All @@ -24,9 +24,8 @@ public static IRequestExecutorBuilder AddObservability(

return builder.AddDiagnosticEventListener(sp =>
{
IApmAgent apmAgent = sp.GetApplicationService<IApmAgent>();
IConfigurationReader configuration = sp.GetApplicationService<IConfigurationReader>();
return new HotChocolateDiagnosticListener(apmAgent, configuration, options);
return new HotChocolateDiagnosticListener(configuration, options);
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

Expand Down Expand Up @@ -64,19 +60,19 @@ private void EnrichTransaction()

if (_context.Result.HasErrors(out IReadOnlyList<IError>? 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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Elastic.Apm.Internals/CompositeDisposable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void Dispose()

_isDisposed = true;

foreach (var d in _disposables)
foreach (IDisposable? d in _disposables)
{
d.Dispose();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ public MassTransitDiagnosticsSubscriber(Action<MassTransitDiagnosticOptions>? co
}

/// <inheritdoc cref="IDiagnosticsSubscriber"/>
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));

Expand Down

0 comments on commit 087b17f

Please sign in to comment.