Exemple #1
0
 internal static CallTargetReturn EndInvocationVoid(Exception exception, Scope scope, ILambdaExtensionRequest requestBuilder)
 {
     scope?.Dispose();
     Flush();
     NotifyExtensionEnd(requestBuilder, scope, exception != null);
     return(CallTargetReturn.GetDefault());
 }
Exemple #2
0
        /// <summary>
        /// OnMethodEnd callback
        /// </summary>
        /// <typeparam name="TTarget">Type of the target</typeparam>
        /// <param name="instance">Instance value, aka `this` of the instrumented method.</param>
        /// <param name="exception">Exception instance in case the original code threw an exception.</param>
        /// <param name="state">Calltarget state value</param>
        /// <returns>Return value of the method</returns>
        public static CallTargetReturn OnMethodEnd <TTarget>(TTarget instance, Exception exception, CallTargetState state)
        {
            if (!Tracer.Instance.Settings.IsIntegrationEnabled(IntegrationName))
            {
                return(CallTargetReturn.GetDefault());
            }

            SynchronizationContext context = SynchronizationContext.Current;

            try
            {
                SynchronizationContext.SetSynchronizationContext(null);
                // We have to ensure the flush of the buffer after we finish the tests of an assembly.
                // For some reason, sometimes when all test are finished none of the callbacks to handling the tracer disposal is triggered.
                // So the last spans in buffer aren't send to the agent.
                // Other times we reach the 500 items of the buffer in a sec and the tracer start to drop spans.
                // In a test scenario we must keep all spans.
                Tracer.Instance.FlushAsync().GetAwaiter().GetResult();
            }
            finally
            {
                SynchronizationContext.SetSynchronizationContext(context);
            }

            return(CallTargetReturn.GetDefault());
        }
Exemple #3
0
        public static CallTargetReturn OnMethodEnd <TTarget>(TTarget instance, Exception exception, CallTargetState state)
        {
            CallTargetReturn returnValue = CallTargetReturn.GetDefault();
            string           msg         = $"{returnValue} {nameof(Noop3ArgumentsVoidIntegration)}.OnMethodEnd<{typeof(TTarget).FullName}>({instance}, {exception}, {state})";

            Console.WriteLine(msg);
            return(returnValue);
        }
        /// <summary>
        /// OnAsyncMethodEnd callback
        /// </summary>
        /// <typeparam name="TTarget">Type of the target</typeparam>
        /// <param name="instance">Instance value, aka `this` of the instrumented method.</param>
        /// <param name="exception">Exception instance in case the original code threw an exception.</param>
        /// <param name="state">Calltarget state value</param>
        /// <returns>A response value, in an async scenario will be T of Task of T</returns>
        public static CallTargetReturn OnMethodEnd<TTarget>(TTarget instance, Exception exception, CallTargetState state)
        {
            var scope = state.Scope;

            scope.DisposeWithException(exception);

            return CallTargetReturn.GetDefault();
        }
    public static CallTargetReturn EndMethod <TIntegration, TTarget>(TTarget instance, Exception exception, CallTargetState state)
    {
        if (IntegrationOptions <TIntegration, TTarget> .IsIntegrationEnabled)
        {
            return(EndMethodHandler <TIntegration, TTarget> .Invoke(instance, exception, state));
        }

        return(CallTargetReturn.GetDefault());
    }
Exemple #6
0
        public static CallTargetReturn <TReturn> OnMethodEnd <TTarget, TReturn>(TTarget instance, TReturn returnValue, Exception exception, CallTargetState state)
            where TTarget : IInstance
            where TReturn : IReturnValue
        {
            CallTargetReturn <TReturn> rValue = new CallTargetReturn <TReturn>(returnValue);
            string msg = $"{rValue} {nameof(Noop7ArgumentsIntegration)}.OnMethodEnd<{typeof(TTarget).FullName}, {typeof(TReturn).FullName}>({instance}, {returnValue}, {exception}, {state})";

            Console.WriteLine(msg);
            return(rValue);
        }
        public static CallTargetReturn OnMethodEnd <TTarget>(TTarget instance, Exception exception, CallTargetState state)
        {
            CallTargetReturn returnValue = CallTargetReturn.GetDefault();

            Console.WriteLine($"ProfilerOK: EndMethod(0)<{typeof(Noop1ArgumentsVoidIntegration)}, {typeof(TTarget)}>({instance}, {exception?.ToString() ?? "(null)"}, {state})");
            if (instance?.GetType().Name.Contains("ThrowOnEnd") == true)
            {
                Console.WriteLine("Exception thrown.");
                throw new Exception();
            }

            return(returnValue);
        }
Exemple #8
0
        public static CallTargetReturn <TReturn> OnMethodEnd <TTarget, TReturn>(TTarget instance, TReturn returnValue, Exception exception, CallTargetState state)
            where TTarget : IInstance, IDuckType
            where TReturn : IReturnValue
        {
            CallTargetReturn <TReturn> rValue = new CallTargetReturn <TReturn>(returnValue);

            Console.WriteLine($"ProfilerOK: EndMethod(1)<{typeof(Noop9ArgumentsIntegration)}, {typeof(TTarget)}, {typeof(TReturn)}>({instance}, {returnValue}, {exception?.ToString() ?? "(null)"}, {state})");
            if (instance.Instance?.GetType().Name.Contains("ThrowOnEnd") == true)
            {
                Console.WriteLine("Exception thrown.");
                throw new Exception();
            }

            return(rValue);
        }
        internal static CallTargetReturn <TReturn> Invoke(TTarget instance, TReturn returnValue, Exception exception, CallTargetState state)
        {
            if (_continuationGenerator != null)
            {
                returnValue = _continuationGenerator.SetContinuation(instance, returnValue, exception, state);
            }

            if (_invokeDelegate != null)
            {
                CallTargetReturn <TReturn> returnWrap = _invokeDelegate(instance, returnValue, exception, state);
                returnValue = returnWrap.GetReturnValue();
            }

            return(new CallTargetReturn <TReturn>(returnValue));
        }
Exemple #10
0
        /// <summary>
        /// OnMethodEnd callback
        /// </summary>
        /// <typeparam name="TTarget">Type of the target</typeparam>
        /// <param name="instance">Instance value, aka `this` of the instrumented method.</param>
        /// <param name="exception">Exception instance in case the original code threw an exception.</param>
        /// <param name="state">Calltarget state value</param>
        /// <returns>A response value, in an async scenario will be T of Task of T</returns>
        public static CallTargetReturn OnMethodEnd <TTarget>(TTarget instance, Exception exception, CallTargetState state)
        {
            var span = state.Segment;

            if (span is not null)
            {
                if (exception is not null)
                {
                    span.CaptureException(exception);
                }

                span.End();
            }

            return(CallTargetReturn.GetDefault());
        }
