Skip to content

Commit 66595e4

Browse files
committed
docs(tracing): extract inline code examples into snippet files
Extract 16 inline C# code blocks from docs/core/tracing.md into standalone snippet files under docs/snippets/tracing/ using pymdownx.snippets includes. Normalize language tags from c# to csharp. No visual changes to rendered documentation. Closes #1184 Refs #794, #1104
1 parent f84d45e commit 66595e4

8 files changed

Lines changed: 324 additions & 232 deletions

File tree

docs/core/tracing.md

Lines changed: 32 additions & 232 deletions
Original file line numberDiff line numberDiff line change
@@ -76,46 +76,14 @@ segment name that appears in traces.
7676

7777
=== "Tracing attribute"
7878

79-
```c# hl_lines="3 14 20"
80-
public class Function
81-
{
82-
[Tracing]
83-
public async Task<APIGatewayProxyResponse> FunctionHandler
84-
(APIGatewayProxyRequest apigProxyEvent, ILambdaContext context)
85-
{
86-
await BusinessLogic1()
87-
.ConfigureAwait(false);
88-
89-
await BusinessLogic2()
90-
.ConfigureAwait(false);
91-
}
92-
93-
[Tracing]
94-
private async Task BusinessLogic1()
95-
{
96-
97-
}
98-
99-
[Tracing]
100-
private async Task BusinessLogic2()
101-
{
102-
103-
}
104-
}
79+
```csharp hl_lines="3 14 20"
80+
--8<-- "docs/snippets/tracing/GettingStarted.cs:tracing_attribute"
10581
```
10682

10783
=== "Custom Segment names"
10884

109-
```c# hl_lines="3"
110-
public class Function
111-
{
112-
[Tracing(SegmentName = "YourCustomName")]
113-
public async Task<APIGatewayProxyResponse> FunctionHandler
114-
(APIGatewayProxyRequest apigProxyEvent, ILambdaContext context)
115-
{
116-
...
117-
}
118-
}
85+
```csharp hl_lines="3"
86+
--8<-- "docs/snippets/tracing/GettingStarted.cs:custom_segment_name"
11987
```
12088

12189
By default, this attribute will automatically record method responses and exceptions. You can change the default behavior by setting
@@ -127,16 +95,8 @@ the environment variables `POWERTOOLS_TRACER_CAPTURE_RESPONSE` and `POWERTOOLS_T
12795

12896
=== "Disable on attribute"
12997

130-
```c# hl_lines="3"
131-
public class Function
132-
{
133-
[Tracing(CaptureMode = TracingCaptureMode.Disabled)]
134-
public async Task<APIGatewayProxyResponse> FunctionHandler
135-
(APIGatewayProxyRequest apigProxyEvent, ILambdaContext context)
136-
{
137-
...
138-
}
139-
}
98+
```csharp hl_lines="3"
99+
--8<-- "docs/snippets/tracing/GettingStarted.cs:disable_capture_mode"
140100
```
141101

142102
=== "Disable Globally"
@@ -167,35 +127,15 @@ context for an operation using any native object.
167127
=== "Annotations"
168128

169129
You can add annotations using `AddAnnotation()` method from Tracing
170-
```c# hl_lines="9"
171-
using AWS.Lambda.Powertools.Tracing;
172-
173-
public class Function
174-
{
175-
[Tracing]
176-
public async Task<APIGatewayProxyResponse> FunctionHandler
177-
(APIGatewayProxyRequest apigProxyEvent, ILambdaContext context)
178-
{
179-
Tracing.AddAnnotation("annotation", "value");
180-
}
181-
}
130+
```csharp hl_lines="9"
131+
--8<-- "docs/snippets/tracing/AnnotationsAndMetadata.cs:add_annotation"
182132
```
183133

184134
=== "Metadata"
185135

186136
You can add metadata using `AddMetadata()` method from Tracing
187-
```c# hl_lines="9"
188-
using AWS.Lambda.Powertools.Tracing;
189-
190-
public class Function
191-
{
192-
[Tracing]
193-
public async Task<APIGatewayProxyResponse> FunctionHandler
194-
(APIGatewayProxyRequest apigProxyEvent, ILambdaContext context)
195-
{
196-
Tracing.AddMetadata("content", "value");
197-
}
198-
}
137+
```csharp hl_lines="9"
138+
--8<-- "docs/snippets/tracing/AnnotationsAndMetadata.cs:add_metadata"
199139
```
200140

