Hello!
@Sai Prabhu Naveen Parimi we are working with @Sefa Teyek and trying to resolve above issues together.
So we've tried to implement a custom approach to enforce writing operation_name in our appinsights logs.
We added custom middleware to start new operation and explicitly add "CustomOperationName" for current activity on every function invocation.
public class TelemetryCorrelationMiddleware : IFunctionsWorkerMiddleware
{
private readonly TelemetryClient _telemetryClient;
public TelemetryCorrelationMiddleware(TelemetryClient telemetryClient)
{
_telemetryClient = telemetryClient.VerifyNotNull(nameof(telemetryClient));
}
public async Task Invoke(FunctionContext context, FunctionExecutionDelegate next)
{
var functionName = context.FunctionDefinition.Name;
using var activity = new Activity(functionName);
activity.AddTag("CustomOperationName", functionName);
activity.AddBaggage("CustomOperationName", functionName);
using (var operation = _telemetryClient.StartOperation<RequestTelemetry>(activity))
{
await next(context);
}
}
}
on the other side we've added custom TelemetryInitializer where we manually check for "CustomOperationName" and try to set it explicitly if possible
public class CustomTelemetryInitializer : ITelemetryInitializer
{
private const string CustomOperationName = "CustomOperationName";
public void Initialize(ITelemetry telemetry)
{
if (telemetry is TraceTelemetry traceTelemetry && string.IsNullOrEmpty(traceTelemetry.Context.Operation.Name))
{
if (traceTelemetry.Properties.TryGetValue(CustomOperationName, out var operationName))
{
traceTelemetry.Context.Operation.Name = operationName;
}
}
if (telemetry is DependencyTelemetry dependencyTelemetry && string.IsNullOrEmpty(dependencyTelemetry.Context.Operation.Name))
{
if (dependencyTelemetry.Properties.TryGetValue(CustomOperationName, out var operationName))
{
dependencyTelemetry.Context.Operation.Name = operationName;
}
}
if (telemetry is EventTelemetry eventTelemetry && string.IsNullOrEmpty(eventTelemetry.Context.Operation.Name))
{
if (eventTelemetry.Properties.TryGetValue(CustomOperationName, out var operationName))
{
eventTelemetry.Context.Operation.Name = operationName;
}
}
if (telemetry is ExceptionTelemetry exceptionTelemetry && string.IsNullOrEmpty(exceptionTelemetry.Context.Operation.Name))
{
if (exceptionTelemetry.Properties.TryGetValue(CustomOperationName, out var operationName))
{
exceptionTelemetry.Context.Operation.Name = operationName;
}
}
}
}
As a result:
- Traces -> inside functions call are written with operation name
- Dependencies -> OK
- Custom Events -> OK
- Exceptions -> OK
- Requests -> No operation name but there is a column "Name" that basically has the same value.
As for [Singleton] we will try to implement solution to this issue in nearest future. I will share a feedback here.
We are considering two major approaches:
- Message Sessions
- Blob lease distributed lock