Skip to content

Commit

Permalink
Update integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tippmar-nr committed Jul 16, 2024
1 parent be36153 commit 7150963
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,12 @@ public async Task<string> ProduceAsync()
await Program.Producer.ProduceAsync();
return "Complete";
}

[HttpGet("bootstrap_server")]
public string GetBootstrapServer()
{
return Program.GetBootstrapServer();
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,18 @@ public static async Task Main(string[] args)
await app.WaitForShutdownAsync();
}

public static string GetBootstrapServer()
{
var broker = Environment.GetEnvironmentVariable("NEW_RELIC_KAFKA_BROKER_NAME");
return $"{broker}:9092";
}

public static void SetupKafka(ILogger logger)
{
Thread.Sleep(15 * 1000); // Waiting for Kafka to get ready

var broker = Environment.GetEnvironmentVariable("NEW_RELIC_KAFKA_BROKER_NAME");
var kafkaConfig = new ConfigurationBuilder().AddInMemoryCollection().Build();
kafkaConfig["bootstrap.servers"] = $"{broker}:9092";
kafkaConfig["bootstrap.servers"] = GetBootstrapServer();
kafkaConfig["group.id"] = "kafka-dotnet-getting-started";
kafkaConfig["auto.offset.reset"] = "earliest";
kafkaConfig["dotnet.cancellation.delay.max.ms"] = "10000";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ public virtual void ExerciseApplication()
GetAndAssertStatusCode(address + "produceasync", System.Net.HttpStatusCode.OK);
}

public string GetBootstrapServer()
{
var address = $"http://localhost:{Port}/kafka/bootstrap_server";
var response = GetString(address);

return response;
}

public void Delay(int seconds)
{
Task.Delay(TimeSpan.FromSeconds(seconds)).GetAwaiter().GetResult();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public abstract class LinuxKafkaTest<T> : NewRelicIntegrationTest<T> where T : K

private readonly string _topicName;
private readonly T _fixture;
private string _bootstrapServer;

protected LinuxKafkaTest(T fixture, ITestOutputHelper output) : base(fixture)
{
Expand All @@ -42,6 +43,8 @@ protected LinuxKafkaTest(T fixture, ITestOutputHelper output) : base(fixture)
_fixture.Delay(15); // wait long enough to ensure kafka and app are ready
_fixture.ExerciseApplication();
_bootstrapServer = _fixture.GetBootstrapServer();
_fixture.Delay(11); // wait long enough to ensure a metric harvest occurs after we exercise the app
_fixture.AgentLog.WaitForLogLine(AgentLogBase.HarvestFinishedLogLineRegex, TimeSpan.FromSeconds(11));
Expand All @@ -65,26 +68,34 @@ public void Test()
var consumeTransactionName = @"OtherTransaction/Message/Kafka/Topic/Consume/Named/" + _topicName;
var produceWebTransactionName = @"WebTransaction/MVC/Kafka/Produce";

var messageBrokerNode = $"MessageBroker/Kafka/Nodes/{_bootstrapServer}";
var messageBrokerNodeProduceTopic = $"MessageBroker/Kafka/Nodes/{_bootstrapServer}/Produce/{_topicName}";
var messageBrokerNodeConsumeTopic = $"MessageBroker/Kafka/Nodes/{_bootstrapServer}/Consume/{_topicName}";

var metrics = _fixture.AgentLog.GetMetrics();
var spans = _fixture.AgentLog.GetSpanEvents();
var produceSpan = spans.FirstOrDefault(s => s.IntrinsicAttributes["name"].Equals(messageBrokerProduce));
var consumeTxnSpan = spans.FirstOrDefault(s => s.IntrinsicAttributes["name"].Equals(consumeTransactionName));

var expectedMetrics = new List<Assertions.ExpectedMetric>
{
new Assertions.ExpectedMetric { metricName = produceWebTransactionName, callCount = 2 }, // includes sync and async actions
new Assertions.ExpectedMetric { metricName = messageBrokerProduce, callCount = 2 },
new Assertions.ExpectedMetric { metricName = messageBrokerProduce, metricScope = produceWebTransactionName, callCount = 2 },
new Assertions.ExpectedMetric { metricName = messageBrokerProduceSerializationKey, callCount = 2 },
new Assertions.ExpectedMetric { metricName = messageBrokerProduceSerializationKey, metricScope = produceWebTransactionName, callCount = 2 },
new Assertions.ExpectedMetric { metricName = messageBrokerProduceSerializationValue, callCount = 2 },
new Assertions.ExpectedMetric { metricName = messageBrokerProduceSerializationValue, metricScope = produceWebTransactionName, callCount = 2 },

new Assertions.ExpectedMetric { metricName = consumeTransactionName, callCount = 2 },
new Assertions.ExpectedMetric { metricName = messageBrokerConsume, callCount = 2 },
new Assertions.ExpectedMetric { metricName = messageBrokerConsume, metricScope = consumeTransactionName, callCount = 2 },
new Assertions.ExpectedMetric { metricName = "Supportability/TraceContext/Create/Success", callCount = 2 },
new Assertions.ExpectedMetric { metricName = "Supportability/TraceContext/Accept/Success", callCount = 2 },
new() { metricName = produceWebTransactionName, callCount = 2 }, // includes sync and async actions
new() { metricName = messageBrokerProduce, callCount = 2 },
new() { metricName = messageBrokerProduce, metricScope = produceWebTransactionName, callCount = 2 },
new() { metricName = messageBrokerProduceSerializationKey, callCount = 2 },
new() { metricName = messageBrokerProduceSerializationKey, metricScope = produceWebTransactionName, callCount = 2 },
new() { metricName = messageBrokerProduceSerializationValue, callCount = 2 },
new() { metricName = messageBrokerProduceSerializationValue, metricScope = produceWebTransactionName, callCount = 2 },

new() { metricName = consumeTransactionName, callCount = 2 },
new() { metricName = messageBrokerConsume, callCount = 2 },
new() { metricName = messageBrokerConsume, metricScope = consumeTransactionName, callCount = 2 },
new() { metricName = "Supportability/TraceContext/Create/Success", callCount = 2 },
new() { metricName = "Supportability/TraceContext/Accept/Success", callCount = 2 },

new() { metricName = messageBrokerNode, callCount = 4 },
new() { metricName = messageBrokerNodeProduceTopic, callCount = 2 },
new() { metricName = messageBrokerNodeConsumeTopic, callCount = 2 }
};

NrAssert.Multiple(
Expand Down

0 comments on commit 7150963

Please sign in to comment.