Skip to content

Commit 39620a4

Browse files
Update apigatewaytracing docs
1 parent d469586 commit 39620a4

File tree

1 file changed

+28
-10
lines changed

1 file changed

+28
-10
lines changed

docs/source/nuget/packages.rst

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ This is another package that is quite independent. It contains extension methods
2626
You can use this package inside a Lambda function or from any place, to send the ``TraceContext`` information in a way that MiddyNet will be able to read using a middleware.
2727

2828
Voxel.MiddyNet.Tracing.Http
29-
--------------------------
29+
---------------------------
3030
This is another package that is quite independent. It contains extension methods to add the information of the ``TraceContext`` object from ``Voxel.MiddyNet.Tracing.Core`` to an HttpRequestMessage via ``Headers``.
3131

3232
You can use this package inside a Lambda function or from any place, to send the ``TraceContext`` information in a way that MiddyNet will be able to read using a middleware.
@@ -95,6 +95,10 @@ This package contains a middleware that reads the ``TraceContext`` information f
9595

9696
The logs will have a property for ``traceparent``, another one for ``tracestate``, and another one for ``trace-id``.
9797

98+
In addition, the MiddyNetContext is enriched with a new entry in the AdditionalContext collection that contains a TraceContext object.
99+
100+
This TraceContext object provides a MutateParentId method that can be used to obtain a traceparent with the same Version, TraceId, and TraceFlags but with a new ParentId that can be used to call other systems, as `recommended by W3C. <https://www.w3.org/TR/trace-context/#mutating-the-traceparent-field>`_
101+
98102
Sample code
99103
^^^^^^^^^^^
100104
A typical use of the middleware for APIGateway will look like this::
@@ -108,17 +112,23 @@ A typical use of the middleware for APIGateway will look like this::
108112

109113
protected override async Task<APIGatewayProxyResponse> Handle(APIGatewayProxyRequest apiEvent, MiddyNetContext context)
110114
{
111-
context.Logger.Log(LogLevel.Info, "hello world");
115+
//This log is enriched with the tracing information received in the headers of the request
116+
context.Logger.Log(LogLevel.Info, "Function called.");
112117

113-
// Do stuff
118+
//If you need to call another system, you need to obtain a traceparent based on the original traceparent
119+
//received but with the ParentId changed
120+
var currentTraceContext = (TraceContext)context.AdditionalContext[ApiGatewayTracingMiddleware.TraceContextKey];
121+
var newTraceContext = TraceContext.MutateParentId(currentTraceContext);
114122

115-
var result = new APIGatewayProxyResponse
123+
//Now you can use this newTraceContext in your calls
124+
var traceparentForCallingAnotherSystem = newTraceContext.TraceParent;
125+
var tracestateForCallingAnotherSystem = newTraceContext.TraceState;
126+
127+
return Task.FromResult(new APIGatewayProxyResponse
116128
{
117129
StatusCode = 200,
118-
Body = "hello from test"
119-
};
120-
121-
return Task.FromResult(result);
130+
Body = "Ok"
131+
});
122132
}
123133
}
124134

@@ -133,9 +143,17 @@ and for APIGatewayHttpV2Api will look like this::
133143

134144
protected override Task<APIGatewayHttpApiV2ProxyResponse> Handle(APIGatewayHttpApiV2ProxyRequest proxyRequest, MiddyNetContext context)
135145
{
136-
context.Logger.Log(LogLevel.Info, "hello world");
146+
//This log is enriched with the tracing information received in the headers of the request
147+
context.Logger.Log(LogLevel.Info, "Function called.");
137148

138-
// Do stuff
149+
//If you need to call another system, you need to obtain a traceparent based on the original traceparent
150+
//received but with the ParentId changed
151+
var currentTraceContext = (TraceContext)context.AdditionalContext[ApiGatewayHttpApiV2TracingMiddleware.TraceContextKey];
152+
var newTraceContext = TraceContext.MutateParentId(currentTraceContext);
153+
154+
//Now you can use this newTraceContext in your calls
155+
var traceparentForCallingAnotherSystem = newTraceContext.TraceParent;
156+
var tracestateForCallingAnotherSystem = newTraceContext.TraceState;
139157

140158
return Task.FromResult(new APIGatewayHttpApiV2ProxyResponse
141159
{

0 commit comments

Comments
 (0)