Skip to content

Commit 1cdd7d5

Browse files
lupengamznwillarmirosNathanielRN
authored
Update user guide for .NET (#154)
* Update dotnet-sdk.mdx * Update trace-manual-instr.mdx * Update trace-manual-instr.mdx * Update src/docs/getting-started/dotnet-sdk/trace-manual-instr.mdx Co-authored-by: William Armiros <[email protected]> * Update src/docs/getting-started/dotnet-sdk/trace-manual-instr.mdx Co-authored-by: William Armiros <[email protected]> * Update src/docs/getting-started/dotnet-sdk/trace-manual-instr.mdx Co-authored-by: William Armiros <[email protected]> * Update src/docs/getting-started/dotnet-sdk/trace-manual-instr.mdx Co-authored-by: William Armiros <[email protected]> * Update src/docs/getting-started/dotnet-sdk/trace-manual-instr.mdx Co-authored-by: William Armiros <[email protected]> * Update trace-manual-instr.mdx * Update src/docs/getting-started/dotnet-sdk.mdx Co-authored-by: (Eliseo) Nathaniel Ruiz Nowell <[email protected]> * Update src/docs/getting-started/dotnet-sdk/trace-manual-instr.mdx Co-authored-by: (Eliseo) Nathaniel Ruiz Nowell <[email protected]> * Update src/docs/getting-started/dotnet-sdk/trace-manual-instr.mdx Co-authored-by: (Eliseo) Nathaniel Ruiz Nowell <[email protected]> * Update src/docs/getting-started/dotnet-sdk/trace-manual-instr.mdx Co-authored-by: (Eliseo) Nathaniel Ruiz Nowell <[email protected]> * Update src/docs/getting-started/dotnet-sdk/trace-manual-instr.mdx Co-authored-by: (Eliseo) Nathaniel Ruiz Nowell <[email protected]> * Update src/docs/getting-started/dotnet-sdk/trace-manual-instr.mdx Co-authored-by: (Eliseo) Nathaniel Ruiz Nowell <[email protected]> * Update src/docs/getting-started/dotnet-sdk/trace-manual-instr.mdx Co-authored-by: (Eliseo) Nathaniel Ruiz Nowell <[email protected]> * Update src/docs/getting-started/dotnet-sdk/trace-manual-instr.mdx Co-authored-by: (Eliseo) Nathaniel Ruiz Nowell <[email protected]> * Update src/docs/getting-started/dotnet-sdk/trace-manual-instr.mdx Co-authored-by: (Eliseo) Nathaniel Ruiz Nowell <[email protected]> Co-authored-by: William Armiros <[email protected]> Co-authored-by: (Eliseo) Nathaniel Ruiz Nowell <[email protected]>
1 parent ded9eed commit 1cdd7d5

File tree

2 files changed

+86
-92
lines changed

2 files changed

+86
-92
lines changed

src/docs/getting-started/dotnet-sdk.mdx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@ path: '/docs/getting-started/dotnet-sdk'
88

99
import SectionSeparator from "components/MdxSectionSeparator/sectionSeparator.jsx"
1010

11-
OpenTelemetry provides different language SDKs to instrument code for collecting telemetry data in the application.
11+
OpenTelemetry provides different language SDKs to instrument customer applications for collecting telemetry data.
1212

13-
In this tutorial, we will introduce how to use OpenTelemetry .NET SDK for manual instrumentation on traces in the application.
13+
In this tutorial, we will introduce how to use OpenTelemetry .NET SDK for manual instrumentation.
1414

1515
<SectionSeparator />
1616

1717
## Getting Started
1818

19-
* [Manual Instrumentation on Traces with .NET SDK](/docs/getting-started/dotnet-sdk/trace-manual-instr)
19+
* [Manual Instrumentation on Traces with OpenTelemetry .NET SDK](/docs/getting-started/dotnet-sdk/trace-manual-instr)
2020

2121

22-
## Sample Code with .NET SDK
22+
## Sample Code
2323

2424
* [AWS Distro for OpenTelemetry Sample Code with .NET SDK](https://github.com/aws-observability/aws-otel-dotnet/tree/main/integration-test-app)

src/docs/getting-started/dotnet-sdk/trace-manual-instr.mdx

Lines changed: 82 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -9,63 +9,101 @@ path: '/docs/getting-started/dotnet-sdk/trace-manual-instr'
99
import SectionSeparator from "components/MdxSectionSeparator/sectionSeparator.jsx"
1010
import SubSectionSeparator from "components/MdxSubSectionSeparator/subsectionSeparator.jsx"
1111

12-
In this tutorial, we will introduce how to manually instrument your application step-by-step using AWS Distro for OpenTelemetry .NET SDK.
12+
The AWS Distro for OpenTelemetry .NET SDK contains an extension library for using OpenTelemetry with AWS X-Ray and for instrumenting the AWS SDK. In this tutorial, we will introduce how to manually instrument your application step-by-step using AWS Distro for OpenTelemetry .NET SDK.
1313

1414
<SectionSeparator />
1515

16-
## Getting Started
16+
## Requirements
17+
18+
The AWS Distro for OpenTelemetry .NET SDK is compatible for .NET framework (net452 and above) and .NET Core (netstandard2.0 and above).
19+
20+
**Note**: You’ll also need to have the [AWS Distro for OpenTelemetry Collector](https://aws-otel.github.io/docs/getting-started/collector) running to export traces to X-Ray.
21+
22+
<SectionSeparator />
23+
24+
## Installation
1725

1826
In order to instrument your .NET application for tracing, start by downloading the `OpenTelemetry` nuget package to your application.
1927

2028
```shell
2129
dotnet add package OpenTelemetry
2230
```
2331

24-
This is the OpenTelemetry SDK for .NET. The SDK deals with concerns such as sampling, processing pipeline, exporting telemetry to a particular backend etc.
32+
The OpenTelemetry SDK for .NET deals with concerns such as sampling, a processing pipeline, and exporting telemetry to a particular backend.
2533
This generally requires additional packages to be downloaded for specific instrumentation or exporter.
2634

27-
<SectionSeparator />
28-
29-
## Sending Traces to AWS X-Ray
30-
31-
By default, the OpenTelemetry SDK generates traces with W3C random ID which X-Ray backend doesn’t currently support.
35+
The OpenTelemetry SDK generates traces with W3C random ID which X-Ray backend doesn’t currently support.
3236
You need to install the `OpenTelemetry.Contrib.Extensions.AWSXRay` to be able to use the `AWSXRayIdGenerator` which generates X-Ray compatible trace IDs.
3337
If you plan to call another application instrumented with AWS X-Ray SDK, you’ll need to configure the `AWSXRayPropagator` as well.
3438

3539
```shell
3640
dotnet add package OpenTelemetry.Contrib.Extensions.AWSXRay
3741
```
3842

39-
Configure `AWSXRayIdGenerator` and `AWSXRayPropagator` globally in your application as follows. Make sure to call `AddXRayTraceId()` in the very **beginning** when creating `TracerProviderBuilder`
43+
In order to export traces from your application to ADOT Collector, you need to install `OpenTelemetry.Exporter.OpenTelemetryProtocol`.
44+
45+
```shell
46+
dotnet add package OpenTelemetry.Exporter.OpenTelemetryProtocol
47+
```
48+
49+
By default the OpenTelemetry exporter sends data to an OpenTelemetry collector at `localhost:4317`.
50+
51+
<SectionSeparator />
52+
53+
## Setting up the Global Tracer
54+
55+
### Sending Traces to AWS X-Ray
56+
57+
Configure AWS X-Ray ID generator, propagator and OpenTelemetry Protocol (OTLP) exporter globally in your application as follows. Make sure to call `AddXRayTraceId()` in the very **beginning** when creating `TracerProvider`
4058

4159
```csharp
4260
using OpenTelemetry;
4361
using OpenTelemetry.Contrib.Extensions.AWSXRay.Trace;
4462
using OpenTelemetry.Trace;
4563

46-
var tracerProviderBuilder = Sdk.CreateTracerProviderBuilder().AddXRayTraceId() // for generating AWS X-Ray compliant trace IDs
64+
var tracerProvider = Sdk.CreateTracerProviderBuilder()
65+
.AddXRayTraceId() // for generating AWS X-Ray compliant trace IDs
66+
.AddOtlpExporter() // default address localhost:4317
67+
.Build();
4768

4869
Sdk.SetDefaultTextMapPropagator(new AWSXRayPropagator()); // configure AWS X-Ray propagator
4970
```
5071

51-
**Note**: You’ll also need to have the [AWS Distro for OpenTelemetry Collector](https://github.com/aws-observability/aws-otel-collector) running to export traces to X-Ray.
72+
### Using the AWS resource Detectors
73+
74+
The ADOT .NET SDK supports automatically recording metadata in EC2, Elastic Beanstalk, ECS, and EKS environments. You can configure the corresponding resource detector to the `TracerProvider` following the EC2 example below.
75+
76+
```csharp
77+
using OpenTelemetry;
78+
using OpenTelemetry.Contrib.Extensions.AWSXRay.Resources;
79+
using OpenTelemetry.Resources;
80+
using OpenTelemetry.Trace;
81+
82+
var tracerProvider = Sdk.CreateTracerProviderBuilder()
83+
// other configurations
84+
.SetResourceBuilder(ResourceBuilder
85+
.CreateDefault()
86+
.AddDetector(new AWSEC2ResourceDetector()))
87+
.Build();
88+
```
5289

5390
<SectionSeparator />
5491

5592
## Instrumenting .NET Applications
5693

57-
### ASP.NET Core
94+
Once you have configured all necessary X-Ray components to the `TracerProvider`, you can proceed to [OpenTelemetry .NET SDK's developer guide](https://github.com/open-telemetry/opentelemetry-dotnet#getting-started) to instrument your .NET application.
95+
96+
OpenTelemetry provides a wide range of instrumentations for popular .NET libraries: [Asp.Net](https://github.com/open-telemetry/opentelemetry-dotnet/tree/main/src/OpenTelemetry.Instrumentation.AspNet#readme), [Asp.Net Core](https://github.com/open-telemetry/opentelemetry-dotnet/tree/main/src/OpenTelemetry.Instrumentation.AspNetCore#readme), [Http](https://github.com/open-telemetry/opentelemetry-dotnet/tree/main/src/OpenTelemetry.Instrumentation.Http#readme), [Grpc](https://github.com/open-telemetry/opentelemetry-dotnet/tree/main/src/OpenTelemetry.Instrumentation.GrpcNetClient#readme), [Redis](https://github.com/open-telemetry/opentelemetry-dotnet/blob/main/src/OpenTelemetry.Instrumentation.StackExchangeRedis/README.md), [Sql](https://github.com/open-telemetry/opentelemetry-dotnet/tree/main/src/OpenTelemetry.Instrumentation.SqlClient#readme) and [EntityFramework](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/tree/main/src/OpenTelemetry.Contrib.Instrumentation.EntityFrameworkCore#readme). Instrumenting a library means that every time the library is used to make or handle a request, that library's calls are automatically wrapped within a span. That span is automatically populated with attributes describing the values used by the library call.
97+
98+
### AWS SDK Instrumentation
5899

59-
Start by downloading the ASP.NET Core and OTLP exporter instrumentation
60-
packages
100+
For tracing downstream calls to AWS services from your .NET application, you will need to install the `OpenTelemetry.Contrib.Instrumentation.AWS` package.
61101

62102
```shell
63-
dotnet add package OpenTelemetry.Instrumentation.AspNetCore
64-
dotnet add package OpenTelemetry.Extensions.Hosting
65-
dotnet add package OpenTelemetry.Exporter.OpenTelemetryProtocol
103+
dotnet add package OpenTelemetry.Contrib.Instrumentation.AWS
66104
```
67105

68-
Next, in your application’s **Startup.cs** file, add the instrumentation and the OTLP exporter to `TracerProviderBuilder` and build.
106+
Call `AddAWSInstrumentation()` to add AWS SDK client instrumentation to `TracerProvider`. The below example is for an ASP.NET Core application.
69107

70108
```csharp
71109
using OpenTelemetry;
@@ -75,91 +113,47 @@ using OpenTelemetry.Trace;
75113
public void ConfigureServices(IServiceCollection services)
76114
{
77115
services.AddControllers();
78-
tracerProviderBuilder
79-
.AddAspNetCoreInstrumentation()
80-
.AddOtlpExporter()
81-
.Build();
116+
117+
tracerProvider.AddAWSInstrumentation() // for tracing calls to AWS services via AWS SDK for .NET
118+
.AddAspNetCoreInstrumentation()
119+
.AddOtlpExporter()
120+
.Build();
82121
}
83122
```
84123

85-
By default the OTLP exporter sends data to an OpenTelemetry collector at **localhost:4317**.
86-
87-
<SubSectionSeparator />
88-
89-
### ASP.NET
90-
91-
Download the ASP.NET and OTLP exporter instrumentation packages
124+
<SectionSeparator />
92125

93-
```shell
94-
dotnet add package OpenTelemetry.Instrumentation.AspNet
95-
dotnet add package OpenTelemetry.Exporter.OpenTelemetryProtocol
96-
```
126+
## Custom Instrumentation
97127

98-
The ASP.NET instrumentation requires [modification](https://github.com/open-telemetry/opentelemetry-dotnet/blob/master/src/OpenTelemetry.Instrumentation.AspNet/README.md#step-2-modify-webconfig)
99-
to **Web.config** to add a module to your web server. When installing `OpenTelemetry.Instrumentation.AspNet` nuget package, the `TelemetryCorrelationHttpModule` should be automatically added.
100-
101-
```xml
102-
<system.webServer>
103-
<modules>
104-
<add name="TelemetryCorrelationHttpModule"
105-
type="Microsoft.AspNet.TelemetryCorrelation.TelemetryCorrelationHttpModule,
106-
Microsoft.AspNet.TelemetryCorrelation"
107-
preCondition="integratedMode,managedHandler" />
108-
</modules>
109-
</system.webServer>
110-
```
128+
### Creating Custom Spans
111129

112-
Now all you need to do is add the instrumentation and exporter to `TracerProviderBuilder` and build. This is done in the **Global.asax.cs** as shown below.
130+
In .NET, you can use the activity API to create custom spans to monitor the performance of internal activities that are not captured by instrumentation libraries.
113131

114132
```csharp
115-
using OpenTelemetry;
116-
using OpenTelemetry.Contrib.Extensions.AWSXRay.Trace;
117-
using OpenTelemetry.Trace;
133+
using System.Diagnostics;
118134

119-
public class WebApiApplication : HttpApplication
120-
{
121-
protected void Application_Start()
122-
{
123-
tracerProvider
124-
.AddAspNetInstrumentation()
125-
.AddOtlpExporter()
126-
.Build();
127-
}
128-
129-
protected void Application_End()
130-
{
131-
tracerProvider?.Dispose();
132-
}
133-
}
135+
ActivitySource activitySource = new ActivitySource("ActivitySourceName", "ActivitySourceVersion");
136+
var activity = activitySource.StartActivity("ActivityName", ActivityKind.Server); // this will be translated to a X-Ray Segment
137+
var internalActivity = activitySource.StartActivity("ActivityName", ActivityKind.Internal); // this will be translated to an X-Ray Subsegment
134138
```
135139

136-
<SubSectionSeparator />
137-
138-
### AWS SDK Instrumentation
140+
Note that only spans of kind `Server` are converted into X-Ray segments, all other kind of spans (`Internal`, `Client`, etc.) are converted into X-Ray subsegments. For more on segments and subsegments, see the [AWS X-Ray developer guide](https://docs.aws.amazon.com/xray/latest/devguide/xray-concepts.html#xray-concepts-segments).
139141

140-
For tracing downstream call to AWS services from your .NET application, you will need AWS client instrumentation.
142+
### Adding Custom Attributes
141143

142-
Download the `OpenTelemetry.Contrib.Instrumentation.AWS` package:
144+
You can also add custom key-value pairs as attributes onto your spans.
143145

144-
```shell
145-
dotnet add package OpenTelemetry.Contrib.Instrumentation.AWS
146+
```csharp
147+
activity.SetTag("http.method", "GET");
148+
activity.SetTag("http.url", "http://www.mywebsite.com");
146149
```
147150

148-
Add AWS client instrumentation to `TracerProviderBuilder` and build. The below example is for an ASP.NET Core application.
151+
Attributes are converted to metadata by default. If you configure your collector, you can convert some or all of the attributes to annotations. To read more about X-Ray annotations and metadata see the [AWS X-Ray developer guide](https://docs.aws.amazon.com/xray/latest/devguide/xray-concepts.html#xray-concepts-annotations).
149152

150-
```csharp
151-
using OpenTelemetry;
152-
using OpenTelemetry.Contrib.Extensions.AWSXRay.Trace;
153-
using OpenTelemetry.Trace;
153+
For more information about the activity API, see the [OpenTelemetry .NET SDK's developer guide](https://github.com/open-telemetry/opentelemetry-dotnet/tree/main/src/OpenTelemetry.Api#instrumenting-a-libraryapplication-with-net-activity-api).
154154

155-
public void ConfigureServices(IServiceCollection services)
156-
{
157-
services.AddControllers();
158-
159-
tracerProviderBuilder
160-
.AddAWSInstrumentation() // for tracing calls to AWS services via AWS SDK for .NET
161-
.AddAspNetCoreInstrumentation()
162-
.AddOtlpExporter()
163-
.Build();
164-
}
165-
```
155+
<SectionSeparator />
156+
157+
## Sample Application
158+
159+
Take a reference to the [sample application](https://github.com/aws-observability/aws-otel-dotnet/tree/main/integration-test-app) that is instrumented by ADOT and OpenTelemetry .NET SDK.

0 commit comments

Comments
 (0)