예제 #1
0
        /// <summary>
        /// Traces an asynchronous call to Elasticsearch.
        /// </summary>
        /// <typeparam name="T">Type type of the Task</typeparam>
        /// <param name="pipeline">The pipeline for the original method</param>
        /// <param name="requestData">The request data</param>
        /// <param name="cancellationToken">A cancellation token</param>
        /// <returns>The original result</returns>
        private static async Task <T> CallElasticsearchAsyncInternal <T>(object pipeline, object requestData, CancellationToken cancellationToken)
        {
            var genericArgument = typeof(T).GetGenericArguments()[0];

            var executeAsync = CallElasticsearchAsyncAccess.GetInterceptedMethod(
                pipeline.GetType(),
                returnType: null,
                methodName: nameof(CallElasticsearchAsync),
                generics: new[] { genericArgument },
                parameters: new[] { ElasticsearchNetCommon.RequestDataType, ElasticsearchNetCommon.CancellationTokenType });

            using (var scope = ElasticsearchNetCommon.CreateScope(Tracer.Instance, IntegrationName, pipeline, requestData))
            {
                try
                {
                    var task = (Task <T>)executeAsync(pipeline, requestData, cancellationToken);
                    return(await task.ConfigureAwait(false));
                }
                catch (Exception ex) when(scope?.Span.SetExceptionForFilter(ex) ?? false)
                {
                    // unreachable code
                    throw;
                }
            }
        }
