Console example of OpenTelemetry tracing (#944)

* Added DI container initialization

* Added httpclient service creation

* Improved minimal example of OpenTelemetry console

* Update Program.cs

* Removed TargetFramework from .csproj
This commit is contained in:
Manuel Menegazzo
2022-07-11 18:07:48 +02:00
committed by GitHub
parent 64ae18cfab
commit 033364746a
4 changed files with 72 additions and 6 deletions

View File

@@ -0,0 +1,36 @@
using k8s;
using OpenTelemetry;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
var serviceName = "MyCompany.MyProduct.MyService";
var serviceVersion = "1.0.0";
// Create the OpenTelemetry TraceProvide with HttpClient instrumentation enabled
// NOTE: for this example telemetry will be exported to console
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddSource(serviceName)
.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService(serviceName: serviceName, serviceVersion: serviceVersion))
.AddHttpClientInstrumentation()
.AddConsoleExporter()
.Build();
// Load kubernetes configuration
var config = KubernetesClientConfiguration.BuildDefaultConfig();
// Create an istance of Kubernetes client
IKubernetes client = new Kubernetes(config);
// Read the list of pods contained in default namespace
var list = client.CoreV1.ListNamespacedPod("default");
// Print the name of pods
foreach (var item in list.Items)
{
Console.WriteLine(item.Metadata.Name);
}
// Or empty if there are no pods
if (list.Items.Count == 0)
{
Console.WriteLine("Empty!");
}

View File

@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="OpenTelemetry" Version="1.3.0" />
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.3.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.0.0-rc9.4" />
</ItemGroup>
</Project>