public AfterWrappedMethodDelegate BeforeWrappedMethod(InstrumentedMethodCall instrumentedMethodCall, IAgent agent, ITransaction transaction)
        {
            var contextObject = instrumentedMethodCall.MethodCall.MethodArguments[2];

            if (contextObject == null)
            {
                throw new NullReferenceException(nameof(contextObject));
            }

            var controllerName = TryGetPropertyName(PropertyControllerName, contextObject);

            if (controllerName == null)
            {
                throw new NullReferenceException(nameof(controllerName));
            }

            var actionName = TryGetPropertyName(PropertyAction, contextObject);

            if (actionName == null)
            {
                throw new NullReferenceException(nameof(actionName));
            }

            transaction.SetWebTransactionName(WebTransactionType.MonoRail, $"{controllerName}.{actionName}", TransactionNamePriority.FrameworkLow);
            var segment = transaction.StartMethodSegment(instrumentedMethodCall.MethodCall, controllerName, actionName);

            return(Delegates.GetDelegateFor(segment));
        }
        public AfterWrappedMethodDelegate BeforeWrappedMethod(InstrumentedMethodCall instrumentedMethodCall,
                                                              IAgentWrapperApi agentWrapperApi)
        {
            var context = instrumentedMethodCall.MethodCall.MethodArguments
                          .ExtractNotNullAs <IIncomingLogicalMessageContext>(0);
            var incomingLogicalMessage = context.Message;

            if (incomingLogicalMessage == null)
            {
                throw new NullReferenceException("logicalMessage");
            }
            var headers = context.Headers;

            if (headers == null)
            {
                throw new NullReferenceException("headers");
            }
            var queueName = TryGetQueueName(incomingLogicalMessage);

            agentWrapperApi.CreateMessageBrokerTransaction(0, "NServiceBus", queueName);
            var isegment = agentWrapperApi.StartMessageBrokerSegment(instrumentedMethodCall.MethodCall, 0,
                                                                     (MessageBrokerAction)1, "NServiceBus", queueName);

            agentWrapperApi.ProcessInboundRequest(headers);
            return(Delegates.GetDelegateFor(() =>
            {
                agentWrapperApi.EndSegment(isegment);
                agentWrapperApi.EndTransaction();
            }, null, agentWrapperApi.NoticeError));
        }
        public AfterWrappedMethodDelegate BeforeWrappedMethod(InstrumentedMethodCall instrumentedMethodCall, IAgent agent, ITransaction transaction)
        {
            // 3.6.0+ (5.1.0+) (IModel)void BasicPublish(string exchange, string routingKey, bool mandatory, IBasicProperties basicProperties, byte[] body)
            var segment = RabbitMqHelper.CreateSegmentForPublishWrappers(instrumentedMethodCall, transaction, agent.Configuration, BasicPropertiesIndex);

            return(Delegates.GetDelegateFor(segment));
        }
