forked from open-telemetry/opentelemetry-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
open-telemetry#172 Resource detectors for Docker (open-telemetry#206)
* open-telemetry#172 Resource detectors for Docker
- Loading branch information
1 parent
1c1b816
commit b530a2f
Showing
15 changed files
with
647 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
--- | ||
name: OpenTelemetry.Extensions.Docker | ||
about: Issue with OpenTelemetry.Extensions.Docker | ||
labels: comp:extensions.docker | ||
--- | ||
|
||
# Issue with OpenTelemetry.Extensions.Docker | ||
|
||
List of [all OpenTelemetry NuGet | ||
packages](https://www.nuget.org/profiles/OpenTelemetry) and version that you are | ||
using (e.g. `OpenTelemetry 1.0.2`): | ||
|
||
* TBD | ||
|
||
Runtime version (e.g. `net462`, `net48`, `netcoreapp3.1`, `net6.0` etc. You can | ||
find this information from the `*.csproj` file): | ||
|
||
* TBD | ||
|
||
**Is this a feature request or a bug?** | ||
|
||
* [ ] Feature Request | ||
* [ ] Bug | ||
|
||
**What is the expected behavior?** | ||
|
||
What do you expect to see? | ||
|
||
**What is the actual behavior?** | ||
|
||
What did you see instead? If you are reporting a bug, create a self-contained | ||
project using the template of your choice and apply the minimum required code to | ||
result in the issue you're observing. We will close this issue if: | ||
|
||
* The repro project you share with us is complex. We can't investigate custom | ||
projects, so don't point us to such, please. | ||
* If we can not reproduce the behavior you're reporting. | ||
|
||
## Additional Context | ||
|
||
Add any other context about the feature request here. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
name: Pack OpenTelemetry.Extensions.Docker | ||
|
||
on: | ||
workflow_dispatch: | ||
inputs: | ||
logLevel: | ||
description: 'Log level' | ||
required: true | ||
default: 'warning' | ||
push: | ||
tags: | ||
- 'Extensions.Docker-*' # trigger when we create a tag with prefix "Extensions.Docker-" | ||
|
||
jobs: | ||
build-test-pack: | ||
runs-on: ${{ matrix.os }} | ||
env: | ||
PROJECT: OpenTelemetry.Extensions.Docker | ||
|
||
strategy: | ||
matrix: | ||
os: [windows-latest] | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
with: | ||
fetch-depth: 0 # fetching all | ||
|
||
- name: Install dependencies | ||
run: dotnet restore | ||
|
||
- name: dotnet build ${{env.PROJECT}} | ||
run: dotnet build src/${{env.PROJECT}} --configuration Release --no-restore -p:Deterministic=true | ||
|
||
- name: dotnet test ${{env.PROJECT}} | ||
run: dotnet test test/${{env.PROJECT}}.Tests | ||
|
||
- name: dotnet pack ${{env.PROJECT}} | ||
run: dotnet pack src/${{env.PROJECT}} --configuration Release --no-build | ||
|
||
- name: Publish Artifacts | ||
uses: actions/upload-artifact@v3 | ||
with: | ||
name: ${{env.PROJECT}}-packages | ||
path: '**/${{env.PROJECT}}/bin/**/*.*nupkg' | ||
|
||
- name: Publish Nuget | ||
run: | | ||
nuget push **/${{env.PROJECT}}/bin/**/*.nupkg -Source https://api.nuget.org/v3/index.json -ApiKey ${{ secrets.NUGET_TOKEN }} -SymbolApiKey ${{ secrets.NUGET_TOKEN }} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// <copyright file="AssemblyInfo.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.Runtime.CompilerServices; | ||
|
||
#if SIGNED | ||
[assembly: InternalsVisibleTo("OpenTelemetry.Extensions.Docker.Tests, PublicKey=002400000480000094000000060200000024000052534131000400000100010051c1562a090fb0c9f391012a32198b5e5d9a60e9b80fa2d7b434c9e5ccb7259bd606e66f9660676afc6692b8cdc6793d190904551d2103b7b22fa636dcbb8208839785ba402ea08fc00c8f1500ccef28bbf599aa64ffb1e1d5dc1bf3420a3777badfe697856e9d52070a50c3ea5821c80bef17ca3acffa28f89dd413f096f898")] | ||
#else | ||
[assembly: InternalsVisibleTo("OpenTelemetry.Extensions.Docker.Tests")] | ||
#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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Changelog - OpenTelemetry.Extensions.Docker | ||
|
||
This is the first release for the `OpenTelemetry.Extensions.Docker` project. | ||
The release targets | ||
[OpenTelemetry.Extensions.Docker](https://www.nuget.org/packages/OpenTelemetry.Extensions.Docker/). | ||
The project targets 1.2.0 of the [OpenTelemetry | ||
SDK](https://www.nuget.org/packages/OpenTelemetry/). | ||
|
||
The Docker extensions include plugin to extract resource detectors | ||
from docker environment (container id). For more details, | ||
please refer to the | ||
[README](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/blob/main/src/OpenTelemetry.Extensions.Docker/README.md) |
63 changes: 63 additions & 0 deletions
63
src/OpenTelemetry.Extensions.Docker/DockerExtensionsEventSource.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,63 @@ | ||
// <copyright file="DockerExtensionsEventSource.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; | ||
using System.Diagnostics.Tracing; | ||
using System.Globalization; | ||
using System.Threading; | ||
|
||
namespace OpenTelemetry.Extensions.Docker | ||
{ | ||
[EventSource(Name = "OpenTelemetry-Extensions-Docker")] | ||
internal class DockerExtensionsEventSource : EventSource | ||
{ | ||
public static DockerExtensionsEventSource Log = new DockerExtensionsEventSource(); | ||
|
||
[NonEvent] | ||
public void ExtractResourceAttributesException(string format, Exception ex) | ||
{ | ||
if (this.IsEnabled(EventLevel.Error, (EventKeywords)(-1))) | ||
{ | ||
this.FailedToExtractResourceAttributes(format, ToInvariantString(ex)); | ||
} | ||
} | ||
|
||
[Event(1, Message = "Failed to extract resource attributes in '{0}'.", Level = EventLevel.Error)] | ||
public void FailedToExtractResourceAttributes(string format, string exception) | ||
{ | ||
this.WriteEvent(1, format, exception); | ||
} | ||
|
||
/// <summary> | ||
/// Returns a culture-independent string representation of the given <paramref name="exception"/> object, | ||
/// appropriate for diagnostics tracing. | ||
/// </summary> | ||
private static string ToInvariantString(Exception exception) | ||
{ | ||
var originalUICulture = Thread.CurrentThread.CurrentUICulture; | ||
|
||
try | ||
{ | ||
Thread.CurrentThread.CurrentUICulture = CultureInfo.InvariantCulture; | ||
return exception.ToString(); | ||
} | ||
finally | ||
{ | ||
Thread.CurrentThread.CurrentUICulture = originalUICulture; | ||
} | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/OpenTelemetry.Extensions.Docker/OpenTelemetry.Extensions.Docker.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,10 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<TargetFrameworks>;net461;netstandard2.0</TargetFrameworks> | ||
<Description>OpenTelemetry Extensions - Container Resource Detector from Docker environment.</Description> | ||
<MinVerTagPrefix>Extensions.Docker-</MinVerTagPrefix> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<PackageReference Include="OpenTelemetry" Version="1.2.0" /> | ||
</ItemGroup> | ||
</Project> |
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,38 @@ | ||
# Docker Resource Detectors | ||
|
||
## Getting Started | ||
|
||
You need to install the | ||
`OpenTelemetry.Extensions.Docker` to be able to use the | ||
Docker Resource Detectors. It detects container.id from | ||
Docker environment. | ||
|
||
```shell | ||
dotnet add package OpenTelemetry.Extensions.Docker | ||
``` | ||
|
||
## Usage | ||
|
||
You can configure Docker resource detector to | ||
the `TracerProvider` with the following example below. | ||
|
||
```csharp | ||
using OpenTelemetry; | ||
using OpenTelemetry.Extensions.Docker.Resources; | ||
|
||
var tracerProvider = Sdk.CreateTracerProviderBuilder() | ||
// other configurations | ||
.SetResourceBuilder(ResourceBuilder | ||
.CreateEmpty() | ||
.AddDetector(new DockerResourceDetector())) | ||
.Build(); | ||
``` | ||
|
||
The resource detectors will record the following metadata based on where | ||
your application is running: | ||
|
||
- **DockerResourceDetector**: container id. | ||
|
||
## References | ||
|
||
- [OpenTelemetry Project](https://opentelemetry.io/) |
Oops, something went wrong.