internal void OnCommandStarted(CommandStartedEvent evt)
        {
            Prune(DateTime.UtcNow);

            if (_settings.FilteredCommands.Contains(evt.CommandName))
            {
                return;
            }

            string target = FormatEndPoint(evt.ConnectionId.ServerId.EndPoint) +
                            " | " + evt.DatabaseNamespace.ToString();
            string dependencyName = target + " | " + evt.CommandName;
            var    telemetry      = new DependencyTelemetry()
            {
                Name   = dependencyName,
                Type   = "MongoDB",
                Target = target,
                // Command can't be null -- the CommandStartedEvent constructor throws to prevent this
                Data    = evt.Command.ToString(),
                Success = true,
            };

            telemetry.GenerateOperationId();
            telemetry.Timestamp = DateTimeOffset.UtcNow;

            var activity = Activity.Current;

            if (activity != null)
            {
                telemetry.Context.Operation.Id       = activity.RootId;
                telemetry.Context.Operation.ParentId = activity.Id;

                foreach (var item in activity.Baggage)
                {
                    if (!telemetry.Context.GlobalProperties.ContainsKey(item.Key))
                    {
                        telemetry.Context.GlobalProperties[item.Key] = item.Value;
                    }
                }
            }
            else
            {
                telemetry.Context.Operation.Id = telemetry.Id;
            }

            var query = new CachedQuery {
                CachedAt = DateTime.UtcNow, Telemetry = telemetry
            };

            _queryCache.TryAdd(evt.RequestId, query);
        }
예제 #2
0
        internal void OnCommandStarted(CommandStartedEvent evt)
        {
            Prune(DateTime.UtcNow);

            if (_settings.FilteredCommands.Contains(evt.CommandName))
            {
                return;
            }

            var target         = $"{FormatEndPoint(evt.ConnectionId.ServerId.EndPoint)} | {evt.DatabaseNamespace}";
            var dependencyName = $"{target} | {evt.CommandName}";

            var commandText = string.Empty;

            if (_settings.EnableMongoCommandTextInstrumentation)
            {
                // Command can't be null -- the CommandStartedEvent constructor throws to prevent this
                commandText = evt.Command.ToString();
            }

            var telemetry = new DependencyTelemetry()
            {
                Name    = dependencyName,
                Type    = "MongoDB",
                Target  = target,
                Data    = commandText,
                Success = true,
            };

            telemetry.GenerateOperationId();
            telemetry.Timestamp = DateTimeOffset.UtcNow;

            /*
             * copying implementation below from Microsoft's SqlClientDiagnosticSourceListener
             * https://github.com/microsoft/ApplicationInsights-dotnet/blob/5ac6bb98d04b7da6d151c0338efece4c124c750a/WEB/Src/DependencyCollector/DependencyCollector/Implementation/SqlClientDiagnostics/SqlClientDiagnosticSourceListener.cs#L347
             */

            var activity = Activity.Current;

            if (activity != null)
            {
                // for web applications the IdFormat is W3C so without the below check the parient ID is set incorrectly
                if (activity.IdFormat == ActivityIdFormat.W3C)
                {
                    var traceId = activity.TraceId.ToHexString();
                    telemetry.Context.Operation.Id       = traceId;
                    telemetry.Context.Operation.ParentId = activity.SpanId.ToHexString();
                }
                else
                {
                    telemetry.Context.Operation.Id       = activity.RootId;
                    telemetry.Context.Operation.ParentId = activity.Id;
                }

                foreach (var item in activity.Baggage)
                {
                    if (!telemetry.Properties.ContainsKey(item.Key))
                    {
                        telemetry.Properties[item.Key] = item.Value;
                    }
                }
            }
            else
            {
                telemetry.Context.Operation.Id = telemetry.Id;
            }

            var query = new CachedQuery {
                CachedAt = DateTime.UtcNow, Telemetry = telemetry
            };

            _queryCache.TryAdd(evt.RequestId, query);
        }