Exemplo n.º 4
0
        public AfterWrappedMethodDelegate BeforeWrappedMethod(InstrumentedMethodCall instrumentedMethodCall, IAgent agent, ITransaction transaction)
        {
            var operation = instrumentedMethodCall.MethodCall.Method.MethodName;
            var isAsync   = operation.EndsWith("Async", StringComparison.OrdinalIgnoreCase);

            operation = isAsync ? "LinqQueryAsync" : "LinqQuery";

            var caller = instrumentedMethodCall.MethodCall.InvocationTarget;

            var collection = MongoDbHelper.GetCollectionFieldFromGeneric(caller);
            var database   = MongoDbHelper.GetDatabaseFromGeneric(collection);

            ConnectionInfo connectionInfo      = MongoDbHelper.GetConnectionInfoFromDatabase(database);
            var            collectionNamespace = MongoDbHelper.GetCollectionNamespacePropertyFromGeneric(caller);
            var            model = MongoDbHelper.GetCollectionName(collectionNamespace);

            var segment = transaction.StartDatastoreSegment(instrumentedMethodCall.MethodCall,
                                                            new ParsedSqlStatement(DatastoreVendor.MongoDB, model, operation), isLeaf: true, connectionInfo: connectionInfo);

            if (!isAsync)
            {
                return(Delegates.GetDelegateFor(segment));
            }

            return(Delegates.GetAsyncDelegateFor <Task>(agent, segment, true));
        }
        public static ISegment CreateSegmentForPublishWrappers6Plus(InstrumentedMethodCall instrumentedMethodCall, ITransaction transaction, int basicPropertiesIndex)
        {
            var basicProperties = instrumentedMethodCall.MethodCall.MethodArguments.ExtractAs <object>(basicPropertiesIndex);

            var routingKey = instrumentedMethodCall.MethodCall.MethodArguments.ExtractNotNullAs <string>(1);
            var destType   = GetBrokerDestinationType(routingKey);
            var destName   = ResolveDestinationName(destType, routingKey);

            var segment = transaction.StartMessageBrokerSegment(instrumentedMethodCall.MethodCall, destType, MessageBrokerAction.Produce, VendorName, destName);

            //If the RabbitMQ version doesn't provide the BasicProperties parameter we just bail.
            if (basicProperties.GetType().FullName != BasicPropertiesType)
            {
                return(segment);
            }

            var setHeaders = new Action <object, string, string>((carrier, key, value) =>
            {
                var headers = GetHeaders(carrier);
                if (headers == null)
                {
                    headers = new Dictionary <string, object>();
                    SetHeaders(carrier, headers);
                }

                headers[key] = value;
            });

            transaction.InsertDistributedTraceHeaders(basicProperties, setHeaders);

            return(segment);
        }
Exemplo n.º 6
0
        public AfterWrappedMethodDelegate BeforeWrappedMethod(InstrumentedMethodCall instrumentedMethodCall, IAgent agent, ITransaction transaction)
        {
            return(Delegates.GetDelegateFor <RouteData>(onSuccess: routeData =>
            {
                if (routeData == null)
                {
                    return;
                }

                if (routeData.RouteHandler == null)
                {
                    return;
                }

                if (routeData.RouteHandler is StopRoutingHandler)
                {
                    return;
                }

                var route = routeData.Route as Route;
                if (route == null)
                {
                    return;
                }

                var url = route.Url;
                if (url == null)
                {
                    return;
                }

                transaction.SetWebTransactionName(WebTransactionType.ASP, url, TransactionNamePriority.Route);
            }));
        }
Exemplo n.º 7
0
        public AfterWrappedMethodDelegate BeforeWrappedMethod(InstrumentedMethodCall instrumentedMethodCall, IAgent agent, ITransaction transaction)
        {
            var page = instrumentedMethodCall.MethodCall.InvocationTarget as Page;

            if (page == null)
            {
                return(Delegates.NoOp);
            }

            var pagePath = page.AppRelativeVirtualPath;

            if (pagePath == null)
            {
                return(Delegates.NoOp);
            }

            if (pagePath.StartsWith("~/"))
            {
                pagePath = pagePath.Substring(2);
            }

            pagePath = pagePath.ToLower();

            transaction.SetWebTransactionName(WebTransactionType.ASP, pagePath, TransactionNamePriority.FrameworkHigh);
            var segment = transaction.StartTransactionSegment(instrumentedMethodCall.MethodCall, pagePath);

            return(Delegates.GetDelegateFor(segment));
        }
