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

Test configuration via IConfiguration #4

Merged
merged 3 commits into from
Jan 24, 2022
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
49 changes: 49 additions & 0 deletions .github/workflows/package-Instrumentation.EventCounters.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: Pack OpenTelemetry.Contrib.Instrumentation.EventCounters

on:
workflow_dispatch:
inputs:
logLevel:
description: 'Log level'
required: true
default: 'warning'
push:
tags:
- 'Instrumentation.EventCounters-*' # trigger when we create a tag with prefix "Instrumentation.AWS-"

jobs:
build-test-pack:
runs-on: ${{ matrix.os }}
env:
PROJECT: OpenTelemetry.Contrib.Instrumentation.EventCounters

strategy:
matrix:
os: [windows-latest]

steps:
- uses: actions/checkout@v2
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@v2
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 }}
4 changes: 2 additions & 2 deletions .github/workflows/pr_build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
fail-fast: false # ensures the entire test matrix is run, even if one permutation fails
matrix:
os: [windows-latest,ubuntu-latest]
version: [net461,netcoreapp2.1,netcoreapp3.1,net5.0]
version: [net461,netcoreapp2.1,netcoreapp3.1,net5.0,net6.0]
exclude:
- os: ubuntu-latest
version: net461
Expand All @@ -29,4 +29,4 @@ jobs:
run: dotnet build --configuration Release --no-restore

- name: Test
run: dotnet test **/bin/**/${{ matrix.version }}/*.Tests.dll
run: dotnet test **/bin/**/${{ matrix.version }}/*.Tests.dll
67 changes: 67 additions & 0 deletions src/OpenTelemetry.Contrib.Instrumentation.EventCounters/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,73 @@ namespace DotnetMetrics
}
```

Configuration can be done completely using the IConfiguration.

```json
{
"Telemetry": {
"RefreshIntervalSecs": 55,
"Sources": [
{
"EventSourceName": "System.Runtime",
"EventCounters" : [
{
"Name": "cpu-usage",
"Description": "Current CPU usage",
"Type": "DoubleGauge"
},
{
"Name": "working-set",
"Description": "Process working set",
"Type": "LongSum",
"MetricName": "process_working_set"
}
]
},
{
"EventSourceName": "MyCustomSource",
"EventCounters" : [
{
"Name": "orders_submitted",
"Description": "Number of submitted orders",
"Type": "LongSum"
}
]
}
]
}
}
```

```csharp
var builder = WebApplication.CreateBuilder(args);


builder.Services.AddControllers();

builder.Services.AddOpenTelemetryMetrics(
(meterBuilder) => meterBuilder
.AddPrometheusExporter()
.AddEventCounters(options =>
{
builder.Configuration.GetSection("Telemetry").Bind(options);
}));

var app = builder.Build();

// Configure the HTTP request pipeline.

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.UseOpenTelemetryPrometheusScrapingEndpoint();

app.Run();
```

## References

* [OpenTelemetry Project](https://opentelemetry.io/)
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// <copyright file="EventCountersOptionsTests.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.IO;
using System.Text;
using Microsoft.Extensions.Configuration;
using Xunit;

namespace OpenTelemetry.Contrib.Instrumentation.EventCounters.Tests
{
public class EventCountersOptionsTests
{
[Fact]
public void Can_Be_Read_From_Configuration()
{
var json = @"{
""Telemetry"": {
""RefreshIntervalSecs"": 55,
""Sources"": [
{
""EventSourceName"": ""System.Runtime"",
""EventCounters"" : [
{
""Name"": ""cpu-usage"",
""Description"": ""Current CPU usage"",
""Type"": ""DoubleGauge""
},
{
""Name"": ""working-set"",
""Description"": ""Process working set"",
""Type"": ""LongSum"",
""MetricName"": ""process_working_set""
}
]
},
{
""EventSourceName"": ""MyCustomSource"",
""EventCounters"" : [
{
""Name"": ""orders_submitted"",
""Description"": ""Number of submitted orders"",
""Type"": ""LongSum""
}
]
}
]
}
}";

var configuration = new ConfigurationBuilder().AddJsonStream(new MemoryStream(Encoding.UTF8.GetBytes(json))).Build();

var options = configuration.GetSection("Telemetry").Get<EventCountersOptions>();
Assert.Equal(55, options.RefreshIntervalSecs);
Assert.Equal(2, options.Sources.Count);

Assert.Equal(2, options.Sources[0].EventCounters.Count);
Assert.Equal("System.Runtime", options.Sources[0].EventSourceName);

Assert.Equal("cpu-usage", options.Sources[0].EventCounters[0].Name);
Assert.Equal("Current CPU usage", options.Sources[0].EventCounters[0].Description);
Assert.Equal(OpenTelemetry.Metrics.MetricType.DoubleGauge, options.Sources[0].EventCounters[0].Type);

Assert.Equal("working-set", options.Sources[0].EventCounters[1].Name);
Assert.Equal("Process working set", options.Sources[0].EventCounters[1].Description);
Assert.Equal(OpenTelemetry.Metrics.MetricType.LongSum, options.Sources[1].EventCounters[0].Type);
Assert.Equal("process_working_set", options.Sources[0].EventCounters[1].MetricName);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />
<PackageReference Include="Moq" Version="4.16.1" />
<PackageReference Include="coverlet.collector" Version="1.3.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(MicrosoftNETTestSdkPkgVer)" />
Expand Down