201141
## Utilities
@@ -209,110 +149,34 @@ You can create subsegments using the familiar `using` statement pattern for auto
209149

210150
=== "Basic Using Statement"
211151

212-
```c# hl_lines="8 9 10 11 12 13"
213-
using AWS.Lambda.Powertools.Tracing;
214-
215-
public class Function
216-
{
217-
public async Task<APIGatewayProxyResponse> FunctionHandler
218-
(APIGatewayProxyRequest apigProxyEvent, ILambdaContext context)
219-
{
220-
using var gatewaySegment = Tracing.BeginSubsegment("PaymentGatewayIntegration");
221-
gatewaySegment.AddAnnotation("Operation", "ProcessPayment");
222-
gatewaySegment.AddAnnotation("PaymentMethod", "CreditCard");
223-
224-
var result = await ProcessPaymentAsync();
225-
gatewaySegment.AddAnnotation("ProcessingTimeMs", result.ProcessingTimeMs);
226-
// Subsegment automatically ends when disposed
227-
}
228-
}
152+
```csharp hl_lines="8 9 10 11 12 13"
153+
--8<-- "docs/snippets/tracing/UsingStatementPattern.cs:basic_using_statement"
229154
```
230155

231156
=== "With Custom Namespace"
232157

233-
```c# hl_lines="8 9 10"
234-
using AWS.Lambda.Powertools.Tracing;
235-
236-
public class Function
237-
{
238-
public async Task<APIGatewayProxyResponse> FunctionHandler
239-
(APIGatewayProxyRequest apigProxyEvent, ILambdaContext context)
240-
{
241-
using var segment = Tracing.BeginSubsegment("MyCustomNamespace", "DatabaseOperation");
242-
segment.AddAnnotation("TableName", "Users");
243-
segment.AddMetadata("query", "SELECT * FROM Users WHERE Active = 1");
244-
}
245-
}
158+
```csharp hl_lines="8 9 10"
159+
--8<-- "docs/snippets/tracing/UsingStatementPattern.cs:custom_namespace"
246160
```
247161

248162
=== "Nested Subsegments"
249163

250-
```c# hl_lines="8 9 10 11 12 13 14 15 16"
251-
using AWS.Lambda.Powertools.Tracing;
252-
253-
public class Function
254-
{
255-
public async Task<APIGatewayProxyResponse> FunctionHandler
256-
(APIGatewayProxyRequest apigProxyEvent, ILambdaContext context)
257-
{
258-
using var outerSegment = Tracing.BeginSubsegment("PaymentProcessing");
259-
outerSegment.AddAnnotation("Operation", "ProcessPayment");
260-
261-
var result = await ProcessPaymentAsync();
262-
263-
using var postProcessingSegment = Tracing.BeginSubsegment("PaymentPostProcessing");
264-
postProcessingSegment.AddAnnotation("PaymentId", result.PaymentId);
265-
266-
await PostProcessPaymentAsync(result);
267-
}
268-
}
164+
```csharp hl_lines="8 9 10 11 12 13 14 15 16"
165+
--8<-- "docs/snippets/tracing/UsingStatementPattern.cs:nested_subsegments"
269166
```
270167

271168
### Callback Pattern
272169

273170
=== "Functional Api"
274171

275-
```c# hl_lines="8 9 10 12 13 14"
276-
using AWS.Lambda.Powertools.Tracing;
277-
278-
public class Function
279-
{
280-
public async Task<APIGatewayProxyResponse> FunctionHandler
281-
(APIGatewayProxyRequest apigProxyEvent, ILambdaContext context)
282-
{
283-
Tracing.WithSubsegment("loggingResponse", (subsegment) => {
284-
// Some business logic
285-
});
286-
287-
Tracing.WithSubsegment("localNamespace", "loggingResponse", (subsegment) => {
288-
// Some business logic
289-
});
290-
}
291-
}
172+
```csharp hl_lines="8 9 10 12 13 14"
173+
--8<-- "docs/snippets/tracing/CallbackPattern.cs:functional_api"
292174
```
293175

