Enrich Tracing

Manual Instrumentation with OpenTracing

Thundra’s agents are fully compliant with OpenTracing API(If you are not familiar with OpenTracing terminology, you could check this documentation). It means that you can access Thundra’s tracer via OpenTracing’s GlobalTracer instance and build new spans and add some business information to increase observability. The Thundra agent is already creating the root span automatically, where you can track the request time. Also, you can create individual span for the parts of your function that you want to monitor (for database requests, external web service calls, or business transactions). Sample code is as follows.

using System;
using System.Linq;
using System.Threading;
using Amazon.Lambda.Core;
using Microsoft.Extensions.Logging;
using Thundra.Agent.Lambda.Core;
using Thundra.Agent.Log.AspNetCore;
using OpenTracing.Util;
using OpenTracing;
namespace blog_opentracing
{
public class Function : LambdaRequestHandler<string, string>
{
public override string DoHandleRequest(string request, ILambdaContext context)
{
var loggerFactory = new LoggerFactory().AddThundraProvider();
var logger = loggerFactory.CreateLogger<Function>();
Console.WriteLine("Hello OpenTracing.");
ValidateEntity();
using (IScope updateEntityScope = GlobalTracer.Instance.BuildSpan("UpdateEntity").StartActive(finishSpanOnDispose: true))
{
updateEntityScope.Span.SetTag("updatedEntityID", 1);
updateEntityScope.Span.SetTag("entitySize","1KB");
using (IScope updateDatabaseScope = GlobalTracer.Instance.BuildSpan("UpdateDatabase").StartActive(finishSpanOnDispose: true))
{
UpdateDatabase();
}
}
return request?.ToUpper();
}
private void ValidateEntity() {
IScope validationScope = GlobalTracer.Instance.BuildSpan("Validation").StartActive(finishSpanOnDispose: true);
//some validations...
validationScope.Dispose();
}
private void UpdateDatabase()
{
//some database calls...
}
}
}

The resulting trace chart that you will see in Thundra web console for this function, will be like the following image.

As you can see, there is a root span that Thundra automatically created, and other spans are the child of the root span, as they should be.