Пример #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)
            {
                // creates a transaction and ends it on the next call to any of
                // Consumer.Consume(), Consumer.Close() Consumer.Dispose() or Consumer.Unsubscribe() call
                var transaction = KafkaIntegration.CreateConsumerTransaction(
                    Agent.Instance,
                    consumeResult.Topic,
                    consumeResult.Partition,
                    consumeResult.Offset,
                    consumeResult.Message);

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

            return(new CallTargetReturn <TResponse>(response));
        }
Пример #2
0
        /// <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>
        public static CallTargetState OnMethodBegin <TTarget, TTopicPartition, TMessage>(TTarget instance, TTopicPartition topicPartition, TMessage message, CancellationToken cancellationToken)
            where TMessage : IMessage
        {
            var agent = Agent.Instance;

            var span = KafkaIntegration.CreateProducerSpan(
                agent,
                topicPartition.DuckCast <ITopicPartition>(),
                isTombstone: message.Value is null,
                finishOnClose: true);

            if (span is not null)
            {
                KafkaIntegration.TryInjectHeaders <TTopicPartition, TMessage>(agent, span, message);
            }

            return(new CallTargetState(span));
        }
Пример #3
0
        /// <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>
        public static CallTargetState OnMethodBegin <TTarget, TTopicPartition, TMessage, TDeliveryHandler>(TTarget instance, TTopicPartition topicPartition, TMessage message, TDeliveryHandler deliveryHandler)
            where TMessage : IMessage
        {
            var agent = Agent.Instance;

            // manually doing duck cast here so we have access to the _original_ TopicPartition type
            // as a generic parameter, for injecting headers
            var span = KafkaIntegration.CreateProducerSpan(
                agent,
                topicPartition.DuckCast <ITopicPartition>(),
                isTombstone: message.Value is null,
                finishOnClose: deliveryHandler is null);

            if (span is not null)
            {
                KafkaIntegration.TryInjectHeaders <TTopicPartition, TMessage>(agent, span, message);
            }

            return(new CallTargetState(span));
        }
Пример #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.
     KafkaIntegration.CloseConsumerTransaction(Agent.Instance);
     return(CallTargetState.GetDefault());
 }
Пример #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>
 public 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.
     KafkaIntegration.CloseConsumerTransaction(Agent.Instance);
     return(CallTargetState.GetDefault());
 }