294176
=== "Multi Threaded Programming"
295177

296-
```c# hl_lines="13-16"
297-
using AWS.Lambda.Powertools.Tracing;
298-
299-
public class Function
300-
{
301-
public async Task<APIGatewayProxyResponse> FunctionHandler
302-
(APIGatewayProxyRequest apigProxyEvent, ILambdaContext context)
303-
{
304-
// Extract existing trace data
305-
var entity = Tracing.GetEntity();
306-
307-
var task = Task.Run(() =>
308-
{
309-
Tracing.WithSubsegment("InlineLog", entity, (subsegment) =>
310-
{
311-
// Business logic in separate task
312-
});
313-
});
314-
}
315-
}
178+
```csharp hl_lines="13-16"
179+
--8<-- "docs/snippets/tracing/CallbackPattern.cs:multi_threaded"
316180
```
317181

318182
### Subsegment Methods
@@ -321,23 +185,8 @@ When using the `using` statement pattern, the returned `TracingSubsegment` objec
321185

322186
=== "Available Methods"
323187

324-
```c# hl_lines="8 9 10 11 12 13 14 15 16"
325-
using var segment = Tracing.BeginSubsegment("PaymentProcessing");
326-
327-
// Add annotations (indexed by X-Ray)
328-
segment.AddAnnotation("PaymentMethod", "CreditCard");
329-
segment.AddAnnotation("Amount", 99.99);
330-
331-
// Add metadata (not indexed, for additional context)
332-
segment.AddMetadata("PaymentDetails", paymentObject);
333-
segment.AddMetadata("CustomNamespace", "RequestId", requestId);
334-
335-
// Add exception information
336-
segment.AddException(exception);
337-
338-
// Add HTTP information
339-
segment.AddHttpInformation("response_code", 200);
340-
segment.AddHttpInformation("url", "https://api.payment.com/process");
188+
```csharp hl_lines="8 9 10 11 12 13 14 15 16"
189+
--8<-- "docs/snippets/tracing/SubsegmentMethods.cs:available_methods"
341190
```
342191

343192
## Instrumenting SDK clients
@@ -346,31 +195,14 @@ You should make sure to instrument the SDK clients explicitly based on the funct
346195

347196
=== "Function.cs"
348197

349-
```c# hl_lines="14"
350-
using Amazon.DynamoDBv2;
351-
using Amazon.DynamoDBv2.Model;
352-
using AWS.Lambda.Powertools.Tracing;
353-
354-
public class Function
355-
{
356-
private static IAmazonDynamoDB _dynamoDb;
357-
358-
/// <summary>
359-
/// Function constructor
360-
/// </summary>
361-
public Function()
362-
{
363-
Tracing.RegisterForAllServices();
364-
365-
_dynamoDb = new AmazonDynamoDBClient();
366-
}
367-
}
198+
```csharp hl_lines="14"
199+
--8<-- "docs/snippets/tracing/SdkInstrumentation.cs:register_all_services"
368200
```
369201

370202
To instrument clients for some services and not others, call Register instead of RegisterForAllServices. Replace the highlighted text with the name of the service's client interface.
371203

372-
```c#
373-
Tracing.Register<IAmazonDynamoDB>()
204+
```csharp
205+
--8<-- "docs/snippets/tracing/SdkInstrumentation.cs:register_single_service"
374206
```
375207

