Skip to content

Bug: Service property no longer uses the LoggingAttribute.Service name when the environment variable is provided #702

Closed
@nCubed

Description

@nCubed

Expected Behaviour

The root level service property of the logging output should be prioritized in this order:

  1. LoggingAttribute.Service property value
  2. POWERTOOLS_SERVICE_NAME environment variable

Current Behaviour

The root level service property of the logging output no longer prioritizes and uses the service name provided prior to the first log call.

Code snippet

// Each log entry is the value of the Root level service property when written to CloudWatch

// v1.5.1 behavior
Logger.LogInformation("Service: service_undefined");

Environment.SetEnvironmentVariable("POWERTOOLS_SERVICE_NAME", "Environment Service", EnvironmentVariableTarget.Process);

Logger.LogInformation("Service: Environment Service");

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

[Logging(Service = "Attribute Service")]
async Task<string> Handler(string input, ILambdaContext context)
{
    Logger.LogInformation("Service: Attribute Service");

    return await Task.FromResult(input);
}


// v1.6.2 behavior - example 1
Logger.LogInformation("Service: service_undefined");

Environment.SetEnvironmentVariable("POWERTOOLS_SERVICE_NAME", "Environment Service", EnvironmentVariableTarget.Process);

Logger.LogInformation("Service: service_undefined");

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

[Logging(Service = "Attribute Service")]
async Task<string> Handler(string input, ILambdaContext context)
{
    Logger.LogInformation("Service: service_undefined");

    return await Task.FromResult(input);
}


// v1.6.2 behavior - example 2
Environment.SetEnvironmentVariable("POWERTOOLS_SERVICE_NAME", "Environment Service", EnvironmentVariableTarget.Process);

Logger.LogInformation("Service: Environment Service");

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

[Logging(Service = "Attribute Service")]
async Task<string> Handler(string input, ILambdaContext context)
{
    Logger.LogInformation("Service: Environment Service");

    return await Task.FromResult(input);
}


// v1.6.2 behavior - example 3
await LambdaBootstrapBuilder.Create((Func<string, ILambdaContext, Task<string>>)Handler, new DefaultLambdaJsonSerializer())
        .Build()
        .RunAsync();

[Logging(Service = "Attribute Service")]
async Task<string> Handler(string input, ILambdaContext context)
{
    Logger.LogInformation("Service: Attribute Service");

    return await Task.FromResult(input);
}

Possible Solution

Rollback to the v1.5.1 behavior.

Steps to Reproduce

Code snippet above provides all the steps to reproduce.

Powertools for AWS Lambda (.NET) version

1.6.2

AWS Lambda function runtime

dotnet8

Debugging logs

Metadata

Metadata

Assignees

Labels

bugUnexpected, reproducible and unintended software behaviourreleasedFix or implementation already in main and releasedstatus/confirmedThe scope is clear, ready for implementation

Type

No type

Projects

Status

✅ Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions