Skip to content

Bug: Tracing does not work with Top Level Lambda functions #735

Closed
1 of 1 issue completed
Closed
@nCubed

Description

@nCubed

Expected Behaviour

When using .net/C# Top Level Lambda functions, tracing, trace details should be available in AWS X-Ray traces console.

does not appear to be working.

Current Behaviour

When using .net/C# Top Level Lambda functions, tracing, trace details are not available in AWS X-Ray traces console.

Code snippet

using System.Drawing;
using System.Reflection;
using System.Text.Json;
using System.Text.Json.Serialization;
using Amazon.Lambda.Core;
using Amazon.Lambda.RuntimeSupport;
using Amazon.Lambda.Serialization.SystemTextJson;
using AWS.Lambda.Powertools.Logging;
using AWS.Lambda.Powertools.Tracing;

await LambdaBootstrapBuilder.Create((Func<string, ILambdaContext, Task<string>>)Handler, new DefaultLambdaJsonSerializer())
        .Build()
        .RunAsync();

[Tracing]
[Logging]
async Task<string> Handler(string input, ILambdaContext context)
{
    LogAssembly();

    string json = await CreateAnonObjectAsJson(input);

    Logger.LogInformation(json);

    return await Task.FromResult(json);
}

[Tracing]
void LogAssembly()
{
    Assembly a = Assembly.GetAssembly(typeof(Logger))!;
    Logger.LogInformation(a.FullName);
    Logger.LogInformation(Pet.Lizard);
    Logger.LogInformation(Thing.Three);
    Logger.LogInformation(Color.MediumVioletRed);
}

[Tracing]
async Task<string> CreateAnonObjectAsJson(string input)
{
    var val = new
    {
        input,
        utcDate = DateTime.UtcNow.ToString("s"),
        date = DateTime.Now.ToString("s"),
        pet = Pet.Lizard,
        thing = Thing.Three
    };

    string json = JsonSerializer.Serialize(val, new JsonSerializerOptions { WriteIndented = true });

    await Task.Delay(690);

    return json;
}

[JsonConverter(typeof(JsonStringEnumConverter))]
public enum Pet
{
    Cat = 1,
    Dog = 3,
    Lizard = 5
}

public enum Thing
{
    One = 1,
    Three = 3,
    Five = 5
}

Possible Solution

I suspect this may be related to a similar issue we found with Top Level statement functions and the logger. See #656

Steps to Reproduce

Run the sample, view the x-ray console, no tracing other than the standard traces exist.

Powertools for AWS Lambda (.NET) version

1.6.0

AWS Lambda function runtime

dotnet8

Debugging logs

v1.6.2 PowerTools.Logging
v1.6.0 PowerTools.Tracing

Sub-issues

Metadata

Metadata

Assignees

Labels

bugUnexpected, reproducible and unintended software behaviour

Type

No type

Projects

Status

✅ Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions