public void WhenReceivingNoTracingInformation()
        {
            var actionContext = ContextUtil.CreateActionContext();

            _testApmWebApiFilterAttribute.OnActionExecuting(actionContext);

            Assert.IsTrue(actionContext.Request.Properties.ContainsKey(Constants.TraceIdHeaderKey));
            var traceId = (string)actionContext.Request.Properties[Constants.TraceIdHeaderKey];

            Assert.IsNotEmpty(traceId);

            Assert.IsTrue(actionContext.Request.Properties.ContainsKey(Constants.SpanIdHeaderKey));
            var spanId = (string)actionContext.Request.Properties[Constants.SpanIdHeaderKey];

            Assert.IsNotEmpty(spanId);

            Assert.AreEqual(traceId, spanId);

            Assert.IsTrue(actionContext.Request.Properties.ContainsKey(Constants.ParentSpanIdHeaderKey));
            var parentSpanId = (string)actionContext.Request.Properties[Constants.ParentSpanIdHeaderKey];

            Assert.AreEqual(ApmWebApiRequestDecorator.NoParent, parentSpanId);
        }
        public void WhenReceivingTracingInformationForTraceIdAndSpanId()
        {
            var actionContext = ContextUtil.CreateActionContext();

            actionContext.Request.Headers.Add(Constants.TraceIdHeaderKey, "TestClient=1234");
            actionContext.Request.Headers.Add(Constants.SpanIdHeaderKey, "SpecialProcess=4321");

            _testApmWebApiFilterAttribute.OnActionExecuting(actionContext);

            Assert.IsTrue(actionContext.Request.Properties.ContainsKey(Constants.TraceIdHeaderKey));
            var traceId = (string)actionContext.Request.Properties[Constants.TraceIdHeaderKey];

            Assert.AreEqual("TestClient=1234", traceId);

            Assert.IsTrue(actionContext.Request.Properties.ContainsKey(Constants.SpanIdHeaderKey));
            var spanId = (string)actionContext.Request.Properties[Constants.SpanIdHeaderKey];

            Assert.AreEqual("SpecialProcess=4321", spanId);

            Assert.IsTrue(actionContext.Request.Properties.ContainsKey(Constants.ParentSpanIdHeaderKey));
            var parentSpanId = (string)actionContext.Request.Properties[Constants.ParentSpanIdHeaderKey];

            Assert.AreEqual(ApmWebApiRequestDecorator.NoParent, parentSpanId);
        }
        public void WhenLoggingEndOfRequest()
        {
            var finishActionLogged = false;
            var applicationName    = string.Empty;
            var eventName          = string.Empty;
            var responseTime       = 0L;
            var exception          = default(object);
            var flags             = string.Empty;
            var methodsIdentifier = string.Empty;
            var parentSpanId      = string.Empty;
            var sampled           = string.Empty;
            var spanId            = string.Empty;
            var traceId           = string.Empty;
            var httpRequest       = default(HttpRequestMessage);
            var httpResponse      = default(HttpResponseMessage);
            var actionContext     = ContextUtil.CreateActionContext();

            actionContext.Request.Headers.Add(Constants.TraceIdHeaderKey, "TestClient=1234");
            actionContext.Request.Headers.Add(Constants.SpanIdHeaderKey, "SpecialProcess=4321");
            actionContext.Request.Headers.Add(Constants.ParentSpanIdHeaderKey, "ParentSpecialProcess=5678");
            actionContext.Request.Headers.Add(Constants.SampledHeaderKey, "Sampled");
            actionContext.Request.Headers.Add(Constants.FlagsHeaderKey, "Flags");

            _finishAction = (context, information) =>
            {
                finishActionLogged = true;
                applicationName    = information.ApplicationName;
                eventName          = information.EventName;
                exception          = information.Exception;
                flags             = information.Flags;
                methodsIdentifier = information.MethodIdentifier;
                parentSpanId      = information.ParentSpanId;
                sampled           = information.Sampled;
                spanId            = information.SpanId;
                traceId           = information.TraceId;
                httpRequest       = information.Request;
                httpResponse      = information.Response;
                responseTime      = information.ResponseTime;
            };

            _testApmWebApiFilterAttribute = new TestApmWebApiFilterAttribute(_applicationName, _addResponseHeaders, _startAction, _finishAction);
            _testApmWebApiFilterAttribute.OnActionExecuting(actionContext);

            var actionExecutedContext = new HttpActionExecutedContext(actionContext, new Exception("Exception occured"));

            _testApmWebApiFilterAttribute.OnActionExecuted(actionExecutedContext);

            Assert.IsNotNull(httpRequest);
            //Assert.IsNotNull(httpResponse);
            Assert.IsNotNull(exception);
            Assert.IsTrue(finishActionLogged);
            Assert.Greater(responseTime, 0);
            Assert.IsNotEmpty(applicationName);
            Assert.IsNotEmpty(eventName);
            Assert.AreEqual("TestClient=1234", traceId);
            Assert.AreEqual("SpecialProcess=4321", spanId);
            Assert.AreEqual("ParentSpecialProcess=5678", parentSpanId);
            Assert.AreEqual("Sampled", sampled);
            Assert.AreEqual("Flags", flags);

            var controllerName = actionContext.ControllerContext.ControllerDescriptor.ControllerName;
            var methodType     = actionContext.Request.Method;
            var actionName     = actionContext.ActionDescriptor.ActionName;
            var arguments      = string.Empty;

            var expectedMethodIdentifier = string.Format("{0}.{1}({2}) - {3}", controllerName, actionName, arguments, methodType);

            Assert.AreEqual(expectedMethodIdentifier, methodsIdentifier);
        }