Example #1
0
        /// <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="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 CallTargetReturn <TResponse> OnMethodEnd <TTarget, TResponse>(TTarget instance, TResponse response, Exception exception, CallTargetState state)
            where TResponse : IConsumeResult, IDuckType
        {
            IConsumeResult consumeResult = response.Instance is not null ? response : null;

            if (exception is not null && exception.TryDuckCast <IConsumeException>(out var consumeException))
            {
                consumeResult = consumeException.ConsumerRecord;
            }

            if (consumeResult is not null)
            {
                // This sets the span as active and either disposes it immediately
                // or disposes it on the next call to Consumer.Consume()
                Scope scope = KafkaHelper.CreateConsumerScope(
                    Tracer.Instance,
                    consumeResult.Topic,
                    consumeResult.Partition,
                    consumeResult.Offset,
                    consumeResult.Message);

                if (!Tracer.Instance.Settings.KafkaCreateConsumerScopeEnabled)
                {
                    // Close and dispose the scope immediately
                    scope.DisposeWithException(exception);
                }
                else if (exception is not null)
                {
                    scope?.Span?.SetException(exception);
                }
            }

            return(new CallTargetReturn <TResponse>(response));
        }
        /// <summary>
        /// OnMethodBegin callback
        /// </summary>
        /// <typeparam name="TTarget">Type of the target</typeparam>
        /// <typeparam name="TTopicPartition">Type of the TopicPartition</typeparam>
        /// <typeparam name="TMessage">Type of the message</typeparam>
        /// <param name="instance">Instance value, aka `this` of the instrumented method.</param>
        /// <param name="topicPartition">TopicPartition instance</param>
        /// <param name="message">Message instance</param>
        /// <param name="cancellationToken">CancellationToken instance</param>
        /// <returns>Calltarget state value</returns>
        internal static CallTargetState OnMethodBegin <TTarget, TTopicPartition, TMessage>(TTarget instance, TTopicPartition topicPartition, TMessage message, CancellationToken cancellationToken)
            where TMessage : IMessage
        {
            Scope scope = KafkaHelper.CreateProducerScope(
                Tracer.Instance,
                topicPartition.DuckCast <ITopicPartition>(),
                isTombstone: message.Value is null,
                finishOnClose: true);

            if (scope is not null)
            {
                KafkaHelper.TryInjectHeaders <TTopicPartition, TMessage>(scope.Span.Context, message);
                return(new CallTargetState(scope));
            }

            return(CallTargetState.GetDefault());
        }
        /// <summary>
        /// OnMethodBegin callback
        /// </summary>
        /// <typeparam name="TTarget">Type of the target</typeparam>
        /// <typeparam name="TTopicPartition">Type of the TopicPartition</typeparam>
        /// <typeparam name="TMessage">Type of the message</typeparam>
        /// <typeparam name="TDeliveryHandler">Type of the delivery handler action</typeparam>
        /// <param name="instance">Instance value, aka `this` of the instrumented method.</param>
        /// <param name="topicPartition">TopicPartition instance</param>
        /// <param name="message">Message instance</param>
        /// <param name="deliveryHandler">Delivery Handler instance</param>
        /// <returns>Calltarget state value</returns>
        internal static CallTargetState OnMethodBegin <TTarget, TTopicPartition, TMessage, TDeliveryHandler>(TTarget instance, TTopicPartition topicPartition, TMessage message, TDeliveryHandler deliveryHandler)
            where TMessage : IMessage
        {
            // manually doing duck cast here so we have access to the _original_ TopicPartition type
            // as a generic parameter, for injecting headers
            Scope scope = KafkaHelper.CreateProducerScope(
                Tracer.Instance,
                topicPartition.DuckCast <ITopicPartition>(),
                isTombstone: message.Value is null,
                finishOnClose: deliveryHandler is null);

            if (scope is not null)
            {
                KafkaHelper.TryInjectHeaders <TTopicPartition, TMessage>(scope.Span.Context, message);
                return(new CallTargetState(scope));
            }

            return(CallTargetState.GetDefault());
        }
Example #4
0
 /// <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.
     KafkaHelper.CloseConsumerScope(Tracer.Instance);
     return(CallTargetState.GetDefault());
 }
Example #5
0
 /// <summary>
 /// OnMethodBegin callback
 /// </summary>
 /// <typeparam name="TTarget">Type of the target</typeparam>
 /// <param name="instance">Instance value, aka `this` of the instrumented method.</param>
 /// <param name="millisecondsTimeout">The maximum period of time the call may block.</param>
 /// <returns>Calltarget state value</returns>
 internal static CallTargetState OnMethodBegin <TTarget>(TTarget instance, int millisecondsTimeout)
 {
     // If we are already in a consumer scope, close it, and start a new one on method exit.
     KafkaHelper.CloseConsumerScope(Tracer.Instance);
     return(CallTargetState.GetDefault());
 }