Skip to content

Commit

Permalink
Merge branch 'main' into issue/AWS-Lambda-SQS-SNS-support
Browse files Browse the repository at this point in the history
  • Loading branch information
rypdal committed Mar 8, 2023
2 parents d55c66b + 588264e commit e51a882
Show file tree
Hide file tree
Showing 9 changed files with 219 additions and 23 deletions.
4 changes: 4 additions & 0 deletions src/OpenTelemetry.Exporter.OneCollector/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

## 0.1.0-alpha.2

Released 2023-Mar-6

* Update OpenTelemetry to 1.4.0
([#1038](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/1038))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ OpenTelemetry.Instrumentation.ElasticsearchClient.ElasticsearchClientInstrumenta
OpenTelemetry.Instrumentation.ElasticsearchClient.ElasticsearchClientInstrumentationOptions.SuppressDownstreamInstrumentation.get -> bool
OpenTelemetry.Instrumentation.ElasticsearchClient.ElasticsearchClientInstrumentationOptions.SuppressDownstreamInstrumentation.set -> void
OpenTelemetry.Trace.TracerProviderBuilderExtensions
static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddElasticsearchClientInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder builder, System.Action<OpenTelemetry.Instrumentation.ElasticsearchClient.ElasticsearchClientInstrumentationOptions> configure = null) -> OpenTelemetry.Trace.TracerProviderBuilder
static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddElasticsearchClientInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder builder, System.Action<OpenTelemetry.Instrumentation.ElasticsearchClient.ElasticsearchClientInstrumentationOptions> configure) -> OpenTelemetry.Trace.TracerProviderBuilder
static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddElasticsearchClientInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder builder) -> OpenTelemetry.Trace.TracerProviderBuilder
static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddElasticsearchClientInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder builder, string name, System.Action<OpenTelemetry.Instrumentation.ElasticsearchClient.ElasticsearchClientInstrumentationOptions> configure) -> OpenTelemetry.Trace.TracerProviderBuilder
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ OpenTelemetry.Instrumentation.ElasticsearchClient.ElasticsearchClientInstrumenta
OpenTelemetry.Instrumentation.ElasticsearchClient.ElasticsearchClientInstrumentationOptions.SuppressDownstreamInstrumentation.get -> bool
OpenTelemetry.Instrumentation.ElasticsearchClient.ElasticsearchClientInstrumentationOptions.SuppressDownstreamInstrumentation.set -> void
OpenTelemetry.Trace.TracerProviderBuilderExtensions
static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddElasticsearchClientInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder builder, System.Action<OpenTelemetry.Instrumentation.ElasticsearchClient.ElasticsearchClientInstrumentationOptions> configure = null) -> OpenTelemetry.Trace.TracerProviderBuilder
static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddElasticsearchClientInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder builder, System.Action<OpenTelemetry.Instrumentation.ElasticsearchClient.ElasticsearchClientInstrumentationOptions> configure) -> OpenTelemetry.Trace.TracerProviderBuilder
static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddElasticsearchClientInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder builder) -> OpenTelemetry.Trace.TracerProviderBuilder
static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddElasticsearchClientInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder builder, string name, System.Action<OpenTelemetry.Instrumentation.ElasticsearchClient.ElasticsearchClientInstrumentationOptions> configure) -> OpenTelemetry.Trace.TracerProviderBuilder
16 changes: 14 additions & 2 deletions src/OpenTelemetry.Instrumentation.ElasticsearchClient/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,24 @@

## Unreleased

* Updated OTel SDK package version to 1.2.0
## 1.0.0-beta.4

Released 2023-Mar-06