Exemplo n.º 8
0
        public AfterWrappedMethodDelegate BeforeWrappedMethod(InstrumentedMethodCall instrumentedMethodCall, IAgent agent, ITransaction transaction)
        {
            if (instrumentedMethodCall.IsAsync)
            {
                transaction.AttachToAsync();
            }

            var operation = instrumentedMethodCall.MethodCall.Method.MethodName;

            if (operation.StartsWith("Get"))
            {
                operation = "Get";
            }

            var parm = instrumentedMethodCall.MethodCall.MethodArguments[0];

            if (parm is IList || parm is IDictionary)
            {
                operation += "Multiple";
            }

            var model = GetMethodInfo.Invoke(instrumentedMethodCall.MethodCall.InvocationTarget);

            var segment = transaction.StartDatastoreSegment(
                instrumentedMethodCall.MethodCall,
                new ParsedSqlStatement(DatastoreVendor.Couchbase, model, operation));

            return(Delegates.GetAsyncDelegateFor <Task>(agent, segment));
        }
        public AfterWrappedMethodDelegate BeforeWrappedMethod(InstrumentedMethodCall instrumentedMethodCall,
                                                              IAgent agent, ITransaction transaction)
        {
            var methodName = GetMethodInfo.Invoke(instrumentedMethodCall.MethodCall.InvocationTarget);

            if (methodName == null)
            {
                throw new NullReferenceException("Could not retrieve a value from _methodName field on the invocation target");
            }

            var service     = instrumentedMethodCall.MethodCall.MethodArguments[0];
            var serviceType = "";

            if (service == null)
            {
                var methodOwner    = GetMethodOwner.Invoke(instrumentedMethodCall.MethodCall.InvocationTarget);
                var methodTypeData = GetMethodTypeData.Invoke(methodOwner);
                var methodType     = GetMethodType.Invoke(methodTypeData);
                serviceType = methodType.Name;
            }
            else
            {
                serviceType = service.GetType().Name;
            }

            var transactionName = serviceType + "/" + methodName;

            transaction.SetWebTransactionName(WebTransactionType.WebService, transactionName, TransactionNamePriority.FrameworkLow);
            var segment = transaction.StartMethodSegment(instrumentedMethodCall.MethodCall,
                                                         instrumentedMethodCall.MethodCall.Method.Type.ToString(), instrumentedMethodCall.MethodCall.Method.MethodName);

            return(Delegates.GetDelegateFor(segment));
        }
