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

Event source configuration #3

Merged
merged 11 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
2 changes: 0 additions & 2 deletions NuGet.config
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
<clear />
<add key="NuGet" value="https://api.nuget.org/v3/index.json" />
<add key="MyGet" value="https://www.myget.org/F/opentelemetry/api/v3/index.json" />
<add key="Dotnet-public" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-public/nuget/v3/index.json" />
<add key="Dotnet-tools" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-tools/nuget/v3/index.json" />
</packageSources>
<disabledPackageSources />
</configuration>
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// <copyright file="WeatherForecastController.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 Microsoft.AspNetCore.Mvc;

namespace Examples.EventCounter.AspNetCore.Controllers
{
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching",
};

private readonly ILogger<WeatherForecastController> logger;

public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
this.logger = logger;
}

[HttpGet]
public IEnumerable<WeatherForecast> Get()
{
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)],
})
.ToArray();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="OpenTelemetry.Exporter.Prometheus" Version="1.2.0-rc1" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.0.0-rc8" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.0.0-rc8" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\src\OpenTelemetry.Contrib.Instrumentation.EventCounters\OpenTelemetry.Contrib.Instrumentation.EventCounters.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// <copyright file="Program.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 OpenTelemetry.Metrics;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();

builder.Services.AddOpenTelemetryMetrics(
(builder) => builder
.AddPrometheusExporter()
.AddEventCounters(options =>
{
options.AddRuntime()
.WithCounters("cpu-usage", "working-set");

options.AddAspNetCore()
.WithCurrentRequests("http_requests_in_progress")
.WithFailedRequests()
.WithRequestRate()
.WithTotalRequests("http_requests_received_total");

options.AddEventSource("Microsoft-AspNetCore-Server-Kestrel")
.WithCounters("total-connections")
.With("connections-per-second", "The number of connections per update interval to the web server", MetricType.LongSum);

options.AddEventSource("Microsoft.AspNetCore.Http.Connections");
}));

var app = builder.Build();

// Configure the HTTP request pipeline.

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.UseOpenTelemetryPrometheusScrapingEndpoint();

app.Run();
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"profiles": {
"Examples.EventCounter.AspNetCore": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "weatherforecast",
"applicationUrl": "https://localhost:7034;http://localhost:5034",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <copyright file="CounterType.cs" company="OpenTelemetry Authors">
// <copyright file="WeatherForecast.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -14,17 +14,16 @@
// limitations under the License.
// </copyright>

namespace OpenTelemetry.Contrib.EventCounterListener.EventPipe
namespace Examples.EventCounter.AspNetCore
{
/// <summary>
/// Counter Type Enumeration.
/// </summary>
internal enum CounterType
public class WeatherForecast
{
/// <summary>Metric Counter Type.</summary>
Metric,
public DateTime Date { get; set; }

/// <summary>Rate Counter Type.</summary>
Rate,
public int TemperatureC { get; set; }

public int TemperatureF => 32 + (int)(this.TemperatureC / 0.5556);

public string? Summary { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
12 changes: 12 additions & 0 deletions examples/eventcounters/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Event counter Instrumentation for OpenTelemetry .NET - Examples

Project structure:

* Examples.EventCounters.AspNetCore

This is a small example how to use and configure the instrumentation library.

It shows how to add several event counters.

Start the project and open the "/metrics" endpoint to view the published
metrics.
21 changes: 17 additions & 4 deletions opentelemetry-dotnet-contrib.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30204.135
# Visual Studio Version 17
VisualStudioVersion = 17.0.32112.339
MinimumVisualStudioVersion = 15.0.26124.0
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{22DF5DC0-1290-4E83-A9D8-6BB7DE3B3E63}"
EndProject
Expand Down Expand Up @@ -156,9 +156,16 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Examples.Owin", "examples\o
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenTelemetry.Contrib.Instrumentation.Owin.Tests", "test\OpenTelemetry.Contrib.Instrumentation.Owin.Tests\OpenTelemetry.Contrib.Instrumentation.Owin.Tests.csproj", "{D52558C8-B7BF-4F59-A0FA-9AA629E68012}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenTelemetry.Contrib.EventCounterListener", "src\OpenTelemetry.Contrib.EventCounterListener\OpenTelemetry.Contrib.EventCounterListener.csproj", "{8487C723-DCAE-4EF5-A29F-ADB9BCDF9DCD}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenTelemetry.Contrib.Instrumentation.EventCounters", "src\OpenTelemetry.Contrib.Instrumentation.EventCounters\OpenTelemetry.Contrib.Instrumentation.EventCounters.csproj", "{8487C723-DCAE-4EF5-A29F-ADB9BCDF9DCD}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenTelemetry.Contrib.EventCounterListener.Tests", "test\OpenTelemetry.Contrib.EventCounterListener.Tests\OpenTelemetry.Contrib.EventCounterListener.Tests.csproj", "{2872634A-581E-494C-B3ED-01E0F3D304ED}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenTelemetry.Contrib.Instrumentation.EventCounters.Tests", "test\OpenTelemetry.Contrib.Instrumentation.EventCounters.Tests\OpenTelemetry.Contrib.Instrumentation.EventCounters.Tests.csproj", "{2872634A-581E-494C-B3ED-01E0F3D304ED}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "eventcounters", "eventcounters", "{906FBA3E-CE12-4618-A833-184BA8ACC24C}"
ProjectSection(SolutionItems) = preProject
examples\eventcounters\README.md = examples\eventcounters\README.md
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Examples.EventCounters.AspNetCore", "examples\eventcounters\Examples.EventCounters.AspNetCore\Examples.EventCounters.AspNetCore.csproj", "{D593FCA7-724D-4C46-AF94-EE48FA2AC48A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -318,6 +325,10 @@ Global
{2872634A-581E-494C-B3ED-01E0F3D304ED}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2872634A-581E-494C-B3ED-01E0F3D304ED}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2872634A-581E-494C-B3ED-01E0F3D304ED}.Release|Any CPU.Build.0 = Release|Any CPU
{D593FCA7-724D-4C46-AF94-EE48FA2AC48A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D593FCA7-724D-4C46-AF94-EE48FA2AC48A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D593FCA7-724D-4C46-AF94-EE48FA2AC48A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D593FCA7-724D-4C46-AF94-EE48FA2AC48A}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -367,6 +378,8 @@ Global
{D52558C8-B7BF-4F59-A0FA-9AA629E68012} = {2097345F-4DD3-477D-BC54-A922F9B2B402}
{8487C723-DCAE-4EF5-A29F-ADB9BCDF9DCD} = {22DF5DC0-1290-4E83-A9D8-6BB7DE3B3E63}
{2872634A-581E-494C-B3ED-01E0F3D304ED} = {2097345F-4DD3-477D-BC54-A922F9B2B402}
{906FBA3E-CE12-4618-A833-184BA8ACC24C} = {B75EE478-97F7-4E9F-9A5A-DB3D0988EDEA}
{D593FCA7-724D-4C46-AF94-EE48FA2AC48A} = {906FBA3E-CE12-4618-A833-184BA8ACC24C}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {B0816796-CDB3-47D7-8C3C-946434DE3B66}
Expand Down
Loading