* Updated OTel SDK package version to 1.4.0
([#1019](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/1019))
* Update minimum full framework support to net462
* Requests that get an HTTP status code of 404 are not marked as an error span status
* Add MaxDbStatementLength option with default of 4096
* Remove duplicated HTTP method and URL from db.statement attribute value
* Fix faulty logic of MaxDbStatementLength option [(#425)](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/425)
* Fix faulty logic of MaxDbStatementLength option
([#425](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/425))
* Remove method with default attribute
([#1019](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/1019))
* Added overloads which accept a name to the `TracerProviderBuilder`
`AddElasticsearchClientInstrumentation` extension to allow for more fine-grained
options management
([#1019](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/1019))

## 1.0.0-beta.3

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<IncludeSharedInstrumentationSource>true</IncludeSharedInstrumentationSource>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="OpenTelemetry" Version="1.2.0" />
<PackageReference Include="OpenTelemetry" Version="$(OpenTelemetryCoreLatestVersion)" />
<PackageReference Include="System.Text.Json" Version="6.0.4" />
</ItemGroup>

Expand Down
101 changes: 89 additions & 12 deletions src/OpenTelemetry.Instrumentation.ElasticsearchClient/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,105 @@
[![NuGet](https://img.shields.io/nuget/v/OpenTelemetry.Instrumentation.ElasticsearchClient.svg)](https://www.nuget.org/packages/OpenTelemetry.Instrumentation.ElasticsearchClient)
[![NuGet](https://img.shields.io/nuget/dt/OpenTelemetry.Instrumentation.ElasticsearchClient.svg)](https://www.nuget.org/packages/OpenTelemetry.Instrumentation.ElasticsearchClient)

Automatically instruments events emitted by the [NEST/Elasticsearch.Net](https://www.nuget.org/packages/NEST)
client library.
This is an [Instrumentation
Library](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/glossary.md#instrumentation-library),
which instruments [NEST/Elasticsearch.Net](https://www.nuget.org/packages/NEST)
and collects traces about outgoing requests.

### Installation
**Note: This component is based on the OpenTelemetry semantic conventions for
[metrics](https://github.com/open-telemetry/opentelemetry-specification/tree/main/specification/metrics/semantic_conventions)
and
[traces](https://github.com/open-telemetry/opentelemetry-specification/tree/main/specification/trace/semantic_conventions).
These conventions are
[Experimental](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/document-status.md),
and hence, this package is a
[pre-release](https://github.com/open-telemetry/opentelemetry-dotnet/blob/main/VERSIONING.md#pre-releases).
Until a [stable
version](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/telemetry-stability.md)
is released, there can be [breaking changes](./CHANGELOG.md). You can track the
progress from
[milestones](https://github.com/open-telemetry/opentelemetry-dotnet/milestone/23).**

## Steps to enable OpenTelemetry.Instrumentation.ElasticsearchClient

### Step 1: Install Package

Add a reference to the
[`OpenTelemetry.Instrumentation.ElasticsearchClient`](https://www.nuget.org/packages/OpenTelemetry.Instrumentation.ElasticsearchClient)
package. Also, add any other instrumentations & exporters you will need.

```shell
dotnet add package OpenTelemetry.Instrumentation.ElasticsearchClient
dotnet add package --prerelease OpenTelemetry.Instrumentation.ElasticsearchClient
```

### Step 2: Enable NEST/Elasticsearch.Net Instrumentation at application startup

`NEST/Elasticsearch.Net` instrumentation must be enabled at application startup.

The following example demonstrates adding `NEST/Elasticsearch.Net`
instrumentation to a console application. This example also sets up the
OpenTelemetry Console exporter, which requires adding the package
[`OpenTelemetry.Exporter.Console`](https://github.com/open-telemetry/opentelemetry-dotnet/blob/main/src/OpenTelemetry.Exporter.Console/README.md)
to the application.

```csharp
using OpenTelemetry;
using OpenTelemetry.Trace;

public class Program
{
public static void Main(string[] args)
{
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddElasticsearchClientInstrumentation()
.AddConsoleExporter()
.Build();
}
}
```

### Configuration
For an ASP.NET Core application, adding instrumentation is typically done in the
`ConfigureServices` of your `Startup` class. Refer to documentation for
[OpenTelemetry.Instrumentation.AspNetCore](https://github.com/open-telemetry/opentelemetry-dotnet/blob/main/src/OpenTelemetry.Instrumentation.AspNetCore/README.md).

For an ASP.NET application, adding instrumentation is typically done in the
`Global.asax.cs`. Refer to the documentation for
[OpenTelemetry.Instrumentation.AspNet](../OpenTelemetry.Instrumentation.AspNet/README.md).

ASP.NET Core instrumentation example:
## Advanced configuration

This instrumentation can be configured to change the default behavior by using
`ElasticsearchClientInstrumentationOptions`.

```csharp
// Add OpenTelemetry and Elasticsearch client instrumentation
services.AddOpenTelemetry().WithTracing(x =>
services.AddOpenTelemetry()
.WithTracing(builder => builder
.AddElasticsearchClientInstrumentation(options =>
{
// add request json as db.statement attribute tag
options.SetDbStatementForRequest = true;
})
.AddConsoleExporter());
```

When used with
[`OpenTelemetry.Extensions.Hosting`](https://github.com/open-telemetry/opentelemetry-dotnet/blob/main/src/OpenTelemetry.Extensions.Hosting/README.md),
all configurations to `ElasticsearchClientInstrumentationOptions`
can be done in the `ConfigureServices` method of you applications `Startup`
class as shown below.

```csharp
// Configure
services.Configure<ElasticsearchClientInstrumentationOptions>(options =>
{
x.AddElasticsearchClientInstrumentation();
x.AddOtlpExporter(config => {
// Configure OTLP
});
// add request json as db.statement attribute tag
options.SetDbStatementForRequest = true;
});

services.AddOpenTelemetry()
.WithTracing(builder => builder
.AddElasticsearchClientInstrumentation()
.AddConsoleExporter());
```

## Elastic.Clients.Elasticsearch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
// </copyright>

using System;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using OpenTelemetry.Instrumentation.ElasticsearchClient;
using OpenTelemetry.Instrumentation.ElasticsearchClient.Implementation;
using OpenTelemetry.Internal;
Expand All @@ -30,18 +32,49 @@ public static class TracerProviderBuilderExtensions
/// Enables Elasticsearch client Instrumentation.
/// </summary>
/// <param name="builder"><see cref="TracerProviderBuilder"/> being configured.</param>
/// <returns>The instance of <see cref="TracerProviderBuilder"/> to chain the calls.</returns>
public static TracerProviderBuilder AddElasticsearchClientInstrumentation(
this TracerProviderBuilder builder) =>
AddElasticsearchClientInstrumentation(builder, name: null, configure: null);

/// <summary>
/// Enables Elasticsearch client Instrumentation.
/// </summary>
/// <param name="builder"><see cref="TracerProviderBuilder"/> being configured.</param>
/// <param name="configure">Elasticsearch client configuration options.</param>
/// <returns>The instance of <see cref="TracerProviderBuilder"/> to chain the calls.</returns>
public static TracerProviderBuilder AddElasticsearchClientInstrumentation(
this TracerProviderBuilder builder,
Action<ElasticsearchClientInstrumentationOptions> configure) =>
AddElasticsearchClientInstrumentation(builder, name: null, configure);

/// <summary>
/// Enables Elasticsearch client Instrumentation.
/// </summary>
/// <param name="builder"><see cref="TracerProviderBuilder"/> being configured.</param>
/// <param name="name">Name which is used when retrieving options.</param>
/// <param name="configure">Elasticsearch client configuration options.</param>
/// <returns>The instance of <see cref="TracerProviderBuilder"/> to chain the calls.</returns>
public static TracerProviderBuilder AddElasticsearchClientInstrumentation(
this TracerProviderBuilder builder,
Action<ElasticsearchClientInstrumentationOptions> configure = null)
string name,
Action<ElasticsearchClientInstrumentationOptions> configure)
{
Guard.ThrowIfNull(builder);

var elasticsearchClientOptions = new ElasticsearchClientInstrumentationOptions();
configure?.Invoke(elasticsearchClientOptions);
name ??= Options.DefaultName;

if (configure != null)
{
builder.ConfigureServices(services => services.Configure(name, configure));
}

builder.AddInstrumentation(sp =>
{
var options = sp.GetRequiredService<IOptionsMonitor<ElasticsearchClientInstrumentationOptions>>().Get(name);
return new ElasticsearchClientInstrumentation(options);
});

builder.AddInstrumentation(() => new ElasticsearchClientInstrumentation(elasticsearchClientOptions));
builder.AddSource(ElasticsearchRequestPipelineDiagnosticListener.ActivitySourceName);
builder.AddLegacySource("CallElasticsearch");

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// <copyright file="DependencyInjectionConfigTests.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>

using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using OpenTelemetry.Trace;
using Xunit;

namespace OpenTelemetry.Instrumentation.ElasticsearchClient.Tests;

public class DependencyInjectionConfigTests
{
[Theory]
[InlineData(null)]
[InlineData("CustomName")]
public async Task TestTracingOptionsDiConfig(string name)
{
bool optionsPickedFromDi = false;

var services = new ServiceCollection();

services
.Configure<ElasticsearchClientInstrumentationOptions>(name, _ => optionsPickedFromDi = true)
.AddOpenTelemetry()
.WithTracing(builder =>
builder.AddElasticsearchClientInstrumentation(name, configure: null));

var sp = services.BuildServiceProvider();

try
{
foreach (var hostedService in sp.GetServices<IHostedService>())
{
await hostedService.StartAsync(CancellationToken.None);
}

Assert.True(optionsPickedFromDi);
}
finally
{
foreach (var hostedService in sp.GetServices<IHostedService>().Reverse())
{
await hostedService.StopAsync(CancellationToken.None);
}

await sp.DisposeAsync();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Description>Unit test project for OpenTelemetry Elasticsearch client instrumentation</Description>
<!-- OmniSharp/VS Code requires TargetFrameworks to be in descending order for IntelliSense and analysis. -->
<TargetFrameworks>net7.0;net6.0;netcoreapp3.1</TargetFrameworks>
<TargetFrameworks>net7.0;net6.0</TargetFrameworks>
<IncludeSharedTestSource>true</IncludeSharedTestSource>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(MicrosoftNETTestSdkPkgVer)" />
<PackageReference Include="Moq" Version="$(MoqPkgVer)" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="$(OpenTelemetryCoreLatestVersion)" />
<PackageReference Include="xunit" Version="$(XUnitPkgVer)" />
<PackageReference Include="xunit.runner.visualstudio" Version="$(XUnitRunnerVisualStudioPkgVer)">
<PrivateAssets>all</PrivateAssets>
Expand Down

0 comments on commit e51a882

Please sign in to comment.