Exemplo n.º 10
0
        public AfterWrappedMethodDelegate BeforeWrappedMethod(InstrumentedMethodCall instrumentedMethodCall, IAgent agent, ITransaction transaction)
        {
            var httpApplication = (HttpApplication)instrumentedMethodCall.MethodCall.InvocationTarget;

            if (httpApplication == null)
            {
                throw new NullReferenceException("invocationTarget");
            }

            var eventIndex = instrumentedMethodCall.MethodCall.MethodArguments.ExtractNotNullAs <object>(0);
            var steps      = instrumentedMethodCall.MethodCall.MethodArguments.ExtractNotNullAs <ArrayList>(1);

            var eventName = Statics.EventIndexToString[eventIndex];

            if (eventName == null)
            {
                throw new NullReferenceException("Could not find a valid eventName for index " + eventIndex);
            }

            var beforeExecutionStep = GetBeforeExecutionStep(instrumentedMethodCall.MethodCall, agent, eventName, httpApplication);
            var afterExecutionStep  = GetAfterExecutionStep(instrumentedMethodCall.MethodCall, agent, eventName, httpApplication);

            steps.Add(beforeExecutionStep);
            return(Delegates.GetDelegateFor(() => steps.Add(afterExecutionStep)));
        }
        public AfterWrappedMethodDelegate BeforeWrappedMethod(InstrumentedMethodCall instrumentedMethodCall, IAgent agent, ITransaction transaction)
        {
            var redisCommandWithArgumentsAsBytes = instrumentedMethodCall.MethodCall.MethodArguments.ExtractNotNullAs <byte[][]>(0);
            var redisCommand = redisCommandWithArgumentsAsBytes[0];

            if (redisCommand == null)
            {
                return(Delegates.NoOp);
            }

            var operation     = GetRedisCommand(redisCommand);
            var contextObject = instrumentedMethodCall.MethodCall.InvocationTarget;

            if (contextObject == null)
            {
                throw new NullReferenceException(nameof(contextObject));
            }

            var host = TryGetPropertyName(PropertyHost, contextObject) ?? "unknown";

            host = ConnectionStringParserHelper.NormalizeHostname(host);
            var portPathOrId   = TryGetPropertyName(PropertyPortPathOrId, contextObject);
            var databaseName   = TryGetPropertyName(PropertyDatabaseName, contextObject);
            var connectionInfo = new ConnectionInfo(host, portPathOrId, databaseName);

            var segment = transaction.StartDatastoreSegment(instrumentedMethodCall.MethodCall, ParsedSqlStatement.FromOperation(DatastoreVendor.Redis, operation), connectionInfo);

            return(Delegates.GetDelegateFor(segment));
        }
        public AfterWrappedMethodDelegate BeforeWrappedMethod(InstrumentedMethodCall instrumentedMethodCall, IAgent agent, ITransaction transaction)
        {
            var logicalMethodInfo = instrumentedMethodCall.MethodCall.InvocationTarget as LogicalMethodInfo;

            if (logicalMethodInfo == null)
            {
                throw new NullReferenceException("LogicalMethodInfo was expected.");
            }

            var declaringType = logicalMethodInfo.DeclaringType;
            var methodName    = logicalMethodInfo.Name;

            var name = string.Format("{0}.{1}", declaringType.FullName, methodName);

            transaction.SetWebTransactionName(WebTransactionType.WebService, name, TransactionNamePriority.FrameworkLow);
            var segment = transaction.StartTransactionSegment(instrumentedMethodCall.MethodCall, name);

            return(Delegates.GetDelegateFor(
                       onFailure: OnFailure,
                       onComplete: segment.End
                       ));

            void OnFailure(Exception ex)
            {
                if (ex is System.Reflection.TargetInvocationException && ex.InnerException != null)
                {
                    ex = ex.InnerException;
                }

                if (ex != null)
                {
                    transaction.NoticeError(ex);
                }
            }
        }
        public AfterWrappedMethodDelegate BeforeWrappedMethod(InstrumentedMethodCall instrumentedMethodCall,
                                                              IAgent agent, ITransaction transaction)
        {
            requestParser = requestParser ?? new ApiRequestParser(agent);

            object[] methodArgs = instrumentedMethodCall.MethodCall.MethodArguments;
            if (methodArgs.Length > 0)
            {
                //Effectively calling something like this below: object request = controllerContext.Request");
                object controllerContextObject = methodArgs[0];
                if (controllerContextObject == null)
                {
                    throw new NullReferenceException(nameof(controllerContextObject));
                }
                object request = controllerContextObject?.GetType()?.GetProperty(REQUEST_PROP_NAME)?.GetValue(controllerContextObject);
                requestParser.parse(request);
            }


            // We are using a variant of GetDelegateFor that doesn't pass down a return value to the local methods since we don't need the return.
            return(Delegates.GetDelegateFor(
                       onSuccess: OnSuccess
                       ));

            void OnSuccess()
            {
            }
        }
        private MethodInfo TryGetMethodInfo(InstrumentedMethodCall instrumentedMethodCall)
        {
            var methodName       = instrumentedMethodCall.MethodCall.Method.MethodName;
            var invocationTarget = instrumentedMethodCall.MethodCall.InvocationTarget;
            var isTAP            = instrumentedMethodCall.InstrumentedMethodInfo.Method.Type.Name == TAPTypeNameShort;

            if (methodName == SyncMethodName)
            {
                return(Statics.GetSyncMethodInfo(invocationTarget));
            }
            else if (methodName == InvokeBeginMethodName)
            {
                return(isTAP ? Statics.GetTAPAsyncTaskMethodInfo(invocationTarget) : Statics.GetAsyncBeginMethodInfo(invocationTarget));
            }
            else if (methodName == InvokeEndMethodName)
            {
                return(isTAP ? Statics.GetTAPAsyncTaskMethodInfo(invocationTarget) : Statics.GetAsyncEndMethodInfo(invocationTarget));
            }
            else if (methodName == InvokeAsyncMethodName)
            {
                return(Statics.GetTAPAsyncTaskMethodInfo(invocationTarget));
            }

            throw new Exception($"Unexpected instrumented method in wrapper: {instrumentedMethodCall.MethodCall.Method.MethodName}");
        }
