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>

View File

@@ -63,7 +63,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "restart", "examples\restart
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "KubernetesClient.Kubectl", "src\KubernetesClient.Kubectl\KubernetesClient.Kubectl.csproj", "{21201F30-5463-4FC6-93C3-FBF157F0D46C}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "KubernetesClient.Kubectl", "src\KubernetesClient.Kubectl\KubernetesClient.Kubectl.csproj", "{21201F30-5463-4FC6-93C3-FBF157F0D46C}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Kubectl.Tests", "tests\Kubectl.Tests\Kubectl.Tests.csproj", "{9128F6DC-6B7A-417F-937A-90461D6989A8}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Kubectl.Tests", "tests\Kubectl.Tests\Kubectl.Tests.csproj", "{9128F6DC-6B7A-417F-937A-90461D6989A8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "openTelemetryConsole", "examples\openTelemetryConsole\openTelemetryConsole.csproj", "{8E266190-AE6E-44A8-948D-BD974AA82428}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -411,6 +413,18 @@ Global
{9128F6DC-6B7A-417F-937A-90461D6989A8}.Release|x64.Build.0 = Release|Any CPU {9128F6DC-6B7A-417F-937A-90461D6989A8}.Release|x64.Build.0 = Release|Any CPU
{9128F6DC-6B7A-417F-937A-90461D6989A8}.Release|x86.ActiveCfg = Release|Any CPU {9128F6DC-6B7A-417F-937A-90461D6989A8}.Release|x86.ActiveCfg = Release|Any CPU
{9128F6DC-6B7A-417F-937A-90461D6989A8}.Release|x86.Build.0 = Release|Any CPU {9128F6DC-6B7A-417F-937A-90461D6989A8}.Release|x86.Build.0 = Release|Any CPU
{8E266190-AE6E-44A8-948D-BD974AA82428}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8E266190-AE6E-44A8-948D-BD974AA82428}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8E266190-AE6E-44A8-948D-BD974AA82428}.Debug|x64.ActiveCfg = Debug|Any CPU
{8E266190-AE6E-44A8-948D-BD974AA82428}.Debug|x64.Build.0 = Debug|Any CPU
{8E266190-AE6E-44A8-948D-BD974AA82428}.Debug|x86.ActiveCfg = Debug|Any CPU
{8E266190-AE6E-44A8-948D-BD974AA82428}.Debug|x86.Build.0 = Debug|Any CPU
{8E266190-AE6E-44A8-948D-BD974AA82428}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8E266190-AE6E-44A8-948D-BD974AA82428}.Release|Any CPU.Build.0 = Release|Any CPU
{8E266190-AE6E-44A8-948D-BD974AA82428}.Release|x64.ActiveCfg = Release|Any CPU
{8E266190-AE6E-44A8-948D-BD974AA82428}.Release|x64.Build.0 = Release|Any CPU
{8E266190-AE6E-44A8-948D-BD974AA82428}.Release|x86.ActiveCfg = Release|Any CPU
{8E266190-AE6E-44A8-948D-BD974AA82428}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
@@ -444,6 +458,7 @@ Global
{973CCB4A-F344-4C4F-81A5-0F40F7F43C07} = {B70AFB57-57C9-46DC-84BE-11B7DDD34B40} {973CCB4A-F344-4C4F-81A5-0F40F7F43C07} = {B70AFB57-57C9-46DC-84BE-11B7DDD34B40}
{21201F30-5463-4FC6-93C3-FBF157F0D46C} = {3D1864AA-1FFC-4512-BB13-46055E410F73} {21201F30-5463-4FC6-93C3-FBF157F0D46C} = {3D1864AA-1FFC-4512-BB13-46055E410F73}
{9128F6DC-6B7A-417F-937A-90461D6989A8} = {8AF4A5C2-F0CE-47D5-A4C5-FE4AB83CA509} {9128F6DC-6B7A-417F-937A-90461D6989A8} = {8AF4A5C2-F0CE-47D5-A4C5-FE4AB83CA509}
{8E266190-AE6E-44A8-948D-BD974AA82428} = {B70AFB57-57C9-46DC-84BE-11B7DDD34B40}
EndGlobalSection EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {049A763A-C891-4E8D-80CF-89DD3E22ADC7} SolutionGuid = {049A763A-C891-4E8D-80CF-89DD3E22ADC7}

View File

@@ -1,12 +1,12 @@
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;
using k8s.Authentication; using k8s.Authentication;
using k8s.Exceptions; using k8s.Exceptions;
using k8s.KubeConfigModels; using k8s.KubeConfigModels;
using System.Diagnostics;
using System.IO;
using System.Net; using System.Net;
using System.Runtime.InteropServices;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;
namespace k8s namespace k8s
{ {