Last active
October 3, 2022 12:01
-
-
Save cartermp/51824e64e78e0abe4b40d6695e1f573b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Project Sdk="Microsoft.NET.Sdk.Web"> | |
<PropertyGroup> | |
<TargetFramework>net6.0</TargetFramework> | |
<Nullable>enable</Nullable> | |
<ImplicitUsings>enable</ImplicitUsings> | |
<RootNamespace>otel_dotnet</RootNamespace> | |
</PropertyGroup> | |
<ItemGroup> | |
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" /> | |
<PackageReference Include="OpenTelemetry" Version="1.3.0" /> | |
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.3.0" /> | |
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.0.0-rc9.4" /> | |
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.0.0-rc9.4" /> | |
</ItemGroup> | |
</Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
LogRecord.Timestamp: 2022-08-21T23:26:22.1933290Z | |
LogRecord.TraceId: 8b9899797fe5f3f083bdd57e066f9e1a | |
LogRecord.SpanId: 7a9a6814bfdaed76 | |
LogRecord.TraceFlags: Recorded | |
LogRecord.CategoryName: Program | |
LogRecord.LogLevel: Information | |
LogRecord.State: Hello from Person { Name = Phillip, Age = 31 }. | |
Resource associated with LogRecord: | |
service.name: MyCompany.MyProduct.MyService | |
service.version: 1.0.0 | |
service.instance.id: 6360402a-71c7-4074-949c-d220ef853740 | |
Activity.TraceId: 8b9899797fe5f3f083bdd57e066f9e1a | |
Activity.SpanId: 7a9a6814bfdaed76 | |
Activity.TraceFlags: Recorded | |
Activity.ParentSpanId: 8bf8605a9de554f8 | |
Activity.ActivitySourceName: MyCompany.MyProduct.MyService | |
Activity.DisplayName: app.manual-span | |
Activity.Kind: Internal | |
Activity.StartTime: 2022-08-21T23:26:22.1929930Z | |
Activity.Duration: 00:00:00.0149910 | |
Activity.Tags: | |
app.manual-span.message: Adding custom spans is also super easy! | |
Resource associated with Activity: | |
service.name: MyCompany.MyProduct.MyService | |
service.version: 1.0.0 | |
service.instance.id: 6360402a-71c7-4074-949c-d220ef853740 | |
Activity.TraceId: 8b9899797fe5f3f083bdd57e066f9e1a | |
Activity.SpanId: 8bf8605a9de554f8 | |
Activity.TraceFlags: Recorded | |
Activity.ActivitySourceName: OpenTelemetry.Instrumentation.AspNetCore | |
Activity.DisplayName: / | |
Activity.Kind: Server | |
Activity.StartTime: 2022-08-21T23:26:22.1341990Z | |
Activity.Duration: 00:00:00.0927820 | |
Activity.Tags: | |
http.host: localhost:7026 | |
http.method: GET | |
http.target: / | |
http.url: https://localhost:7026/ | |
http.user_agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36 | |
http.status_code: 200 | |
StatusCode : UNSET | |
Resource associated with Activity: | |
service.name: MyCompany.MyProduct.MyService | |
service.version: 1.0.0 | |
service.instance.id: 6360402a-71c7-4074-949c-d220ef853740 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using OpenTelemetry; | |
using OpenTelemetry.Logs; | |
using OpenTelemetry.Resources; | |
using OpenTelemetry.Trace; | |
var builder = WebApplication.CreateBuilder(args); | |
var app = builder.Build(); | |
var appResourceBuilder = ResourceBuilder.CreateDefault() | |
.AddService(serviceName: Telemetry.ServiceName, serviceVersion: Telemetry.ServiceVersion); | |
using var tracerProvider = Sdk.CreateTracerProviderBuilder() | |
.AddSource(Telemetry.ServiceName) | |
.SetResourceBuilder(appResourceBuilder) | |
.AddConsoleExporter() | |
.AddAspNetCoreInstrumentation() | |
.Build(); | |
using var loggerFactory = LoggerFactory.Create(builder => | |
{ | |
builder.AddOpenTelemetry(options => | |
{ | |
options.SetResourceBuilder(appResourceBuilder) | |
.AddConsoleExporter(); | |
}); | |
}); | |
var logger = loggerFactory.CreateLogger<Program>(); | |
app.MapGet("/", () => | |
{ | |
using var activity = Telemetry.MyActivitySource.StartActivity("app.manual-span"); | |
activity?.AddTag("app.manual-span.message", "Adding custom spans is also super easy!"); | |
logger.LogInformation("Hello from {person}.", new Person("Phillip", 31)); | |
}); | |
app.Run(); | |
record Person(string Name, int Age); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Diagnostics; | |
public static class Telemetry | |
{ | |
public static readonly string ServiceName = "MyCompany.MyProduct.MyService"; | |
public static readonly string ServiceVersion = "1.0.0"; | |
public static readonly ActivitySource MyActivitySource = new(ServiceName); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment