Skip to content

Commit

Permalink
check current execution segment on null (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vadim Hatsura authored Jun 28, 2021
1 parent cc269fd commit e6c579b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ internal static void CaptureError(this IApmAgent apmAgent, IReadOnlyList<IError>
{
try
{
IExecutionSegment executionSegment = apmAgent.Tracer.GetExecutionSegment();
IExecutionSegment? executionSegment = apmAgent.Tracer.GetExecutionSegment();
if (executionSegment == null) return;

foreach (IError error in errors)
{
var path = error.Path?.ToString();
Expand All @@ -33,7 +35,9 @@ internal static void CaptureException(this IApmAgent apmAgent, Exception excepti
{
try
{
IExecutionSegment executionSegment = apmAgent.Tracer.GetExecutionSegment();
IExecutionSegment? executionSegment = apmAgent.Tracer.GetExecutionSegment();
if (executionSegment == null) return;

executionSegment.CaptureException(exception);

// TODO: Use application ILogger ?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ internal HotChocolateDiagnosticListener(

public override IActivityScope ExecuteRequest(IRequestContext context)
{
ITransaction transaction = _apmAgent.Tracer.CurrentTransaction;
return new RequestActivityScope(context, transaction, _apmAgent);
ITransaction? transaction = _apmAgent.Tracer.CurrentTransaction;
return transaction != null
? new RequestActivityScope(context, transaction, _apmAgent)
: EmptyScope;
}

public override IActivityScope ResolveFieldValue(IMiddlewareContext context)
Expand All @@ -46,7 +48,10 @@ public override IActivityScope 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 = _apmAgent.Tracer.GetExecutionSegment();

if (executionSegment == null) return EmptyScope;

ISpan span = executionSegment.StartSpan(
context.Field.Name!.Value, ApiConstants.TypeRequest, Constants.Apm.SubType);

Expand Down
6 changes: 3 additions & 3 deletions src/Shared/TracerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ namespace Elastic.Apm
{
internal static class TracerExtensions
{
public static IExecutionSegment GetExecutionSegment(this ITracer tracer)
public static IExecutionSegment? GetExecutionSegment(this ITracer tracer)
{
ITransaction transaction = tracer.CurrentTransaction;
return tracer.CurrentSpan ?? (IExecutionSegment)transaction;
ITransaction? transaction = tracer.CurrentTransaction;
return tracer.CurrentSpan ?? (IExecutionSegment?)transaction;
}
}
}

0 comments on commit e6c579b

Please sign in to comment.