Exemplo n.º 15
0
        public AfterWrappedMethodDelegate BeforeWrappedMethod(InstrumentedMethodCall instrumentedMethodCall, IAgent agent, ITransaction transaction)
        {
            var typeName = instrumentedMethodCall.MethodCall.Method.Type.FullName ?? "unknown";
            var segment  = transaction.StartMethodSegment(instrumentedMethodCall.MethodCall, typeName, instrumentedMethodCall.MethodCall.Method.MethodName, isLeaf: true);

            return(Delegates.GetDelegateFor(segment));
        }
        public AfterWrappedMethodDelegate BeforeWrappedMethod(InstrumentedMethodCall instrumentedMethodCall, IAgent agent, ITransaction transaction)
        {
            var request = (System.Net.HttpWebRequest)instrumentedMethodCall.MethodCall.InvocationTarget;

            if (request == null)
            {
                throw new NullReferenceException(nameof(request));
            }

            if (request.Headers == null)
            {
                throw new NullReferenceException("request.Headers");
            }

            var setHeaders = new Action <System.Net.HttpWebRequest, string, string>((carrier, key, value) =>
            {
                carrier.Headers?.Set(key, value);
            });

            try
            {
                transaction.InsertDistributedTraceHeaders(request, setHeaders);
            }
            catch (Exception ex)
            {
                agent.HandleWrapperException(ex);
            }

            return(Delegates.NoOp);
        }
        public static ISegment CreateSegmentForPublishWrappers(InstrumentedMethodCall instrumentedMethodCall, ITransaction transaction, IConfiguration configuration, int basicPropertiesIndex)
        {
            // ATTENTION: We have validated that the use of dynamic here is appropriate based on the visibility of the data we're working with.
            // If we implement newer versions of the API or new methods we'll need to re-evaluate.
            // never null. Headers property can be null.
            var basicProperties = instrumentedMethodCall.MethodCall.MethodArguments.ExtractAs <dynamic>(basicPropertiesIndex);

            var routingKey = instrumentedMethodCall.MethodCall.MethodArguments.ExtractNotNullAs <string>(1);
            var destType   = GetBrokerDestinationType(routingKey);
            var destName   = ResolveDestinationName(destType, routingKey);

            var segment = transaction.StartMessageBrokerSegment(instrumentedMethodCall.MethodCall, destType, MessageBrokerAction.Produce, VendorName, destName);

            //If the RabbitMQ version doesn't provide the BasicProperties parameter we just bail.
            if (basicProperties.GetType().ToString() != BasicPropertiesType)
            {
                return(segment);
            }

            var setHeaders = new Action <dynamic, string, string>((carrier, key, value) =>
            {
                Dictionary <string, object> headers = carrier.Headers as Dictionary <string, object>;
                if (headers == null)
                {
                    headers         = new Dictionary <string, object>();
                    carrier.Headers = headers;
                }

                headers[key] = value;
            });

            transaction.InsertDistributedTraceHeaders(basicProperties, setHeaders);

            return(segment);
        }
        public AfterWrappedMethodDelegate BeforeWrappedMethod(InstrumentedMethodCall instrumentedMethodCall, IAgent agent, ITransaction transaction)
        {
            if (instrumentedMethodCall.IsAsync)
            {
                transaction.AttachToAsync();
            }

            // find the first string argument
            string segmentName = null;

            foreach (var argument in instrumentedMethodCall.MethodCall.MethodArguments)
            {
                segmentName = argument as string;
                if (segmentName != null)
                {
                    break;
                }
            }

            if (segmentName == null)
            {
                throw new ArgumentException("The CustomSegmentWrapper can only be applied to a method with a String parameter.");
            }

            var segment = transaction.StartCustomSegment(instrumentedMethodCall.MethodCall, segmentName);

            return(instrumentedMethodCall.IsAsync
                ? Delegates.GetAsyncDelegateFor <Task>(agent, segment)
                : Delegates.GetDelegateFor(segment));
        }
