-
Notifications
You must be signed in to change notification settings - Fork 206
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for
Elastic.Clients.Elasticsearch
(#1935)
* Test `Elastic.Clients.Elasticsearch` via OTel * Suppress HTTP span creation for elasticsearch via OTel * Cleanup * Update README.md * Update HttpDiagnosticListenerImplBase.cs * Update IHttpSpanTracer.cs * Update ElasticSearchHttpNonTracer.cs * Update ElasticSearchHttpNonTracer.cs * Update ElasticSearchTests.cs * Apply suggestions from code review Co-authored-by: Wolfgang Ziegler <[email protected]> * Fix typo: ElasticSearch -> Elasticsearch * Change method name on IHttpSpanTracer `SuppressSpanCreation` -> `ShouldSuppressSpanCreation` * Renaming leftovers Rider "renaming" feature left me down. * Update ElasticsearchTestFixture.cs * Update ElasticsearchTestFixture.cs Co-authored-by: Wolfgang Ziegler <[email protected]>
- Loading branch information
1 parent
c77bb77
commit a81ac60
Showing
14 changed files
with
416 additions
and
21 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
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
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
34 changes: 34 additions & 0 deletions
34
src/Elastic.Apm/OpenTelemetry/ElasticSearchHttpNonTracer.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,34 @@ | ||
// Licensed to Elasticsearch B.V under | ||
// one or more agreements. | ||
// 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 | ||
#if NET5_0_OR_GREATER | ||
|
||
using System; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
using Elastic.Apm.Api; | ||
using Elastic.Apm.DiagnosticListeners; | ||
|
||
namespace Elastic.Apm.OpenTelemetry | ||
{ | ||
/// <summary> | ||
/// Handles HTTP spans for outgoing HTTP calls from `Elastic.Clients.ElasticSearch`. | ||
/// Since `Elastic.Clients.ElasticSearch` emits <see cref="Activity"/>, and according to our spec, for calls into elasticsearch | ||
/// we don't need to create an extra HTTP span, all this does is that it suppresses span creation during the outgoing HTTP call. | ||
/// </summary> | ||
public class ElasticSearchHttpNonTracer : IHttpSpanTracer | ||
{ | ||
public bool IsMatch(string method, Uri requestUrl, Func<string, string> headerGetter) => false; | ||
public ISpan StartSpan(IApmAgent agent, string method, Uri requestUrl, Func<string, string> headerGetter) => null; | ||
|
||
public bool ShouldSuppressSpanCreation() | ||
{ | ||
if (Activity.Current == null || Activity.Current.Parent == null) | ||
return false; | ||
return Activity.Current.Parent.DisplayName.StartsWith("Elasticsearch:") && Activity.Current.Parent.Tags.Any(n => n is { Key: "db.system", Value: "elasticsearch" }); | ||
} | ||
} | ||
} | ||
|
||
#endif |
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
23 changes: 23 additions & 0 deletions
23
test/Elastic.Clients.Elasticsearch.Tests/Elastic.Clients.Elasticsearch.Tests.csproj
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,23 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="FluentAssertions" Version="5.6.0" /> | ||
<PackageReference Include="xunit" Version="2.4.1" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3" /> | ||
<PackageReference Include="Elastic.Clients.Elasticsearch" Version="8.0.1" /> | ||
<PackageReference Include="Testcontainers" Version="2.2.0" /> | ||
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Elastic.Apm\Elastic.Apm.csproj" /> | ||
<ProjectReference Include="..\Elastic.Apm.Tests.Utilities\Elastic.Apm.Tests.Utilities.csproj" /> | ||
</ItemGroup> | ||
</Project> |
57 changes: 57 additions & 0 deletions
57
test/Elastic.Clients.Elasticsearch.Tests/ElasticsearchTestFixture.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,57 @@ | ||
// Licensed to Elasticsearch B.V under | ||
// one or more agreements. | ||
// 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 DotNet.Testcontainers.Builders; | ||
using DotNet.Testcontainers.Configurations; | ||
using DotNet.Testcontainers.Containers; | ||
using Elastic.Transport; | ||
using Xunit; | ||
|
||
namespace Elastic.Clients.Elasticsearch.Tests; | ||
|
||
public class ElasticsearchTestFixture : IAsyncDisposable, IAsyncLifetime | ||
{ | ||
public ElasticsearchTestcontainer Container { get; } | ||
public ElasticsearchClient? Client { get; private set; } | ||
|
||
private readonly TestcontainerDatabaseConfiguration _configuration = new ElasticsearchTestcontainerConfiguration { Password = "secret" }; | ||
|
||
public ElasticsearchTestFixture() => | ||
Container = new TestcontainersBuilder<ElasticsearchTestcontainer>() | ||
.WithImage("docker.elastic.co/elasticsearch/elasticsearch:8.5") | ||
.WithDatabase(_configuration) | ||
.Build(); | ||
|
||
public async Task InitializeAsync() | ||
{ | ||
await Container.StartAsync(); | ||
|
||
var settings = new ElasticsearchClientSettings(new Uri(Container.ConnectionString)) | ||
.ServerCertificateValidationCallback(CertificateValidations.AllowAll) | ||
.Authentication(new BasicAuthentication(Container.Username, Container.Password)); | ||
|
||
Client = new ElasticsearchClient(settings); | ||
if (Client == null) | ||
throw new Exception("`new ElasticsearchClient(settings)` returned `null`"); | ||
} | ||
|
||
async Task IAsyncLifetime.DisposeAsync() | ||
{ | ||
if (Container.State == TestcontainersStates.Running) | ||
{ | ||
await Container.StopAsync(); | ||
await Container.DisposeAsync(); | ||
} | ||
} | ||
|
||
async ValueTask IAsyncDisposable.DisposeAsync() | ||
{ | ||
if (Container.State == TestcontainersStates.Running) | ||
{ | ||
await Container.StopAsync(); | ||
await Container.DisposeAsync(); | ||
} | ||
} | ||
} |
Oops, something went wrong.