Exemple #1
0
        /// <summary>
        /// OnMethodBegin callback
        /// </summary>
        /// <typeparam name="TTarget">Type of the target</typeparam>
        /// <typeparam name="TKey">Type of the message key</typeparam>
        /// <typeparam name="TValue">Type of the message value</typeparam>
        /// <typeparam name="TActionOfDeliveryReport">Type of the delivery report</typeparam>
        /// <param name="instance">Instance value, aka `this` of the instrumented method.</param>
        /// <param name="topic">The topic to which the message was sent</param>
        /// <param name="key">The message key value</param>
        /// <param name="value">The message value</param>
        /// <param name="handler">The delivery handler instance</param>
        /// <returns>Calltarget state value</returns>
        internal static CallTargetState OnMethodBegin <TTarget, TKey, TValue, TActionOfDeliveryReport>(TTarget instance, string topic, TKey key, TValue value, TActionOfDeliveryReport handler)
        {
            if (handler is null)
            {
                // Handled in KafkaProduceSyncIntegration
                return(CallTargetState.GetDefault());
            }

            try
            {
                // The current span should be started in KafkaProduceSyncIntegration.OnMethodBegin
                // The OnMethodBegin and OnMethodEnd of this integration happens between KafkaProduceSyncIntegration.OnMethodBegin
                // and KafkaProduceSyncIntegration.OnMethodEnd, so the consumer span is active for the duration of this integration
                var activeScope = Tracer.Instance?.InternalActiveScope;
                var span        = activeScope?.Span;
                if (span is null)
                {
                    Logger.Warning("Unexpected null span for Kafka Producer with delivery handler");
                    return(CallTargetState.GetDefault());
                }

                var newAction = CachedWrapperDelegate <TActionOfDeliveryReport> .CreateWrapper(handler, span);

                Action <ITypedDeliveryHandlerShimAction> updateHandlerAction = inst => inst.Handler = newAction;

                // store the call to update the handler variable as state
                // so we update it at the _end_ of the constructor
                return(new CallTargetState(scope: activeScope, state: updateHandlerAction));
            }
            catch (Exception ex)
            {
                Logger.Error(ex, "Error creating wrapped delegate for delivery report");
                return(CallTargetState.GetDefault());
            }
        }
        /// <summary>
        /// OnMethodBegin callback
        /// </summary>
        /// <typeparam name="TTarget">Type of the target</typeparam>
        /// <typeparam name="TKey">Type of the message key</typeparam>
        /// <typeparam name="TValue">Type of the message value</typeparam>
        /// <typeparam name="TActionOfDeliveryReport">Type of the delivery report</typeparam>
        /// <param name="instance">Instance value, aka `this` of the instrumented method.</param>
        /// <param name="topic">The topic to which the message was sent</param>
        /// <param name="key">The message key value</param>
        /// <param name="value">The message value</param>
        /// <param name="handler">The delivery handler instance</param>
        /// <returns>Calltarget state value</returns>
        public static CallTargetState OnMethodBegin <TTarget, TKey, TValue, TActionOfDeliveryReport>(TTarget instance, string topic, TKey key, TValue value, TActionOfDeliveryReport handler)
        {
            if (handler is null)
            {
                // Handled in KafkaProduceSyncIntegration
                return(CallTargetState.GetDefault());
            }

            try
            {
                var agent = Agent.Instance;

                // The current span should be started in KafkaProduceSyncIntegration.OnMethodBegin
                // The OnMethodBegin and OnMethodEnd of this integration happens between KafkaProduceSyncIntegration.OnMethodBegin
                // and KafkaProduceSyncIntegration.OnMethodEnd, so the consumer span is active for the duration of this integration
                var span = agent.Tracer.CurrentSpan;
                // span may be null when the Produce call is to an ignored topic.
                if (span is null)
                {
                    return(CallTargetState.GetDefault());
                }

                var newAction = CachedWrapperDelegate <TActionOfDeliveryReport> .CreateWrapper(handler, span);

                Action <ITypedDeliveryHandlerShimAction> updateHandlerAction = inst => inst.Handler = newAction;

                // store the call to update the handler variable as state
                // so we update it at the _end_ of the constructor
                return(new CallTargetState(span, updateHandlerAction));
            }
            catch (Exception ex)
            {
                Agent.Instance.Logger.Error()?.LogException(ex, "Error creating wrapped delegate for delivery report");
                return(CallTargetState.GetDefault());
            }
        }