Exemplo n.º 19
0
        public AfterWrappedMethodDelegate BeforeWrappedMethod(InstrumentedMethodCall instrumentedMethodCall, IAgent agent, ITransaction transaction)
        {
            return(Delegates.GetDelegateFor(onComplete: () =>
            {
                var serviceDescription = instrumentedMethodCall.MethodCall.MethodArguments[0] as ServiceDescription;

                lock (_bindingLock)
                {
                    var bindingsSent = new List <Type>();
                    foreach (var endpoint in serviceDescription.Endpoints)
                    {
                        var bindingType = endpoint.Binding.GetType();
                        if (!bindingsSent.Contains(bindingType))
                        {
                            bindingsSent.Add(bindingType);
                            if (!SystemBindingTypes.Contains(bindingType))
                            {
                                agent.GetExperimentalApi().RecordSupportabilityMetric("WCFService/BindingType/CustomBinding");
                            }
                            else
                            {
                                agent.GetExperimentalApi().RecordSupportabilityMetric($"WCFService/BindingType/{bindingType.Name}");
                            }
                        }
                    }
                }
            }));
        }
        public AfterWrappedMethodDelegate BeforeWrappedMethod(InstrumentedMethodCall instrumentedMethodCall, IAgent agent, ITransaction transaction)
        {
            var httpApplication = GetHttpApplication(instrumentedMethodCall.MethodCall.InvocationTarget);

            if (httpApplication == null)
            {
                throw new NullReferenceException("httpApplication");
            }

            var httpContext = httpApplication.Context;

            if (httpContext == null)
            {
                throw new NullReferenceException("httpContext");
            }

            var httpHandler = httpContext.Handler;

            if (httpHandler == null)
            {
                return(Delegates.NoOp);
            }

            var httpHandlerName = httpHandler.GetType().Name;

            transaction.SetWebTransactionName(WebTransactionType.ASP, httpHandlerName, TransactionNamePriority.Handler);

            return(Delegates.NoOp);
        }
        public AfterWrappedMethodDelegate BeforeWrappedMethod(InstrumentedMethodCall instrumentedMethodCall, IAgent agent, ITransaction transaction)
        {
            var operation = GetOperationName(instrumentedMethodCall.MethodCall);
            var segment   = transaction.StartDatastoreSegment(instrumentedMethodCall.MethodCall, ParsedSqlStatement.FromOperation(DatastoreVendor.MongoDB, operation));

            return(Delegates.GetDelegateFor(segment));
        }
Exemplo n.º 22
0
        public AfterWrappedMethodDelegate BeforeWrappedMethod(InstrumentedMethodCall instrumentedMethodCall, IAgent agent, ITransaction transaction)
        {
            transaction.AttachToAsync();

            var controllerContext = instrumentedMethodCall.MethodCall.MethodArguments.ExtractNotNullAs <dynamic>(0);

            if (controllerContext != null)
            {
                var controllerName = MvcRouteNamingHelper.TryGetControllerNameFromObject(controllerContext);
                var actionName     = MvcRouteNamingHelper.TryGetActionNameFromRouteParameters(instrumentedMethodCall.MethodCall, controllerContext.RouteData);

                var httpContext = controllerContext.HttpContext;
                if (httpContext == null)
                {
                    throw new NullReferenceException("httpContext");
                }

                var transactionName = string.Format("{0}/{1}", controllerName, actionName);
                transaction.SetWebTransactionName(WebTransactionType.MVC, transactionName, TransactionNamePriority.FrameworkHigh);

                var segment = transaction.StartMethodSegment(instrumentedMethodCall.MethodCall, controllerName, actionName);

                httpContext.Items[HttpContextSegmentKey] = segment;
            }

            return(Delegates.NoOp);
        }
        private int GetRabbitMQVersion(InstrumentedMethodCall methodCall)
        {
            var fullName      = methodCall.MethodCall.Method.Type.Assembly.ManifestModule.Assembly.FullName;
            var versionString = "Version=";
            var length        = versionString.Length;

            return(Int32.Parse(fullName.Substring(fullName.IndexOf(versionString) + length, 1)));
        }
Exemplo n.º 24
0
        public AfterWrappedMethodDelegate BeforeWrappedMethod(InstrumentedMethodCall instrumentedMethodCall, IAgent agent, ITransaction transaction)
        {
            var operation = GetRemoveOperationName(instrumentedMethodCall.MethodCall);
            var model     = MongoDBHelper.GetCollectionModelName(instrumentedMethodCall.MethodCall);
            var segment   = transaction.StartDatastoreSegment(instrumentedMethodCall.MethodCall, new ParsedSqlStatement(DatastoreVendor.MongoDB, model, operation));

            return(Delegates.GetDelegateFor(segment));
        }
