-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add instrumentation for consumer (#1)
- Loading branch information
Showing
6 changed files
with
190 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/Confluent.Kafka.Extensions.Diagnostics/ConsumerExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
namespace Confluent.Kafka.Extensions.Diagnostics; | ||
|
||
/// <summary> | ||
/// Extension methods for <see cref="IConsumer{TKey,TValue}" />. | ||
/// </summary> | ||
public static class ConsumerExtensions | ||
{ | ||
/// <summary> | ||
/// Consumes a message from the topic with instrumentation. | ||
/// </summary> | ||
public static async Task ConsumeWithInstrumentation<TKey, TValue>(this IConsumer<TKey, TValue> consumer, | ||
Func<ConsumeResult<TKey, TValue>, CancellationToken, Task> action, CancellationToken cancellationToken) | ||
{ | ||
var result = consumer.Consume(cancellationToken); | ||
|
||
var activity = ActivityDiagnosticsHelper.StartConsumeActivity(result.TopicPartition, result.Message); | ||
|
||
try | ||
{ | ||
await action(result, cancellationToken); | ||
} | ||
finally | ||
{ | ||
activity?.Stop(); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Consumes a message from the topic with instrumentation. | ||
/// </summary> | ||
public static void ConsumeWithInstrumentation<TKey, TValue>(this IConsumer<TKey, TValue> consumer, | ||
Action<ConsumeResult<TKey, TValue>> action, int millisecondsTimeout) | ||
{ | ||
var result = consumer.Consume(millisecondsTimeout); | ||
|
||
var activity = ActivityDiagnosticsHelper.StartConsumeActivity(result.TopicPartition, result.Message); | ||
|
||
try | ||
{ | ||
action(result); | ||
} | ||
finally | ||
{ | ||
activity?.Stop(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
src/Confluent.Kafka.Extensions.Diagnostics/ProducerBuilderExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters