Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Increase logging of testcontainer #2468

Merged
merged 4 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@
<PackageVersion Include="StackExchange.Redis" Version="2.7.20" />
<PackageVersion Include="Swashbuckle.AspNetCore" Version="6.5.0" />
<PackageVersion Include="Testcontainers.Elasticsearch" Version="3.7.0" />
<PackageVersion Include="Testcontainers.Kafka" Version="3.7.0" />
<PackageVersion Include="Testcontainers.MongoDb" Version="3.7.0" />
<PackageVersion Include="Testcontainers.MsSql" Version="3.7.0" />
<PackageVersion Include="Testcontainers.MySql" Version="3.7.0" />
<PackageVersion Include="Testcontainers.Oracle" Version="3.7.0" />
<PackageVersion Include="Testcontainers.PostgreSql" Version="3.7.0" />
<PackageVersion Include="Testcontainers.RabbitMq" Version="3.7.0" />
<PackageVersion Include="Testcontainers.Redis" Version="3.7.0" />
<PackageVersion Include="Testcontainers.Kafka" Version="3.10.0" />
<PackageVersion Include="Testcontainers.MongoDb" Version="3.10.0" />
<PackageVersion Include="Testcontainers.MsSql" Version="3.10.0" />
<PackageVersion Include="Testcontainers.MySql" Version="3.10.0" />
<PackageVersion Include="Testcontainers.Oracle" Version="3.10.0" />
<PackageVersion Include="Testcontainers.PostgreSql" Version="3.10.0" />
<PackageVersion Include="Testcontainers.RabbitMq" Version="3.10.0" />
<PackageVersion Include="Testcontainers.Redis" Version="3.10.0" />
<PackageVersion Include="YamlDotNet" Version="11.2.1" />
<PackageVersion Include="xunit" Version="2.7.0" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.5.7" PrivateAssets="All" IncludeAssets="runtime; build; native; contentfiles; analyzers; buildtransitive" />
Expand Down
2 changes: 1 addition & 1 deletion sample/StackExchangeRedisSample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,5 @@ await Agent.Tracer.CaptureTransaction("Set and Get String", ApiConstants.TypeDb,
await Task.Delay(TimeSpan.FromMilliseconds(100));
}
Console.WriteLine("Stopping Redis Container...");
await container.StopAsync();
await container.StopAsync(ctx.Token);
Console.WriteLine("Exiting");
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,15 @@ public AzureCredentialsFactAttribute()
Skip = "Azure credentials not available. If running locally, run `az login` to login";
}
}

public class CosmosDbFactAttribute : FactAttribute
{
public CosmosDbFactAttribute()
{
if (AzureCredentials.Instance is Unauthenticated)
Skip = "Azure credentials not available. If running locally, run `az login` to login";
if (TestEnvironment.IsCi)
Skip = "CosmosDb tests are temporarily disabled on CI environments";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ public AzureCosmosDbTestEnvironment(IMessageSink messageSink)
if (credentials is Unauthenticated)
return;

// TODO remove this and move our cosmosdb tests over to a always ready serverless
if (TestEnvironment.IsCi)
return;

_terraform = new TerraformResources(terraformResourceDirectory, credentials, messageSink);

var resourceGroupName = AzureResources.CreateResourceGroupName("cosmosdb-test");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public MicrosoftAzureCosmosTests(AzureCosmosDbTestEnvironment environment, ITest
});
}

[AzureCredentialsFact]
[CosmosDbFact]
public async Task Capture_Span_When_Create_Database()
{
await _agent.Tracer.CaptureTransaction("Create CosmosDb Database", ApiConstants.TypeDb, async () =>
Expand All @@ -45,7 +45,7 @@ await _agent.Tracer.CaptureTransaction("Create CosmosDb Database", ApiConstants.
AssertSpan("Create database");
}

[AzureCredentialsFact]
[CosmosDbFact]
public async Task Capture_Span_When_Delete_Database()
{
var db = await CreateDatabaseAsync();
Expand All @@ -57,7 +57,7 @@ await _agent.Tracer.CaptureTransaction("Delete CosmosDb Database", ApiConstants.
AssertSpan($"Delete database {db.Id}", db.Id);
}

[AzureCredentialsFact]
[CosmosDbFact]
public async Task Capture_Span_When_Get_Database()
{
var db = await CreateDatabaseAsync();
Expand All @@ -69,7 +69,7 @@ await _agent.Tracer.CaptureTransaction("Get CosmosDb Database", ApiConstants.Typ
AssertSpan($"Get database {db.Id}", db.Id);
}

[AzureCredentialsFact]
[CosmosDbFact]
public async Task Capture_Span_When_List_Databases()
{
await _agent.Tracer.CaptureTransaction("List CosmosDb Databases", ApiConstants.TypeDb, async () =>
Expand All @@ -83,7 +83,7 @@ await _agent.Tracer.CaptureTransaction("List CosmosDb Databases", ApiConstants.T
AssertSpan("List databases");
}

[AzureCredentialsFact]
[CosmosDbFact]
public async Task Capture_Span_When_Create_Collection()
{
var db = await CreateDatabaseAsync();
Expand All @@ -95,7 +95,7 @@ await _agent.Tracer.CaptureTransaction("Create CosmosDb Collection", ApiConstant
AssertSpan($"Create collection {db.Id}", db.Id);
}

[AzureCredentialsFact]
[CosmosDbFact]
public async Task Capture_Span_When_Delete_Collection()
{
var db = await CreateDatabaseAsync();
Expand All @@ -111,7 +111,7 @@ await _agent.Tracer.CaptureTransaction("Delete CosmosDb Collection", ApiConstant

private string RandomName() => Guid.NewGuid().ToString("N");

[AzureCredentialsFact]
[CosmosDbFact]
public async Task Capture_Span_When_List_Collections()
{
var db = await CreateDatabaseAsync();
Expand All @@ -131,7 +131,7 @@ await _agent.Tracer.CaptureTransaction("List CosmosDb Collections", ApiConstants
AssertSpan($"List collections {db.Id}", db.Id);
}

[AzureCredentialsFact]
[CosmosDbFact]
public async Task Capture_Span_When_Create_Document()
{
var db = await CreateDatabaseAsync();
Expand All @@ -147,7 +147,7 @@ await _agent.Tracer.CaptureTransaction("Create CosmosDb Item", ApiConstants.Type
AssertSpan($"Create/query document {db.Id} {container.Id}", db.Id, 2);
}

[AzureCredentialsFact]
[CosmosDbFact]
public async Task Capture_Span_When_Upsert_Document()
{
var db = await CreateDatabaseAsync();
Expand All @@ -163,7 +163,7 @@ await _agent.Tracer.CaptureTransaction("Create CosmosDb Item", ApiConstants.Type
AssertSpan($"Create/query document {db.Id} {container.Id}", db.Id, 2);
}

[AzureCredentialsFact]
[CosmosDbFact]
public async Task Capture_Span_When_Delete_Document()
{
var db = await CreateDatabaseAsync();
Expand All @@ -181,7 +181,7 @@ await _agent.Tracer.CaptureTransaction("Delete CosmosDb Item", ApiConstants.Type
AssertSpan($"Delete document {db.Id} {container.Id}", db.Id);
}

[AzureCredentialsFact]
[CosmosDbFact]
public async Task Capture_Span_When_Replace_Document()
{
var db = await CreateDatabaseAsync();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public MicrosoftAzureDocumentDbTests(AzureCosmosDbTestEnvironment environment, I
_client = new DocumentClient(new Uri(environment.Endpoint), environment.PrimaryMasterKey);
}

[AzureCredentialsFact]
[CosmosDbFact]
public async Task Capture_Span_When_Create_Database()
{
await _agent.Tracer.CaptureTransaction("Create CosmosDb Database", ApiConstants.TypeDb, async () =>
Expand All @@ -42,7 +42,7 @@ await _agent.Tracer.CaptureTransaction("Create CosmosDb Database", ApiConstants.
AssertSpan("Create database");
}

[AzureCredentialsFact]
[CosmosDbFact]
public async Task Capture_Span_When_Delete_Database()
{
var db = await CreateDatabaseAsync();
Expand All @@ -54,7 +54,7 @@ await _agent.Tracer.CaptureTransaction("Delete CosmosDb Database", ApiConstants.
AssertSpan($"Delete database {db.Id}", db.Id);
}

[AzureCredentialsFact]
[CosmosDbFact]
public async Task Capture_Span_When_Get_Database()
{
var db = await CreateDatabaseAsync();
Expand All @@ -66,7 +66,7 @@ await _agent.Tracer.CaptureTransaction("Get CosmosDb Database", ApiConstants.Typ
AssertSpan($"Get database {db.Id}", db.Id);
}

[AzureCredentialsFact]
[CosmosDbFact]
public async Task Capture_Span_When_List_Databases()
{
await _agent.Tracer.CaptureTransaction("List CosmosDb Databases", ApiConstants.TypeDb, async () =>
Expand All @@ -80,7 +80,7 @@ await _agent.Tracer.CaptureTransaction("List CosmosDb Databases", ApiConstants.T
AssertSpan("List databases");
}

[AzureCredentialsFact]
[CosmosDbFact]
public async Task Capture_Span_When_Create_Collection()
{
var db = await CreateDatabaseAsync();
Expand All @@ -92,7 +92,7 @@ await _agent.Tracer.CaptureTransaction("Create CosmosDb Collection", ApiConstant
AssertSpan($"Create collection {db.Id}", db.Id);
}

[AzureCredentialsFact]
[CosmosDbFact]
public async Task Capture_Span_When_Delete_Collection()
{
var db = await CreateDatabaseAsync();
Expand All @@ -108,7 +108,7 @@ await _agent.Tracer.CaptureTransaction("Delete CosmosDb Collection", ApiConstant

private string RandomName() => Guid.NewGuid().ToString("N");

[AzureCredentialsFact]
[CosmosDbFact]
public async Task Capture_Span_When_List_Collections()
{
var db = await CreateDatabaseAsync();
Expand All @@ -124,7 +124,7 @@ await _agent.Tracer.CaptureTransaction("List CosmosDb Collections", ApiConstants
AssertSpan($"List collections {db.Id}", db.Id);
}

[AzureCredentialsFact]
[CosmosDbFact]
public async Task Capture_Span_When_Create_Document()
{
var db = await CreateDatabaseAsync();
Expand All @@ -140,7 +140,7 @@ await _agent.Tracer.CaptureTransaction("Create CosmosDb Item", ApiConstants.Type
AssertSpan($"Create/query document {db.Id} {container.Id}", db.Id, 2);
}

[AzureCredentialsFact]
[CosmosDbFact]
public async Task Capture_Span_When_Delete_Document()
{
var db = await CreateDatabaseAsync();
Expand All @@ -158,7 +158,7 @@ await _client.DeleteDocumentAsync(
AssertSpan($"Delete document {db.Id} {container.Id}", db.Id, 2);
}

[AzureCredentialsFact]
[CosmosDbFact]
public async Task Capture_Span_When_Upsert_Document()
{
var db = await CreateDatabaseAsync();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,49 @@
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information

using System;
using System.Threading;
using System.Threading.Tasks;
using Testcontainers.Elasticsearch;
using Xunit;
using Xunit.Abstractions;
using Xunit.Sdk;

namespace Elastic.Apm.Elasticsearch.Tests
{
public sealed class ElasticsearchFixture : IAsyncLifetime
public sealed class ElasticsearchFixture(IMessageSink sink) : IAsyncLifetime
{
private readonly ElasticsearchContainer _container = new ElasticsearchBuilder().Build();

public string ConnectionString => _container.GetConnectionString();
public string ConnectionString => _container.GetConnectionString();

public Task InitializeAsync() => _container.StartAsync();
public async Task InitializeAsync()
{
await _container.StartAsync();

var (stdOut, stdErr) = await _container.GetLogsAsync();

sink.OnMessage(new DiagnosticMessage(stdOut));
sink.OnMessage(new DiagnosticMessage(stdErr));
}

public Task DisposeAsync() => _container.DisposeAsync().AsTask();
public async Task DisposeAsync()
{
var cts = new CancellationTokenSource();
cts.CancelAfter(TimeSpan.FromMinutes(2));

try
{
sink.OnMessage(new DiagnosticMessage($"Stopping {nameof(ElasticsearchFixture)}"));
await _container.StopAsync(cts.Token);
}
catch (Exception e)
{
sink.OnMessage(new DiagnosticMessage(e.Message));
}

sink.OnMessage(new DiagnosticMessage($"Disposing {nameof(ElasticsearchFixture)}"));
await _container.DisposeAsync();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ public async Task Elasticsearch_Span_Should_Align_With_Spec()

var spans = payloadSender.SpansOnFirstTransaction;

var parentSpan = spans.Where(s => s.ParentId == s.TransactionId).Single();
// could be 2 because product check generates a span too
var parentSpans = spans.Where(s => s.ParentId == s.TransactionId && s.Action == "request").ToList();

parentSpans.Should().HaveCount(1);
var parentSpan = parentSpans[0];

parentSpan.Name.Should().StartWith("Elasticsearch: ");
parentSpan.Action.Should().Be("request");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,32 @@
// Elasticsearch B.V licenses this file, including any modifications, to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.

using System;
using System.Threading;
using System.Threading.Tasks;
using MongoDB.Driver;
using Testcontainers.MongoDb;
using Xunit;
using Xunit.Abstractions;
using Xunit.Sdk;

namespace Elastic.Apm.MongoDb.Tests.Fixture
{
public class MongoFixture<TConfiguration, TDocument> : IAsyncLifetime
where TConfiguration : IMongoConfiguration<TDocument>, new()
{
private readonly IMessageSink _sink;
private const string MongoDbImage = "mongo:4.4.5";

private readonly TConfiguration _configuration;

private readonly MongoDbContainer _container = new MongoDbBuilder().WithImage(MongoDbImage).Build();

public MongoFixture() => _configuration = new TConfiguration();
public MongoFixture(IMessageSink sink)
{
_sink = sink;
_configuration = new TConfiguration();
}

public IMongoCollection<TDocument> Collection { get; private set; }

Expand All @@ -33,14 +42,32 @@ public async Task InitializeAsync()
.GetCollection<TDocument>(_configuration.CollectionName);

await _configuration.InitializeAsync(Collection);

var (stdOut, stdErr) = await _container.GetLogsAsync();

_sink.OnMessage(new DiagnosticMessage(stdOut));
_sink.OnMessage(new DiagnosticMessage(stdErr));
}

public async Task DisposeAsync()
{
if (Collection != null)
await _configuration.DisposeAsync(Collection);

await _container.StopAsync();
var cts = new CancellationTokenSource();
cts.CancelAfter(TimeSpan.FromMinutes(2));

try
{
_sink.OnMessage(new DiagnosticMessage($"Stopping {nameof(MongoDbContainer)}"));
await _container.StopAsync(cts.Token);
}
catch (Exception e)
{
_sink.OnMessage(new DiagnosticMessage(e.Message));
}

_sink.OnMessage(new DiagnosticMessage($"Disposing {nameof(MongoDbContainer)}"));
await _container.DisposeAsync();
}
}
Expand Down
Loading
Loading