/// <summary> /// OnMethodBegin callback /// </summary> /// <typeparam name="TTarget">Type of the target</typeparam> /// <param name="instance">Instance value, aka `this` of the instrumented method.</param> /// <returns>Calltarget state value</returns> public static CallTargetState OnMethodBegin <TTarget>(TTarget instance) { // If we are already in a consumer scope, close it. KafkaIntegration.CloseConsumerTransaction(Agent.Instance); return(CallTargetState.GetDefault()); }
/// <summary> /// OnMethodEnd callback /// </summary> /// <typeparam name="TTarget">Type of the target</typeparam> /// <typeparam name="TReturn">Type of the return value</typeparam> /// <param name="instance">Instance value, aka `this` of the instrumented method.</param> /// <param name="returnValue">Task of HttpResponse message instance</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 <TReturn> OnMethodEnd <TTarget, TReturn>(TTarget instance, TReturn returnValue, Exception exception, CallTargetState state) { state.Scope.DisposeWithException(exception); return(new CallTargetReturn <TReturn>(returnValue)); }
/// <summary> /// OnAsyncMethodEnd callback /// </summary> /// <typeparam name="TTarget">Type of the target</typeparam> /// <typeparam name="TReturn">Type of the return value, in an async scenario will be T of Task of T</typeparam> /// <param name="instance">Instance value, aka `this` of the instrumented method.</param> /// <param name="returnValue">Return value instance</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 TReturn OnAsyncMethodEnd <TTarget, TReturn>(TTarget instance, TReturn returnValue, Exception exception, CallTargetState state) { DbSpanFactory <TTarget> .EndSpan(Agent.Instance, (IDbCommand)instance, (ISpan)state.Segment, exception); return(returnValue); }
internal static CallTargetReturn Invoke(TTarget instance, Exception exception, CallTargetState state) { return(_invokeDelegate(instance, exception, state)); }
/// <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()); }
/// <summary> /// OnMethodEnd callback /// </summary> /// <typeparam name="TTarget">Type of the target</typeparam> /// <typeparam name="TResponse">Type of the response</typeparam> /// <param name="instance">Instance value, aka `this` of the instrumented method.</param> /// <param name="responseMessage">HttpResponse message instance</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 <TResponse> OnMethodEnd <TTarget, TResponse>(TTarget instance, TResponse responseMessage, Exception exception, CallTargetState state) where TResponse : IHttpResponseMessage { return(new CallTargetReturn <TResponse>(HttpMessageHandlerCommon.OnMethodEnd(instance, responseMessage, exception, state))); }
static BeginMethodHandler() { try { DynamicMethod dynMethod = IntegrationMapper.CreateBeginMethodDelegate(typeof(TIntegration), typeof(TTarget), new[] { typeof(TArg1), typeof(TArg2), typeof(TArg3), typeof(TArg4), typeof(TArg5), typeof(TArg6) }); if (dynMethod != null) { _invokeDelegate = (InvokeDelegate)dynMethod.CreateDelegate(typeof(InvokeDelegate)); } } catch (Exception ex) { throw new CallTargetInvokerException(ex); } finally { if (_invokeDelegate is null) { _invokeDelegate = (instance, arg1, arg2, arg3, arg4, arg5, arg6) => CallTargetState.GetDefault(); } } }
/// <summary> /// OnAsyncMethodEnd callback /// </summary> /// <typeparam name="TTarget">Type of the target</typeparam> /// <typeparam name="TResponse">Type of the response, in an async scenario will be T of Task of T</typeparam> /// <param name="instance">Instance value, aka `this` of the instrumented method.</param> /// <param name="responseMessage">HttpResponse message instance</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 TResponse OnAsyncMethodEnd <TTarget, TResponse>(TTarget instance, TResponse responseMessage, Exception exception, CallTargetState state) where TResponse : IHttpResponseMessage { Scope scope = state.Scope; if (scope is null) { return(responseMessage); } try { scope.Span.SetHttpStatusCode(responseMessage.StatusCode, isServer: false); if (exception != null) { scope.Span.SetException(exception); } } finally { scope.Dispose(); } return(responseMessage); }
private static CallTargetState CreateCosmosDbCallState <TTarget, TQueryDefinition>(TTarget instance, TQueryDefinition queryDefinition, string containerId, string databaseId, string endpoint) { if (!Tracer.Instance.Settings.IsIntegrationEnabled(IntegrationId)) { // integration disabled, don't create a scope, skip this trace return(CallTargetState.GetDefault()); } try { string query; if (queryDefinition is string queryDefinitionString) { query = queryDefinitionString; } else { var success = queryDefinition.TryDuckCast <QueryDefinitionStruct>(out var queryDefinitionObj); query = queryDefinitionObj.QueryText; } var tracer = Tracer.Instance; var parent = tracer.ActiveScope?.Span; if (parent != null && parent.Type == SpanTypes.Sql && parent.GetTag(Tags.DbType) == "cosmosdb" && parent.ResourceName == query) { // we are already instrumenting this, // don't instrument nested methods that belong to the same stacktrace return(new CallTargetState(null)); } var tags = new CosmosDbTags { ContainerId = containerId, DatabaseId = databaseId, Host = endpoint, DbType = "cosmosdb", }; tags.SetAnalyticsSampleRate(IntegrationId, tracer.Settings, enabledWithGlobalSetting: false); var serviceName = tracer.Settings.GetServiceName(tracer, ServiceName); var scope = tracer.StartActiveInternal(OperationName, tags: tags, serviceName: serviceName); var span = scope.Span; span.ResourceName = query; span.Type = SpanTypes.Sql; tracer.TracerManager.Telemetry.IntegrationGeneratedSpan(IntegrationId); return(new CallTargetState(scope)); } catch (Exception ex) { Log.Error(ex, "Error creating or populating scope."); } return(new CallTargetState(null)); }
internal static CallTargetState Invoke(TTarget instance, TArg1 arg1, TArg2 arg2, TArg3 arg3) { return(CallTargetState.WithPreviousScope(Tracer.Instance.ActiveScope, _invokeDelegate(instance, arg1, arg2, arg3))); }
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); }
/// <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>CallTargetReturn</returns> public static CallTargetReturn OnMethodEnd <TTarget>(TTarget instance, Exception exception, CallTargetState state) { state.Scope.DisposeWithException(exception); return(CallTargetReturn.GetDefault()); }
public static TReturn OnAsyncMethodEnd <TTarget, TReturn>(TTarget instance, TReturn returnValue, Exception exception, CallTargetState state) where TTarget : IInstance, IDuckType where TReturn : IReturnValue, IDuckType { string msg = $"{returnValue} {nameof(Noop7ArgumentsIntegration)}.OnAsyncMethodEnd<{typeof(TTarget).FullName}, {typeof(TReturn).FullName}>({instance}, {returnValue}, {exception}, {state})"; Console.WriteLine(msg); return(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) => CallTargetReturn.GetDefault();
public static TReturn OnAsyncMethodEnd <TTarget, TReturn>(TTarget instance, TReturn returnValue, Exception exception, CallTargetState state) { return(returnValue); }
/// <summary> /// OnAsyncMethodEnd callback /// </summary> /// <typeparam name="TTarget">Type of the target</typeparam> /// <typeparam name="TResponse">Type of the response, in an async scenario will be T of Task of T</typeparam> /// <param name="instance">Instance value, aka `this` of the instrumented method.</param> /// <param name="responseMessage">HttpResponse message instance</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 TResponse OnAsyncMethodEnd <TTarget, TResponse>(TTarget instance, TResponse responseMessage, Exception exception, CallTargetState state) { var scope = state.Scope; if (scope is null) { return(responseMessage); } var controllerContext = (IHttpControllerContext)state.State; // some fields aren't set till after execution, so populate anything missing AspNetWebApi2Integration.UpdateSpan(controllerContext, scope.Span, (AspNetTags)scope.Span.Tags, Enumerable.Empty <KeyValuePair <string, string> >()); if (exception != null) { scope.Span.SetException(exception); // We don't have access to the final status code at this point // Ask the HttpContext to call us back to that we can get it var httpContext = HttpContext.Current; if (httpContext != null) { // We don't know how long it'll take for ASP.NET to invoke the callback, // so we store the real finish time var now = scope.Span.Context.TraceContext.UtcNow; httpContext.AddOnRequestCompleted(h => OnRequestCompleted(h, scope, now)); } else { // Looks like we won't be able to get the final status code scope.Dispose(); } } else { var httpContext = HttpContext.Current; if (httpContext != null && HttpRuntime.UsingIntegratedPipeline) { scope.Span.SetHeaderTags <IHeadersCollection>(httpContext.Response.Headers.Wrap(), Tracer.Instance.Settings.HeaderTags, defaultTagPrefix: SpanContextPropagator.HttpResponseHeadersTagPrefix); } scope.Span.SetHttpStatusCode(responseMessage.DuckCast <HttpResponseMessageStruct>().StatusCode, isServer: true); scope.Dispose(); } return(responseMessage); }
public async Task ExceptionGenericTest() { Exception ex = null; // Normal ex = await Assert.ThrowsAsync <CustomException>(() => GetPreviousTask().AsTask()); Assert.Equal("Internal Test Exception", ex.Message); // Using the continuation var tcg = new ValueTaskContinuationGenerator <ValueTaskContinuationGeneratorTests, ValueTaskContinuationGeneratorTests, ValueTask <bool>, bool>(); ex = await Assert.ThrowsAsync <CustomException>(() => tcg.SetContinuation(this, GetPreviousTask(), null, CallTargetState.GetDefault()).AsTask()); Assert.Equal("Internal Test Exception", ex.Message); async ValueTask <bool> GetPreviousTask() { await Task.Delay(1000).ConfigureAwait(false); throw new CustomException("Internal Test Exception"); } }
/// <summary> /// OnMethodEnd callback /// </summary> /// <typeparam name="TTarget">Type of the target</typeparam> /// <typeparam name="TResult">Type of the result</typeparam> /// <param name="instance">Instance value, aka `this` of the instrumented method.</param> /// <param name="messageResult">message result</param> /// <param name="exception">Exception instance in case the original code threw an exception.</param> /// <param name="state">Calltarget state value</param> /// <returns>CallTargetReturn</returns> public static CallTargetReturn <TResult> OnMethodEnd <TTarget, TResult>(TTarget instance, TResult messageResult, Exception exception, CallTargetState state) { state.Scope.DisposeWithException(exception); return(new CallTargetReturn <TResult>(messageResult)); }
/// <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="returnValue">Return value</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 decimal OnAsyncMethodEnd <TTarget>(TTarget instance, decimal returnValue, Exception exception, CallTargetState state) { Scope scope = state.Scope; if (scope != null) { TestInvokerStruct invokerInstance = instance.As <TestInvokerStruct>(); XUnitIntegration.FinishScope(scope, invokerInstance.Aggregator); } return(returnValue); }
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)); }
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)); }
/// <summary> /// OnAsyncMethodEnd callback /// </summary> /// <typeparam name="TTarget">Type of the target</typeparam> /// <typeparam name="TReturn">Type of the return value</typeparam> /// <param name="instance">Instance value, aka `this` of the instrumented method.</param> /// <param name="returnValue">Return value</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 TReturn OnAsyncMethodEnd <TTarget, TReturn>(TTarget instance, TReturn returnValue, Exception exception, CallTargetState state) { var scope = state.Scope; scope.DisposeWithException(exception); return(returnValue); }
/// <summary> /// OnMethodBegin callback /// </summary> /// <typeparam name="TTarget">Type of the target</typeparam> /// <param name="instance">Instance value, aka `this` of the instrumented method.</param> /// <returns>Calltarget state value</returns> public static CallTargetState OnMethodBegin <TTarget>(TTarget instance) { return(CallTargetState.GetDefault()); }
/// <summary> /// OnMethodBegin callback /// </summary> /// <typeparam name="TTarget">Type of the target</typeparam> /// <param name="instance">Instance value, aka `this` of the instrumented method.</param> /// <returns>Calltarget state value</returns> internal static CallTargetState OnMethodBegin <TTarget>(TTarget instance) { // If we are already in a consumer scope, close it. KafkaHelper.CloseConsumerScope(Tracer.Instance); return(CallTargetState.GetDefault()); }
/// <summary> /// OnAsyncMethodEnd callback /// </summary> /// <typeparam name="TTarget">Type of the target</typeparam> /// <typeparam name="TReturn">Type of the return value</typeparam> /// <param name="instance">Instance value, aka `this` of the instrumented method.</param> /// <param name="returnValue">Return value</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 <TReturn> OnMethodEnd <TTarget, TReturn>(TTarget instance, TReturn returnValue, Exception exception, CallTargetState state) { if (MsTestIntegration.IsEnabled) { Scope scope = state.Scope; if (scope != null) { Array returnValueArray = returnValue as Array; if (returnValueArray.Length == 1) { object testResultObject = returnValueArray.GetValue(0); if (testResultObject != null && testResultObject.TryDuckCast <TestResultStruct>(out var testResult)) { string errorMessage = null; string errorStackTrace = null; if (testResult.TestFailureException != null) { Exception testException = testResult.TestFailureException.InnerException ?? testResult.TestFailureException; string testExceptionName = testException.GetType().Name; if (testExceptionName != "UnitTestAssertException" && testExceptionName != "AssertInconclusiveException") { scope.Span.SetException(testException); } errorMessage = testException.Message; errorStackTrace = testException.ToString(); } switch (testResult.Outcome) { case UnitTestOutcome.Error: case UnitTestOutcome.Failed: case UnitTestOutcome.Timeout: scope.Span.SetTag(TestTags.Status, TestTags.StatusFail); scope.Span.Error = true; scope.Span.SetTag(Tags.ErrorMsg, errorMessage); scope.Span.SetTag(Tags.ErrorStack, errorStackTrace); break; case UnitTestOutcome.Inconclusive: case UnitTestOutcome.NotRunnable: scope.Span.SetTag(TestTags.Status, TestTags.StatusSkip); scope.Span.SetTag(TestTags.SkipReason, errorMessage); break; case UnitTestOutcome.Passed: scope.Span.SetTag(TestTags.Status, TestTags.StatusPass); break; } } } if (exception != null) { scope.Span.SetException(exception); scope.Span.SetTag(TestTags.Status, TestTags.StatusFail); } scope.Dispose(); } } return(new CallTargetReturn <TReturn>(returnValue)); }
/// <summary> /// OnMethodBegin callback /// </summary> /// <typeparam name="TTarget">Type of the target</typeparam> /// <typeparam name="TTestMethod">Type of the ITestMethod</typeparam> /// <param name="instance">Instance value, aka `this` of the instrumented method.</param> /// <param name="testMethod">Test method instance</param> /// <returns>Calltarget state value</returns> public static CallTargetState OnMethodBegin <TTarget, TTestMethod>(TTarget instance, TTestMethod testMethod) { return(CallTargetState.GetDefault()); }
public static TReturn OnAsyncMethodEnd <TTarget, TReturn>(TTarget instance, TReturn returnValue, Exception exception, CallTargetState state) where TTarget : IInstance, IDuckType where TReturn : IReturnValue { Console.WriteLine($"ProfilerOK: EndMethodAsync(1)<{typeof(Noop2ArgumentsIntegration)}, {typeof(TTarget)}, {typeof(TReturn)}>({instance}, {returnValue}, {exception?.ToString() ?? "(null)"}, {state})"); if (instance.Instance?.GetType().Name.Contains("ThrowOnAsyncEnd") == true) { Console.WriteLine("Exception thrown."); throw new Exception(); } return(returnValue); }
/// <summary> /// OnAsyncMethodEnd callback /// </summary> /// <typeparam name="TTarget">Type of the target</typeparam> /// <typeparam name="TReturn">Type of the return value</typeparam> /// <param name="instance">Instance value, aka `this` of the instrumented method.</param> /// <param name="returnValue">Return value</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 <TReturn> OnMethodEnd <TTarget, TReturn>(TTarget instance, TReturn returnValue, Exception exception, CallTargetState state) { if (Common.TestTracer.Settings.IsIntegrationEnabled(IntegrationId)) { Scope scope = Common.TestTracer.ActiveScope; if (scope != null) { Array returnValueArray = returnValue as Array; if (returnValueArray.Length == 1) { object testResultObject = returnValueArray.GetValue(0); if (testResultObject != null && testResultObject.TryDuckCast <TestResultStruct>(out var testResult) && testResult.TestFailureException != null) { Exception testException = testResult.TestFailureException.InnerException ?? testResult.TestFailureException; string testExceptionName = testException.GetType().Name; if (testExceptionName != "UnitTestAssertException" && testExceptionName != "AssertInconclusiveException") { scope.Span.SetException(testException); } } } if (exception != null) { scope.Span.SetException(exception); } } } return(new CallTargetReturn <TReturn>(returnValue)); }
public override TReturn SetContinuation(TTarget instance, TReturn returnValue, Exception exception, CallTargetState state) { if (_continuation == null) { return(returnValue); } if (exception != null || returnValue == null) { _continuation(instance, default, exception, state);
static BeginMethodHandler() { try { Type tArg1ByRef = typeof(TArg1).IsByRef ? typeof(TArg1) : typeof(TArg1).MakeByRefType(); Type tArg2ByRef = typeof(TArg2).IsByRef ? typeof(TArg2) : typeof(TArg2).MakeByRefType(); Type tArg3ByRef = typeof(TArg3).IsByRef ? typeof(TArg3) : typeof(TArg3).MakeByRefType(); Type tArg4ByRef = typeof(TArg4).IsByRef ? typeof(TArg4) : typeof(TArg4).MakeByRefType(); Type tArg5ByRef = typeof(TArg5).IsByRef ? typeof(TArg5) : typeof(TArg5).MakeByRefType(); Type tArg6ByRef = typeof(TArg6).IsByRef ? typeof(TArg6) : typeof(TArg6).MakeByRefType(); DynamicMethod dynMethod = IntegrationMapper.CreateBeginMethodDelegate(typeof(TIntegration), typeof(TTarget), new[] { tArg1ByRef, tArg2ByRef, tArg3ByRef, tArg4ByRef, tArg5ByRef, tArg6ByRef }); if (dynMethod != null) { _invokeDelegate = (InvokeDelegate)dynMethod.CreateDelegate(typeof(InvokeDelegate)); } } catch (Exception ex) { throw new CallTargetInvokerException(ex); } finally { if (_invokeDelegate is null) { _invokeDelegate = (TTarget instance, ref TArg1 arg1, ref TArg2 arg2, ref TArg3 arg3, ref TArg4 arg4, ref TArg5 arg5, ref TArg6 arg6) => CallTargetState.GetDefault(); } } }