Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get current context from ConcurrentDictionary #499

Merged
merged 2 commits into from
Aug 23, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 22 additions & 15 deletions src/SkyApm.Diagnostics.MongoDB/MongoDiagnosticProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,31 @@

using MongoDB.Driver.Core.Events;
using SkyApm.Tracing;
using SkyApm.Tracing.Segments;
using System;
using System.Collections.Concurrent;

namespace SkyApm.Diagnostics.MongoDB
{
public class MongoDiagnosticsProcessor : ITracingDiagnosticProcessor
{
private readonly ConcurrentDictionary<int, SegmentContext> _contextMap = new ConcurrentDictionary<int, SegmentContext>();

public string ListenerName => "MongoSourceListener";
private readonly ITracingContext _tracingContext;
private readonly IExitSegmentContextAccessor _contextAccessor;

public MongoDiagnosticsProcessor(ITracingContext tracingContext,
IExitSegmentContextAccessor contextAccessor)
public MongoDiagnosticsProcessor(ITracingContext tracingContext)
{
_tracingContext = tracingContext;
_contextAccessor = contextAccessor;
}

[DiagnosticName("MongoActivity.Start")]
public void BeforeExecuteCommand([Object] CommandStartedEvent @event)
{
var operationName = DiagnosticsActivityEventSubscriber.GetCollectionName(@event);
var context = _tracingContext.CreateExitSegmentContext(operationName, @event.ConnectionId.ServerId.EndPoint.ToString());
_contextMap.TryAdd(@event.RequestId, context);

context.Span.SpanLayer = Tracing.Segments.SpanLayer.DB;
context.Span.Component = Common.Components.MongoDBCLIENT;
context.Span.AddTag("db.system", "mongodb");
Expand All @@ -53,24 +56,28 @@ public void BeforeExecuteCommand([Object] CommandStartedEvent @event)

[DiagnosticName("MongoActivity.Stop")]
public void AfterExecuteCommand([Object] CommandSucceededEvent @event)
{
var context = _contextAccessor.Context;
context?.Span.AddTag(Common.Tags.STATUS_CODE, "ok");
{
if (_contextMap.TryRemove(@event.RequestId, out var context))
{
context?.Span.AddTag(Common.Tags.STATUS_CODE, "ok");

_tracingContext.Release(context);
_tracingContext.Release(context);
}
}

[DiagnosticName("MongoActivity.Failed")]
public void FailedExecuteCommand([Object] CommandFailedEvent @event)
{
var context = _contextAccessor.Context;
context?.Span.AddTag("status_description", @event.Failure.Message);
context?.Span.AddTag("error.type", @event.Failure.GetType().FullName);
context?.Span.AddTag("error.msg", @event.Failure.Message);
context?.Span.AddTag("error.stack", @event.Failure.StackTrace);
context?.Span.AddTag(Common.Tags.STATUS_CODE, "error");
if (_contextMap.TryRemove(@event.RequestId, out var context))
{
context?.Span.AddTag("status_description", @event.Failure.Message);
context?.Span.AddTag("error.type", @event.Failure.GetType().FullName);
context?.Span.AddTag("error.msg", @event.Failure.Message);
context?.Span.AddTag("error.stack", @event.Failure.StackTrace);
context?.Span.AddTag(Common.Tags.STATUS_CODE, "error");

_tracingContext.Release(context);
_tracingContext.Release(context);
}
}

}
Expand Down