/// <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="response">Response 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 response, Exception exception, CallTargetState state)
            where TResponse : IDeliveryResult
        {
            if (state.Scope?.Span?.Tags is KafkaTags tags)
            {
                IDeliveryResult deliveryResult = null;
                if (exception is not null)
                {
                    var produceException = exception.DuckAs <IProduceException>();
                    if (produceException is not null)
                    {
                        deliveryResult = produceException.DeliveryResult;
                    }
                }
                else if (response is not null)
                {
                    deliveryResult = response;
                }

                if (deliveryResult is not null)
                {
                    tags.Partition = deliveryResult.Partition.ToString();
                    tags.Offset    = deliveryResult.Offset.ToString();
                }
            }

            state.Scope.DisposeWithException(exception);
            return(response);
        }
Ejemplo n.º 2
0
        /// <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="response">Response 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 response, Exception exception, CallTargetState state)
            where TResponse : IDeliveryResult
        {
            var span = state.Segment;

            if (span is not null)
            {
                IDeliveryResult deliveryResult = null;
                if (exception is not null)
                {
                    span.CaptureException(exception);

                    var produceException = exception.DuckAs <IProduceException>();
                    if (produceException is not null)
                    {
                        deliveryResult = produceException.DeliveryResult;
                    }
                }
                else if (response is not null)
                {
                    deliveryResult = response;
                }

                if (deliveryResult is not null)
                {
                    span.SetLabel("partition", deliveryResult.Partition.ToString());
                    span.SetLabel("offset", deliveryResult.Offset.ToString());
                }

                span.End();
            }
            return(response);
        }