Skip to content

Commit

Permalink
open-telemetry#172 Resource detectors for Docker (open-telemetry#206)
Browse files Browse the repository at this point in the history
* open-telemetry#172 Resource detectors for Docker
  • Loading branch information
swetharavichandrancisco authored Jun 8, 2022
1 parent 1c1b816 commit b530a2f
Show file tree
Hide file tree
Showing 15 changed files with 647 additions and 0 deletions.
41 changes: 41 additions & 0 deletions .github/ISSUE_TEMPLATE/comp_extensions_docker.md
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.
4 changes: 4 additions & 0 deletions .github/component_owners.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

# Each component identified by its path prefix has a list of users
components:
src/OpenTelemetry.Extensions.Docker/:
- swetharavichandrancisco
src/OpenTelemetry.Contrib.Extensions.AWSXRay/:
- srprash
- lupengamzn
Expand Down Expand Up @@ -66,6 +68,8 @@ components:
- reyang
- utpilla
- Yun-Ting
test/OpenTelemetry.Extensions.Docker.Tests/:
- swetharavichandrancisco
test/OpenTelemetry.Exporter.Instana.Tests/:
- zivaninstana
test/OpenTelemetry.Exporter.Stackdriver.Tests/:
Expand Down
49 changes: 49 additions & 0 deletions .github/workflows/package-Extensions.Docker.yml
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 }}
14 changes: 14 additions & 0 deletions opentelemetry-dotnet-contrib.sln
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenTelemetry.Extensions",
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenTelemetry.Extensions.Tests", "test\OpenTelemetry.Extensions.Tests\OpenTelemetry.Extensions.Tests.csproj", "{2117F4E3-6612-4E4D-A757-27271EEB7783}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenTelemetry.Extensions.Docker", "src\OpenTelemetry.Extensions.Docker\OpenTelemetry.Extensions.Docker.csproj", "{498A6808-C0DF-441F-A764-51A3BC4B8FC5}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenTelemetry.Extensions.Docker.Tests", "test\OpenTelemetry.Extensions.Docker.Tests\OpenTelemetry.Extensions.Docker.Tests.csproj", "{FB41E19E-2682-4D07-BA59-FD5205AFA71E}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenTelemetry.Exporter.Geneva", "src\OpenTelemetry.Exporter.Geneva\OpenTelemetry.Exporter.Geneva.csproj", "{1105C814-31DA-4214-BEA8-6DB5FC12C808}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenTelemetry.Exporter.Geneva.Benchmark", "test\OpenTelemetry.Exporter.Geneva.Benchmark\OpenTelemetry.Exporter.Geneva.Benchmark.csproj", "{F53FD7F5-DBC0-4FA5-83BA-B4C07A5BD248}"
Expand Down Expand Up @@ -369,6 +373,14 @@ Global
{2117F4E3-6612-4E4D-A757-27271EEB7783}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2117F4E3-6612-4E4D-A757-27271EEB7783}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2117F4E3-6612-4E4D-A757-27271EEB7783}.Release|Any CPU.Build.0 = Release|Any CPU
{498A6808-C0DF-441F-A764-51A3BC4B8FC5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{498A6808-C0DF-441F-A764-51A3BC4B8FC5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{498A6808-C0DF-441F-A764-51A3BC4B8FC5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{498A6808-C0DF-441F-A764-51A3BC4B8FC5}.Release|Any CPU.Build.0 = Release|Any CPU
{FB41E19E-2682-4D07-BA59-FD5205AFA71E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FB41E19E-2682-4D07-BA59-FD5205AFA71E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FB41E19E-2682-4D07-BA59-FD5205AFA71E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FB41E19E-2682-4D07-BA59-FD5205AFA71E}.Release|Any CPU.Build.0 = Release|Any CPU
{1105C814-31DA-4214-BEA8-6DB5FC12C808}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1105C814-31DA-4214-BEA8-6DB5FC12C808}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1105C814-31DA-4214-BEA8-6DB5FC12C808}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down Expand Up @@ -468,6 +480,8 @@ Global
{6AE92AAD-CF08-4E60-98EF-A7F762DAAB4D} = {2097345F-4DD3-477D-BC54-A922F9B2B402}
{42B3FB71-BB42-46E3-9CEC-56620CB76BD9} = {22DF5DC0-1290-4E83-A9D8-6BB7DE3B3E63}
{2117F4E3-6612-4E4D-A757-27271EEB7783} = {2097345F-4DD3-477D-BC54-A922F9B2B402}
{498A6808-C0DF-441F-A764-51A3BC4B8FC5} = {22DF5DC0-1290-4E83-A9D8-6BB7DE3B3E63}
{FB41E19E-2682-4D07-BA59-FD5205AFA71E} = {2097345F-4DD3-477D-BC54-A922F9B2B402}
{1105C814-31DA-4214-BEA8-6DB5FC12C808} = {22DF5DC0-1290-4E83-A9D8-6BB7DE3B3E63}
{F53FD7F5-DBC0-4FA5-83BA-B4C07A5BD248} = {2097345F-4DD3-477D-BC54-A922F9B2B402}
{F632DFB6-38AD-4356-8997-8CCC0492619C} = {2097345F-4DD3-477D-BC54-A922F9B2B402}
Expand Down
23 changes: 23 additions & 0 deletions src/OpenTelemetry.Extensions.Docker/AssemblyInfo.cs
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
12 changes: 12 additions & 0 deletions src/OpenTelemetry.Extensions.Docker/CHANGELOG.md
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 src/OpenTelemetry.Extensions.Docker/DockerExtensionsEventSource.cs
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;
}
}
}
}
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>
38 changes: 38 additions & 0 deletions src/OpenTelemetry.Extensions.Docker/README.md
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/)
Loading

0 comments on commit b530a2f

Please sign in to comment.