예제 #2
0
        /// <summary>
        /// Traces an asynchronous call to Elasticsearch.
        /// </summary>
        /// <typeparam name="T">Type type of the Task</typeparam>
        /// <param name="pipeline">The pipeline for the original method</param>
        /// <param name="requestData">The request data</param>
        /// <param name="cancellationToken">A cancellation token</param>
        /// <param name="originalMethod">A delegate for the method we are instrumenting</param>
        /// <returns>The original result</returns>
        private static async Task <T> CallElasticsearchAsyncInternal <T>(
            object pipeline,
            object requestData,
            CancellationToken cancellationToken,
            Func <object, object, CancellationToken, object> originalMethod)
        {
            using (var scope = ElasticsearchNetCommon.CreateScope(Tracer.Instance, IntegrationName, pipeline, requestData))
            {
                try
                {
                    var task     = (Task <T>)originalMethod(pipeline, requestData, cancellationToken);
                    var returned = await task.ConfigureAwait(false);

                    if (scope != null)
                    {
                        await scope.Span.SetDbStatementFromRequestDataAsync(requestData);
                    }

                    return(returned);
                }
                catch (Exception ex) when(scope?.Span.SetExceptionForFilter(ex) ?? false)
                {
                    // unreachable code
                    throw;
                }
            }
        }
        public static object CallElasticsearch <TResponse>(
            object pipeline,
            object requestData,
            int opCode,
            int mdToken,
            long moduleVersionPtr)
        {
            if (pipeline == null)
            {
                throw new ArgumentNullException(nameof(pipeline));
            }

            const string methodName = nameof(CallElasticsearch);
            Func <object, object, TResponse> callElasticSearch;
            var pipelineType    = pipeline.GetType();
            var genericArgument = typeof(TResponse);

            try
            {
                callElasticSearch =
                    MethodBuilder <Func <object, object, TResponse> >
                    .Start(moduleVersionPtr, mdToken, opCode, methodName)
                    .WithConcreteType(pipelineType)
                    .WithMethodGenerics(genericArgument)
                    .WithParameters(requestData)
                    .WithNamespaceAndNameFilters(ClrNames.Ignore, "Elasticsearch.Net.RequestData")
                    .Build();
            }
            catch (Exception ex)
            {
                // profiled app will not continue working as expected without this method
                Log.ErrorRetrievingMethod(
                    exception: ex,
                    moduleVersionPointer: moduleVersionPtr,
                    mdToken: mdToken,
                    opCode: opCode,
                    instrumentedType: RequestPipelineInterfaceTypeName,
                    methodName: methodName,
                    instanceType: pipeline.GetType().AssemblyQualifiedName);
                throw;
            }

            using (var scope = ElasticsearchNetCommon.CreateScope(Tracer.Instance, IntegrationName, pipeline, requestData))
            {
                try
                {
                    var returned = callElasticSearch(pipeline, requestData);
                    scope?.Span.SetDbStatementFromRequestData(requestData);
                    return(returned);
                }
                catch (Exception ex) when(scope?.Span.SetExceptionForFilter(ex) ?? false)
                {
                    // unreachable code
                    throw;
                }
            }
        }
        /// <summary>
        /// Traces an asynchronous call to Elasticsearch.
        /// </summary>
        /// <typeparam name="TResponse">Type type of the response</typeparam>
        /// <param name="pipeline">The pipeline for the original method</param>
        /// <param name="requestData">The request data</param>
        /// <param name="cancellationToken">A cancellation token</param>
        /// <param name="opCode">The OpCode used in the original method call.</param>
        /// <param name="mdToken">The mdToken of the original method call.</param>
        /// <param name="moduleVersionPtr">A pointer to the module version GUID.</param>
        /// <returns>The original result</returns>
        private static async Task <TResponse> CallElasticsearchAsyncInternal <TResponse>(
            object pipeline,
            object requestData,
            CancellationToken cancellationToken,
            int opCode,
            int mdToken,
            long moduleVersionPtr)
        {
            const string methodName = "CallElasticsearchAsync";
            Func <object, object, CancellationToken, Task <TResponse> > callElasticSearchAsync;
            var pipelineType    = pipeline.GetType();
            var genericArgument = typeof(TResponse);

            try
            {
                callElasticSearchAsync =
                    MethodBuilder <Func <object, object, CancellationToken, Task <TResponse> > >
                    .Start(moduleVersionPtr, mdToken, opCode, methodName)
                    .WithConcreteType(pipelineType)
                    .WithMethodGenerics(genericArgument)
                    .WithParameters(requestData, cancellationToken)
                    .WithNamespaceAndNameFilters(ClrNames.GenericTask, "Elasticsearch.Net.RequestData", ClrNames.CancellationToken)
                    .Build();
            }
            catch (Exception ex)
            {
                Log.ErrorRetrievingMethod(
                    exception: ex,
                    moduleVersionPointer: moduleVersionPtr,
                    mdToken: mdToken,
                    opCode: opCode,
                    instrumentedType: RequestPipelineInterfaceTypeName,
                    methodName: methodName,
                    instanceType: pipelineType.AssemblyQualifiedName);
                throw;
            }

            using (var scope = ElasticsearchNetCommon.CreateScope(Tracer.Instance, IntegrationName, pipeline, requestData))
            {
                try
                {
                    var returned = await callElasticSearchAsync(pipeline, requestData, cancellationToken).ConfigureAwait(false);

                    if (scope != null)
                    {
                        await scope.Span.SetDbStatementFromRequestDataAsync(requestData);
                    }

                    return(returned);
                }
                catch (Exception ex) when(scope?.Span.SetExceptionForFilter(ex) ?? false)
                {
                    // unreachable code
                    throw;
                }
            }
        }
