Skip to content

Commit

Permalink
Set debug context config from predefined enum #66
Browse files Browse the repository at this point in the history
  • Loading branch information
IharYakimush committed Feb 6, 2025
1 parent 2e4ecab commit a85f3b5
Show file tree
Hide file tree
Showing 5 changed files with 196 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .runsettings
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</RunConfiguration>
<xUnit>
<LongRunningTestSeconds>40</LongRunningTestSeconds>
<ParallelizeTestCollections>true</ParallelizeTestCollections>
<ParallelizeTestCollections>false</ParallelizeTestCollections>
</xUnit>
<DataCollectionRunSettings>
<DataCollectors>
Expand Down
120 changes: 120 additions & 0 deletions src/Epam.Kafka/DebugContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
// Copyright © 2024 EPAM Systems

namespace Epam.Kafka;

/// <summary>
/// Flags representing debug contexts in librdkafka.
/// </summary>
[Flags]
public enum DebugContext
{
/// <summary>
/// No debug context.
/// </summary>
None = 0x0,

/// <summary>
/// Generic (non-specific) debug context.
/// </summary>
Generic = 0x1,

/// <summary>
/// Broker-related debugging.
/// </summary>
Broker = 0x2,

/// <summary>
/// Topic-related debugging.
/// </summary>
Topic = 0x4,

/// <summary>
/// Metadata-related debugging.
/// </summary>
Metadata = 0x8,

/// <summary>
/// Feature-related debugging.
/// </summary>
Feature = 0x10,

/// <summary>
/// Queue-related debugging.
/// </summary>
Queue = 0x20,

/// <summary>
/// Message-related debugging.
/// </summary>
Msg = 0x40,

/// <summary>
/// Protocol-related debugging.
/// </summary>
Protocol = 0x80,

/// <summary>
/// Consumer group-related debugging.
/// </summary>
Cgrp = 0x100,

/// <summary>
/// Security-related debugging.
/// </summary>
Security = 0x200,

/// <summary>
/// Fetch operation-related debugging.
/// </summary>
Fetch = 0x400,

/// <summary>
/// Interceptor-related debugging.
/// </summary>
Interceptor = 0x800,

/// <summary>
/// Plugin-related debugging.
/// </summary>
Plugin = 0x1000,

/// <summary>
/// Consumer-related debugging.
/// </summary>
Consumer = 0x2000,

/// <summary>
/// Admin-related debugging.
/// </summary>
Admin = 0x4000,

/// <summary>
/// Exactly-once semantics (EOS) debugging.
/// </summary>
Eos = 0x8000,

/// <summary>
/// Mock-related debugging.
/// </summary>
Mock = 0x10000,

/// <summary>
/// Assignor-related debugging.
/// </summary>
Assignor = 0x20000,

/// <summary>
/// Configuration-related debugging.
/// </summary>
Conf = 0x40000,

/// <summary>
/// Telemetry-related debugging.
/// </summary>
Telemetry = 0x80000,

/// <summary>
/// All available debug contexts.
/// </summary>
All = 0xfffff
}
36 changes: 36 additions & 0 deletions src/Epam.Kafka/KafkaConfigExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,42 @@ public static void SetCancellationDelayMaxMs(this ConsumerConfig config, int val
config.Set(DotnetCancellationDelayMaxMsKey, value.ToString("D", CultureInfo.InvariantCulture));
}

/// <summary>
/// Set 'debug' value to config.
/// </summary>
/// <param name="config">The config to update</param>
/// <param name="value">The value</param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
public static void SetDebugContexts(this ClientConfig config, DebugContext value)
{
if (config == null) throw new ArgumentNullException(nameof(config));

if (value == DebugContext.All)
{
config.Debug = DebugContext.All.ToString("G").ToLowerInvariant();
}
else
{
#if NET6_0_OR_GREATER
DebugContext[] contexts = Enum.GetValues<DebugContext>();
#else
Array contexts = Enum.GetValues(typeof(DebugContext));
#endif
List<string> items = new();

foreach (DebugContext item in contexts)
{
if ((item & value) == item && item != DebugContext.None)
{
items.Add(item.ToString("G").ToLowerInvariant());
}
}

config.Debug = string.Join(",", items);
}
}

/// <summary>
/// Clone existing config and optionally replace placeholders if <paramref name="placeholders"/> is not null
/// </summary>
Expand Down
27 changes: 27 additions & 0 deletions tests/Epam.Kafka.Tests/Epam.Kafka.approved.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
namespace Epam.Kafka
{
[System.Flags]
public enum DebugContext
{
None = 0,
Generic = 1,
Broker = 2,
Topic = 4,
Metadata = 8,
Feature = 16,
Queue = 32,
Msg = 64,
Protocol = 128,
Cgrp = 256,
Security = 512,
Fetch = 1024,
Interceptor = 2048,
Plugin = 4096,
Consumer = 8192,
Admin = 16384,
Eos = 32768,
Mock = 65536,
Assignor = 131072,
Conf = 262144,
Telemetry = 524288,
All = 1048575,
}
public interface IKafkaFactory
{
Confluent.Kafka.IConsumer<TKey, TValue> CreateConsumer<TKey, TValue>(Confluent.Kafka.ConsumerConfig config, string? cluster = null, System.Action<Confluent.Kafka.ConsumerBuilder<TKey, TValue>>? configure = null);
Expand Down Expand Up @@ -32,6 +58,7 @@ namespace Epam.Kafka
public static int GetCancellationDelayMaxMs(this Confluent.Kafka.ConsumerConfig config) { }
public static string GetDotnetLoggerCategory(this Confluent.Kafka.Config config) { }
public static void SetCancellationDelayMaxMs(this Confluent.Kafka.ConsumerConfig config, int value) { }
public static void SetDebugContexts(this Confluent.Kafka.ClientConfig config, Epam.Kafka.DebugContext value) { }
public static void SetDotnetLoggerCategory(this Confluent.Kafka.Config config, string value) { }
}
public static class LogExtensions
Expand Down
12 changes: 12 additions & 0 deletions tests/Epam.Kafka.Tests/KafkaConfigExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ public void CancellationDelayMaxMs()
Assert.Throws<ArgumentOutOfRangeException>(() => cfg.GetCancellationDelayMaxMs());
}

[Theory]
[InlineData(DebugContext.None, "")]
[InlineData(DebugContext.All, "all")]
[InlineData(DebugContext.Broker, "broker")]
[InlineData(DebugContext.Broker | DebugContext.Admin, "broker,admin")]
public void SetDebugContext(DebugContext value, string result)
{
ClientConfig cfg = new ClientConfig();
cfg.SetDebugContexts(value);
cfg.Debug.ShouldBe(result);
}

[Fact]
public void LoggerCategory()
{
Expand Down

0 comments on commit a85f3b5

Please sign in to comment.