Exemplo n.º 25
0
        public AfterWrappedMethodDelegate BeforeWrappedMethod(InstrumentedMethodCall instrumentedMethodCall, IAgent agent, ITransaction transaction)
        {
            if (instrumentedMethodCall.IsAsync)
            {
                transaction.AttachToAsync();
            }

            var httpRequestMessage = instrumentedMethodCall.MethodCall.MethodArguments.ExtractNotNullAs <HttpRequestMessage>(0);
            var httpClient         = (System.Net.Http.HttpClient)instrumentedMethodCall.MethodCall.InvocationTarget;
            var uri = TryGetAbsoluteUri(httpRequestMessage, httpClient);

            if (uri == null)
            {
                // It is possible for RequestUri to be null, but if it is then SendAsync method will eventually throw (which we will see). It would not be valuable to throw another exception here.
                return(Delegates.NoOp);
            }

            var method = (httpRequestMessage.Method != null ? httpRequestMessage.Method.Method : "<unknown>") ?? "<unknown>";

            var transactionExperimental = transaction.GetExperimentalApi();

            var externalSegmentData = transactionExperimental.CreateExternalSegmentData(uri, method);
            var segment             = transactionExperimental.StartSegment(instrumentedMethodCall.MethodCall);

            segment.GetExperimentalApi().SetSegmentData(externalSegmentData);

            if (agent.Configuration.ForceSynchronousTimingCalculationHttpClient)
            {
                //When segments complete on a thread that is different than the thread of the parent segment,
                //we typically do not deduct the child segment's duration from the parent segment's duration
                //when calculating exclusive time for the parent. In versions of the agent prior to 6.20 this was not
                //the case and at least one customer is complaining about this. We are special-casing this behavior
                //for HttpClient to make this customer happier, and because HttpClient is not a real "async" method.
                //Please refer to the "total time" definition in https://source.datanerd.us/agents/agent-specs/blob/master/Total-Time-Async.md
                //for more information.

                //This pattern should not be copied to other instrumentation without a good reason, because there may be a better
                //pattern to use for that use case.
                segment.DurationShouldBeDeductedFromParent = true;
            }


            // We cannot rely on SerializeHeadersWrapper to attach the headers because it is called on a thread that does not have access to the transaction
            TryAttachHeadersToRequest(agent, httpRequestMessage);

            // 1.  Since this finishes on a background thread, it is possible it will race the end of
            //     the transaction. Using holdTransactionOpen = true to prevent the transaction from ending early.
            // 2.  Do not want to post to the sync context as this library is commonly used with the
            //     blocking TPL pattern of .Result or .Wait(). Posting to the sync context will result
            //     in recording time waiting for the current unit of work on the sync context to finish.
            //     This overload GetAsyncDelegateFor does not use the synchronization context's task scheduler.
            return(Delegates.GetAsyncDelegateFor <Task <HttpResponseMessage> >(agent, segment, true, InvokeTryProcessResponse));

            void InvokeTryProcessResponse(Task <HttpResponseMessage> httpResponseMessage)
            {
                TryProcessResponse(agent, httpResponseMessage, transaction, segment, externalSegmentData);
            }
        }
        public AfterWrappedMethodDelegate BeforeWrappedMethod(InstrumentedMethodCall instrumentedMethodCall, IAgentWrapperApi agentWrapperApi, ITransactionWrapperApi transactionWrapperApi)
        {
            /// Read and setup the configured headers from newrelic.config file
            if (configuredHeaders == null)
            {
                IReadOnlyDictionary <string, string> appSettings = agentWrapperApi.Configuration.GetAppSettings();
                string reqHeaders = null;
                if (appSettings.TryGetValue("requestHeaders", out reqHeaders))
                {
                    configuredHeaders = reqHeaders?.Split(',').Select(p => p.Trim()).ToDictionary(t => t, t => $"http.{t}");
                }
                else
                {
                    configuredHeaders = new Dictionary <string, string>();
                }
            }

            transactionWrapperApi = agentWrapperApi.CreateTransaction(true, "WCF", "Windows Communication Foundation", false);
            var segment = transactionWrapperApi.StartTransactionSegment(instrumentedMethodCall.MethodCall, "InvokeBegin");

            if (webOperationContextType == null)
            {
                Assembly webAssembly = Assembly.Load("System.ServiceModel.Web");
                webOperationContextType = webAssembly?.GetType("System.ServiceModel.Web.WebOperationContext");
            }

            if (configuredHeaders != null)
            {
                BindingFlags flags          = BindingFlags.Static | BindingFlags.Public;
                object       currentContext = webOperationContextType?.GetProperty("Current", flags)?.GetValue(null);
                object       request        = currentContext?.GetType()?.GetProperty("IncomingRequest")?.GetValue(currentContext);
                object       headers        = request?.GetType()?.GetProperty("Headers")?.GetValue(request);

                if (headers != null)
                {
                    WebHeaderCollection headerCollection = headers as System.Net.WebHeaderCollection;
                    if (headerCollection != null)
                    {
                        foreach (KeyValuePair <string, string> entry in configuredHeaders)
                        {
                            string headerValue = headerCollection.Get(entry.Key);
                            if (headerValue != null)
                            {
                                InternalApi.AddCustomParameter(entry.Value, headerValue);
                            }
                        }
                    }
                }
            }

            return(Delegates.GetDelegateFor(
                       onFailure: transactionWrapperApi.NoticeError,
                       onComplete: () =>
            {
                segment.End();
                transactionWrapperApi.End();
            }));
        }
Exemplo n.º 27
0
        public AfterWrappedMethodDelegate BeforeWrappedMethod(InstrumentedMethodCall instrumentedMethodCall, IAgent agent, ITransaction transaction)
        {
            if (ShouldIgnoreTransaction(instrumentedMethodCall.MethodCall))
            {
                agent.CurrentTransaction.Ignore();
            }

            return(Delegates.NoOp);
        }
Exemplo n.º 28
0
        public AfterWrappedMethodDelegate BeforeWrappedMethod(InstrumentedMethodCall instrumentedMethodCall, IAgent agent, ITransaction transaction)
        {
            var mongoCursor = (MongoCursor)instrumentedMethodCall.MethodCall.InvocationTarget;
            var operation   = "GetEnumerator";
            var modelName   = (mongoCursor.Collection == null) ? null : mongoCursor.Collection.Name;
            var segment     = transaction.StartDatastoreSegment(instrumentedMethodCall.MethodCall, new ParsedSqlStatement(DatastoreVendor.MongoDB, modelName, operation));

            return(Delegates.GetDelegateFor(segment));
        }
 public AfterWrappedMethodDelegate BeforeWrappedMethod(InstrumentedMethodCall instrumentedMethodCall, IAgent agent, ITransaction transaction)
 {
     return(Delegates.GetDelegateFor(onComplete: () =>
     {
         var channelFactory = instrumentedMethodCall.MethodCall.InvocationTarget as ChannelFactory;
         channelFactory?.Endpoint.Behaviors.Add(new NewRelicEndpointBehavior(agent));
         TrySendBindingMetric(channelFactory?.Endpoint.Binding.GetType(), agent);
     }));
 }
Exemplo n.º 30
0
        public AfterWrappedMethodDelegate BeforeWrappedMethod(InstrumentedMethodCall instrumentedMethodCall, IAgent agent, ITransaction transaction)
        {
            var operation      = Common.GetRedisCommand(instrumentedMethodCall.MethodCall, AssemblyName);
            var connectionInfo = Common.GetConnectionInfoFromConnectionMultiplexer(instrumentedMethodCall.MethodCall, AssemblyName);

            var segment = transaction.StartDatastoreSegment(instrumentedMethodCall.MethodCall, ParsedSqlStatement.FromOperation(DatastoreVendor.Redis, operation), connectionInfo);

            return(Delegates.GetDelegateFor(segment));
        }