예제 #5
0
        public static object CallElasticsearch <TResponse>(
            object pipeline,
            object requestData,
            int opCode,
            int mdToken,
            long moduleVersionPtr)
        {
            if (pipeline == null)
            {
                throw new ArgumentNullException(nameof(pipeline));
            }

            const string methodName = nameof(CallElasticsearch);
            Func <object, object, object> callElasticSearch;
            var pipelineType    = pipeline.GetType();
            var genericArgument = typeof(TResponse);

            try
            {
                callElasticSearch =
                    MethodBuilder <Func <object, object, object> >
                    .Start(moduleVersionPtr, mdToken, opCode, methodName)
                    .WithConcreteType(pipelineType)
                    .WithMethodGenerics(genericArgument)
                    .WithNamespaceAndNameFilters("Elasticsearch.Net.ElasticsearchResponse`1", "Elasticsearch.Net.RequestData")
                    .WithParameters(requestData)
                    .Build();
            }
            catch (Exception ex)
            {
                Log.ErrorRetrievingMethod(
                    exception: ex,
                    moduleVersionPointer: moduleVersionPtr,
                    mdToken: mdToken,
                    opCode: opCode,
                    instrumentedType: RequestPipelineInterfaceTypeName,
                    methodName: methodName,
                    instanceType: pipeline.GetType().AssemblyQualifiedName);
                throw;
            }

            using (var scope = ElasticsearchNetCommon.CreateScope(Tracer.Instance, IntegrationName, pipeline, requestData))
            {
                try
                {
                    return(callElasticSearch(pipeline, requestData));
                }
                catch (Exception ex)
                {
                    scope?.Span.SetException(ex);
                    throw;
                }
            }
        }
        /// <summary>
        /// Traces an asynchronous call to Elasticsearch.
        /// </summary>
        /// <typeparam name="TResponse">Type type of the response</typeparam>
        /// <param name="pipeline">The pipeline for the original method</param>
        /// <param name="requestData">The request data</param>
        /// <param name="cancellationToken">A cancellation token</param>
        /// <param name="opCode">The OpCode used in the original method call.</param>
        /// <param name="mdToken">The mdToken of the original method call.</param>
        /// <param name="moduleVersionPtr">A pointer to the module version GUID.</param>
        /// <returns>The original result</returns>
        private static async Task <TResponse> CallElasticsearchAsyncInternal <TResponse>(
            object pipeline,
            object requestData,
            CancellationToken cancellationToken,
            int opCode,
            int mdToken,
            long moduleVersionPtr)
        {
            const string methodName = "CallElasticsearchAsync";
            Func <object, object, CancellationToken, Task <TResponse> > callElasticSearchAsync;
            var pipelineType    = pipeline.GetType();
            var genericArgument = typeof(TResponse);

            try
            {
                callElasticSearchAsync =
                    MethodBuilder <Func <object, object, CancellationToken, Task <TResponse> > >
                    .Start(moduleVersionPtr, mdToken, opCode, methodName)
                    .WithConcreteType(pipelineType)
                    .WithMethodGenerics(genericArgument)
                    .WithParameters(requestData, cancellationToken)
                    .WithNamespaceAndNameFilters(ClrNames.GenericTask, "Elasticsearch.Net.RequestData", ClrNames.CancellationToken)
                    .Build();
            }
            catch (Exception ex)
            {
                // profiled app will not continue working as expected without this method
                Log.ErrorException($"Error retrieving {pipelineType.Name}.{methodName}(RequestData requestData, CancellationToken cancellationToken)", ex);
                throw;
            }

            using (var scope = ElasticsearchNetCommon.CreateScope(Tracer.Instance, IntegrationName, pipeline, requestData))
            {
                try
                {
                    return(await callElasticSearchAsync(pipeline, requestData, cancellationToken).ConfigureAwait(false));
                }
                catch (Exception ex) when(scope?.Span.SetExceptionForFilter(ex) ?? false)
                {
                    // unreachable code
                    throw;
                }
            }
        }
        public static object CallElasticsearch <TResponse>(
            object pipeline,
            object requestData,
            int opCode,
            int mdToken,
            long moduleVersionPtr)
        {
            const string methodName = nameof(CallElasticsearch);
            Func <object, object, TResponse> callElasticSearch;
            var pipelineType    = pipeline.GetType();
            var genericArgument = typeof(TResponse);

            try
            {
                callElasticSearch =
                    MethodBuilder <Func <object, object, TResponse> >
                    .Start(moduleVersionPtr, mdToken, opCode, methodName)
                    .WithConcreteType(pipelineType)
                    .WithMethodGenerics(genericArgument)
                    .WithParameters(requestData)
                    .WithNamespaceAndNameFilters(ClrNames.Ignore, "Elasticsearch.Net.RequestData")
                    .Build();
            }
            catch (Exception ex)
            {
                // profiled app will not continue working as expected without this method
                Log.ErrorException($"Error retrieving {pipelineType.Name}.{methodName}(RequestData requestData)", ex);
                throw;
            }

            using (var scope = ElasticsearchNetCommon.CreateScope(Tracer.Instance, IntegrationName, pipeline, requestData))
            {
                try
                {
                    return(callElasticSearch(pipeline, requestData));
                }
                catch (Exception ex) when(scope?.Span.SetExceptionForFilter(ex) ?? false)
                {
                    // unreachable code
                    throw;
                }
            }
        }
예제 #8
0
 /// <summary>
 /// Traces an asynchronous call to Elasticsearch.
 /// </summary>
 /// <typeparam name="T">Type type of the Task</typeparam>
 /// <param name="pipeline">The pipeline for the original method</param>
 /// <param name="requestData">The request data</param>
 /// <param name="cancellationToken">A cancellation token</param>
 /// <param name="originalMethod">A delegate for the method we are instrumenting</param>
 /// <returns>The original result</returns>
 private static async Task <T> CallElasticsearchAsyncInternal <T>(
     object pipeline,
     object requestData,
     CancellationToken cancellationToken,
     Func <object, object, CancellationToken, object> originalMethod)
 {
     using (var scope = ElasticsearchNetCommon.CreateScope(Tracer.Instance, IntegrationName, pipeline, requestData))
     {
         try
         {
             var task = (Task <T>)originalMethod(pipeline, requestData, cancellationToken);
             return(await task.ConfigureAwait(false));
         }
         catch (Exception ex)
         {
             scope?.Span.SetException(ex);
             throw;
         }
     }
 }
예제 #9
0
        public static object CallElasticsearch <TResponse>(object pipeline, object requestData, int opCode)
        {
            // TResponse CallElasticsearch<TResponse>(RequestData requestData) where TResponse : class, IElasticsearchResponse, new();
            var originalMethod = Emit.DynamicMethodBuilder <Func <object, object, object> >
                                 .GetOrCreateMethodCallDelegate(
                ElasticsearchNetCommon.RequestPipelineType,
                "CallElasticsearch",
                methodGenericArguments : new[] { typeof(TResponse) });

            using (var scope = ElasticsearchNetCommon.CreateScope(Tracer.Instance, IntegrationName, pipeline, requestData))
            {
                try
                {
                    return(originalMethod(pipeline, requestData));
                }
                catch (Exception ex) when(scope?.Span.SetExceptionForFilter(ex) ?? false)
                {
                    // unreachable code
                    throw;
                }
            }
        }
예제 #10
0
        /// <summary>
        /// Traces an asynchronous call to Elasticsearch.
        /// </summary>
        /// <typeparam name="TResponse">Type type of the response</typeparam>
        /// <param name="pipeline">The pipeline for the original method</param>
        /// <param name="requestData">The request data</param>
        /// <param name="cancellationToken">A cancellation token</param>
        /// <returns>The original result</returns>
        private static async Task<TResponse> CallElasticsearchAsyncInternal<TResponse>(object pipeline, object requestData, CancellationToken cancellationToken)
        {
            var originalMethod = Emit.DynamicMethodBuilder<Func<object, object, CancellationToken, Task<TResponse>>>
                                     .GetOrCreateMethodCallDelegate(
                                          ElasticsearchNetCommon.RequestPipelineType,
                                          "CallElasticsearchAsync",
                                          methodParameterTypes: new[] { ElasticsearchNetCommon.RequestDataType, ElasticsearchNetCommon.CancellationTokenType },
                                          methodGenericArguments: new[] { typeof(TResponse) });

            using (var scope = ElasticsearchNetCommon.CreateScope(Tracer.Instance, IntegrationName, pipeline, requestData))
            {
                try
                {
                    return await originalMethod(pipeline, requestData, cancellationToken).ConfigureAwait(false);
                }
                catch (Exception ex) when (scope?.Span.SetExceptionForFilter(ex) ?? false)
                {
                    // unreachable code
                    throw;
                }
            }
        }