376208
This functionality is a thin wrapper for AWS X-Ray .NET SDK. Refer details on [how to instrument SDK client with Xray](https://docs.aws.amazon.com/xray/latest/devguide/xray-sdk-dotnet-sdkclients.html)
@@ -379,17 +211,8 @@ This functionality is a thin wrapper for AWS X-Ray .NET SDK. Refer details on [h
379211

380212
=== "Function.cs"
381213

382-
```c# hl_lines="7"
383-
using Amazon.XRay.Recorder.Handlers.System.Net;
384-
385-
public class Function
386-
{
387-
public Function()
388-
{
389-
var httpClient = new HttpClient(new HttpClientXRayTracingHandler(new HttpClientHandler()));
390-
var myIp = await httpClient.GetStringAsync("https://checkip.amazonaws.com/");
391-
}
392-
}
214+
```csharp hl_lines="7"
215+
--8<-- "docs/snippets/tracing/SdkInstrumentation.cs:instrument_http_calls"
393216
```
394217

395218
More information about instrumenting [outgoing http calls](https://docs.aws.amazon.com/xray/latest/devguide/xray-sdk-dotnet-httpclients.html).
@@ -408,37 +231,14 @@ Examples:
408231

409232
=== "Without Powertools Logging"
410233

411-
```c# hl_lines="8"
412-
using AWS.Lambda.Powertools.Tracing;
413-
using AWS.Lambda.Powertools.Tracing.Serializers;
414-
415-
private static async Task Main()
416-
{
417-
Func<string, ILambdaContext, string> handler = FunctionHandler;
418-
await LambdaBootstrapBuilder.Create(handler, new SourceGeneratorLambdaJsonSerializer<LambdaFunctionJsonSerializerContext>()
419-
.WithTracing())
420-
.Build()
421-
.RunAsync();
422-
}
234+
```csharp hl_lines="8"
235+
--8<-- "docs/snippets/tracing/AotSupport.cs:without_powertools_logging"
423236
```
424237

425238
=== "With Powertools Logging"
426239

427-
```c# hl_lines="10 11"
428-
using AWS.Lambda.Powertools.Logging;
429-
using AWS.Lambda.Powertools.Logging.Serializers;
430-
using AWS.Lambda.Powertools.Tracing;
431-
using AWS.Lambda.Powertools.Tracing.Serializers;
432-
433-
private static async Task Main()
434-
{
435-
Func<string, ILambdaContext, string> handler = FunctionHandler;
436-
await LambdaBootstrapBuilder.Create(handler,
437-
new PowertoolsSourceGeneratorSerializer<LambdaFunctionJsonSerializerContext>()
438-
.WithTracing())
439-
.Build()
440-
.RunAsync();
441-
}
240+
```csharp hl_lines="10 11"
241+
--8<-- "docs/snippets/tracing/AotSupport.cs:with_powertools_logging"
442242
```
443243

444244
### Publishing
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// This file is referenced by docs/core/tracing.md
2+
// via pymdownx.snippets (mkdocs).
3+
4+
namespace AWS.Lambda.Powertools.Docs.Snippets.Tracing;
5+
6+
// --8<-- [start:add_annotation]
7+
using AWS.Lambda.Powertools.Tracing;
8+
9+
public class Function
10+
{
11+
[Tracing]
12+
public async Task<APIGatewayProxyResponse> FunctionHandler
13+
(APIGatewayProxyRequest apigProxyEvent, ILambdaContext context)
14+
{
15+
Tracing.AddAnnotation("annotation", "value");
16+
}
17+
}
18+
// --8<-- [end:add_annotation]
19+
20+
// --8<-- [start:add_metadata]
21+
using AWS.Lambda.Powertools.Tracing;
22+
23+
public class Function
24+
{
25+
[Tracing]
26+
public async Task<APIGatewayProxyResponse> FunctionHandler
27+
(APIGatewayProxyRequest apigProxyEvent, ILambdaContext context)
28+
{
29+
Tracing.AddMetadata("content", "value");
30+
}
31+
}
32+
// --8<-- [end:add_metadata]

0 commit comments

Comments
 (0)