Exemple #11
0
        public static CallTargetReturn OnMethodEnd <TTarget>(TTarget instance, Exception?exception, CallTargetState state)
        {
            if (!TracerManager.Instance.DirectLogSubmission.Settings.IsIntegrationEnabled(IntegrationId.ILogger))
            {
                return(CallTargetReturn.GetDefault());
            }

            if (exception is not null)
            {
                // If there's an exception during the constructor, things aren't going to work anyway
                return(CallTargetReturn.GetDefault());
            }

            LoggerFactoryIntegrationCommon <TTarget> .AddDirectSubmissionLoggerProvider(instance);

            return(CallTargetReturn.GetDefault());
        }
    internal static CallTargetReturn <TReturn> Invoke(TTarget instance, TReturn returnValue, Exception exception, CallTargetState state)
    {
        if (_continuationGenerator != null)
        {
            returnValue = _continuationGenerator.SetContinuation(instance, returnValue, exception, state);

            // Restore previous scope if there is a continuation
            // This is used to mimic the ExecutionContext copy from the StateMachine
            Activity.Current = state.PreviousActivity;
        }

        if (_invokeDelegate != null)
        {
            CallTargetReturn <TReturn> returnWrap = _invokeDelegate(instance, returnValue, exception, state);
            returnValue = returnWrap.GetReturnValue();
        }

        return(new CallTargetReturn <TReturn>(returnValue));
    }
        /// <summary>
        /// OnMethodEnd callback
        /// </summary>
        /// <typeparam name="TTarget">Type of the target</typeparam>
        /// <param name="instance">Instance value, aka `this` of the instrumented method.</param>
        /// <param name="exception">Exception instance in case the original code threw an exception.</param>
        /// <param name="state">Calltarget state value</param>
        /// <returns>A response value, in an async scenario will be T of Task of T</returns>
        public static CallTargetReturn OnMethodEnd <TTarget>(TTarget instance, Exception exception, CallTargetState state)
        {
            if (state.State is Action <ITypedDeliveryHandlerShimAction> updateHandlerAction &&
                instance.TryDuckCast <ITypedDeliveryHandlerShimAction>(out var shim))
            {
                try
                {
                    updateHandlerAction.Invoke(shim);
                }
                catch (Exception ex)
                {
                    Logger.Error(ex, "There was an error updating the delivery report handler to ");
                    // Not ideal to close the span here immediately, but as we can't trace the result,
                    // we don't really have a choice
                    state.Scope?.Span.Finish();
                }
            }

            return(CallTargetReturn.GetDefault());
        }
        internal static CallTargetReturn <TReturn> Invoke(TTarget instance, TReturn returnValue, Exception exception, CallTargetState state)
        {
            if (_continuationGenerator != null)
            {
                returnValue = _continuationGenerator.SetContinuation(instance, returnValue, exception, state);

                // Restore previous scope if there is a continuation
                // This is used to mimic the ExecutionContext copy from the StateMachine
                if (((IDatadogTracer)Tracer.Instance).ScopeManager is IScopeRawAccess rawAccess)
                {
                    rawAccess.Active = state.PreviousScope;
                }
            }

            if (_invokeDelegate != null)
            {
                CallTargetReturn <TReturn> returnWrap = _invokeDelegate(instance, returnValue, exception, state);
                returnValue = returnWrap.GetReturnValue();
            }

            return(new CallTargetReturn <TReturn>(returnValue));
        }
 static EndMethodHandler()
 {
     try
     {
         DynamicMethod dynMethod = IntegrationMapper.CreateEndMethodDelegate(typeof(TIntegration), typeof(TTarget));
         if (dynMethod != null)
         {
             _invokeDelegate = (InvokeDelegate)dynMethod.CreateDelegate(typeof(InvokeDelegate));
         }
     }
     catch (Exception ex)
     {
         throw new CallTargetInvokerException(ex);
     }
     finally
     {
         if (_invokeDelegate is null)
         {
             _invokeDelegate = (instance, exception, state) => CallTargetReturn.GetDefault();
         }
     }
 }
            public Task <HttpResponseMessage> PublicSendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
            {
                Task <HttpResponseMessage> result = CachedResult;
                CallTargetState            state  = CallTargetState.GetDefault();
                CallTargetReturn <Task <HttpResponseMessage> > cReturn = CallTargetReturn <Task <HttpResponseMessage> > .GetDefault();

                Exception exception = null;

                try
                {
                    try
                    {
                        state = CallTargetInvoker.BeginMethod <HttpClientHandlerIntegration, HttpClientHandler, HttpRequestMessage, CancellationToken>(this, request, cancellationToken);
                    }
                    catch (Exception ex)
                    {
                        CallTargetInvoker.LogException <HttpClientHandlerIntegration, HttpClientHandler>(ex);
                    }
                    result = CachedResult;
                }
                catch (Exception ex)
                {
                    exception = ex;
                    throw;
                }
                finally
                {
                    try
                    {
                        cReturn = CallTargetInvoker.EndMethod <HttpClientHandlerIntegration, HttpClientHandler, Task <HttpResponseMessage> >(this, result, exception, state);
                        result  = cReturn.GetReturnValue();
                    }
                    catch (Exception ex)
                    {
                        CallTargetInvoker.LogException <HttpClientHandlerIntegration, HttpClientHandler>(ex);
                    }
                }
                return(result);
            }
Exemple #17
0
 /// <summary>
 /// OnMethodEnd callback
 /// </summary>
 /// <typeparam name="TTarget">Type of the target</typeparam>
 /// <param name="instance">Instance value, aka `this` of the instrumented method.</param>
 /// <param name="exception">Exception instance in case the original code threw an exception.</param>
 /// <param name="state">Calltarget state value</param>
 /// <returns>A response value, in an async scenario will be T of Task of T</returns>
 public static CallTargetReturn OnMethodEnd <TTarget>(TTarget instance, Exception exception, CallTargetState state)
 {
     return(CallTargetReturn.GetDefault());
 }
 /// <summary>
 /// OnMethodEnd callback
 /// </summary>
 /// <typeparam name="TTarget">Type of the target</typeparam>
 /// <param name="instance">Instance value, aka `this` of the instrumented method.</param>
 /// <param name="exception">Exception instance in case the original code threw an exception.</param>
 /// <param name="state">Calltarget state value</param>
 /// <returns>Return value of the method</returns>
 public static CallTargetReturn OnMethodEnd <TTarget>(TTarget instance, Exception exception, CallTargetState state)
 {
     Common.FlushSpans(NUnitIntegration.IntegrationId);
     return(CallTargetReturn.GetDefault());
 }