Skip to content

Commit

Permalink
feat: Auto-instrument ASP.NET Core Lambda functions (#2662) (#2674)
Browse files Browse the repository at this point in the history
  • Loading branch information
tippmar-nr committed Aug 7, 2024
1 parent c3faea2 commit ceaefc5
Show file tree
Hide file tree
Showing 21 changed files with 808 additions and 10 deletions.
1 change: 1 addition & 0 deletions .github/workflows/all_solutions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ jobs:
Api,
AppDomainCaching,
AspNetCore,
AwsLambda.AutoInstrumentation,
AwsLambda.CloudWatch,
AwsLambda.Custom,
AwsLambda.DynamoDb,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ public bool ValidateWebRequestParameters(InstrumentedMethodCall instrumentedMeth
{
dynamic requestContext = input.RequestContext;

return !string.IsNullOrEmpty(requestContext.Http.Method) && !string.IsNullOrEmpty(requestContext.Http.Path);
if (requestContext.Http != null)
return !string.IsNullOrEmpty(requestContext.Http.Method) && !string.IsNullOrEmpty(requestContext.Http.Path);
}

return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,11 @@ SPDX-License-Identifier: Apache-2.0
<exactMethodMatcher methodName=".ctor" />
</match>
</tracerFactory>
<!-- Instrument the function handler for AspNetCore lambdas -->
<tracerFactory name="NewRelic.Providers.Wrapper.AwsLambda.HandlerMethod">
<match assemblyName="Amazon.Lambda.AspNetCoreServer" className="Amazon.Lambda.AspNetCoreServer.AbstractAspNetCoreFunction`2">
<exactMethodMatcher methodName="FunctionHandlerAsync" />
</match>
</tracerFactory>
</instrumentation>
</extension>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 New Relic, Inc. All rights reserved.
// Copyright 2020 New Relic, Inc. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

using CommandLine;
Expand Down Expand Up @@ -47,7 +47,8 @@ public static string GetPortFromArgs(string[] args)
var commandLine = string.Join(" ", args);
Log($"Joined args: {commandLine}");

Parser.Default.ParseArguments<Options>(args)
new Parser(with => { with.IgnoreUnknownArguments = true;})
.ParseArguments<Options>(args)
.WithParsed(o =>
{
portToUse = o.Port ?? DefaultPort;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<AWSProjectType>Lambda</AWSProjectType>
<!-- This property makes the build directory similar to a publish directory and helps the AWS .NET Lambda Mock Test Tool find project dependencies. -->
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Amazon.Lambda.AspNetCoreServer" Version="9.0.0" />
<PackageReference Include="Amazon.Lambda.RuntimeSupport" Version="1.10.0" />
<PackageReference Include="Amazon.Lambda.Serialization.SystemTextJson" Version="2.4.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\ApplicationHelperLibraries\ApplicationLifecycle\ApplicationLifecycle.csproj" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2020 New Relic, Inc. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

using Microsoft.AspNetCore.Mvc;

namespace AspNetCoreWebApiLambdaApplication.Controllers
{
[Route("api/[controller]")]
public class ValuesController : ControllerBase
{
public ValuesController()
{

}

// GET api/values
[HttpGet]
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}

// GET api/values/5
[HttpGet("{id}")]
public string Get(int id)
{
return "value";
}

// POST api/values
[HttpPost]
public void Post([FromBody] string value)
{
}

// PUT api/values/5
[HttpPut("{id}")]
public void Put(int id, [FromBody] string value)
{
}

// DELETE api/values/5
[HttpDelete("{id}")]
public void Delete(int id)
{
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2020 New Relic, Inc. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

namespace AspNetCoreWebApiLambdaApplication
{
public class APIGatewayProxyFunctionEntryPoint : Amazon.Lambda.AspNetCoreServer.APIGatewayProxyFunction
{
protected override void Init(IWebHostBuilder builder)
{
builder
.UseStartup<Startup>();
}

protected override void Init(IHostBuilder builder)
{
}
}

public class ApplicationLoadBalancerFunctionEntryPoint :Amazon.Lambda.AspNetCoreServer.ApplicationLoadBalancerFunction
{
protected override void Init(IWebHostBuilder builder)
{
builder.UseStartup<Startup>();
}

protected override void Init(IHostBuilder builder)
{
}
}

public class APIGatewayHttpApiV2ProxyFunctionEntryPoint : Amazon.Lambda.AspNetCoreServer.APIGatewayHttpApiV2ProxyFunction
{
protected override void Init(IWebHostBuilder builder)
{
builder.UseStartup<Startup>();
}

protected override void Init(IHostBuilder builder)
{
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// Copyright 2020 New Relic, Inc. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

using System.Diagnostics;
using Amazon.Lambda.APIGatewayEvents;
using Amazon.Lambda.ApplicationLoadBalancerEvents;
using Amazon.Lambda.Core;
using Amazon.Lambda.RuntimeSupport;
using Amazon.Lambda.Serialization.SystemTextJson;
using ApplicationLifecycle;
using CommandLine;

namespace AspNetCoreWebApiLambdaApplication
{
internal class Program
{
private class Options
{
[Option("handler", Required = true, HelpText = "Handler function to use.")]
public string Handler { get; set; }
}

private static string _port = "";
private static string _handlerToInvoke = "";

private static void Main(string[] args)
{
_port = AppLifecycleManager.GetPortFromArgs(args);

_handlerToInvoke = GetHandlerFromArgs(args);

using var cancellationTokenSource = new CancellationTokenSource();
using var handlerWrapper = GetHandlerWrapper();

// Instantiate a LambdaBootstrap and run it.
// It will wait for invocations from AWS Lambda and call the handler function for each one.
using var bootstrap = new LambdaBootstrap(handlerWrapper);

_ = bootstrap.RunAsync(cancellationTokenSource.Token);

AppLifecycleManager.CreatePidFile();

AppLifecycleManager.WaitForTestCompletion(_port);

cancellationTokenSource.Cancel();
}

private static string GetHandlerFromArgs(string[] args)
{
var handler = string.Empty;

var commandLine = string.Join(" ", args);

new Parser(with => { with.IgnoreUnknownArguments = true; })
.ParseArguments<Options>(args)
.WithParsed(o =>
{
handler = o.Handler;
});

if (string.IsNullOrEmpty(handler))
throw new Exception("--handler commandline argument could not be parsed.");

return handler;
}

private static HandlerWrapper GetHandlerWrapper()
{
var defaultLambdaJsonSerializer = new DefaultLambdaJsonSerializer();

switch (_handlerToInvoke)
{
case "APIGatewayProxyFunctionEntryPoint":
{
var entryPoint = new APIGatewayProxyFunctionEntryPoint();
Func<APIGatewayProxyRequest, ILambdaContext, Task<APIGatewayProxyResponse>> handlerFunc = entryPoint.FunctionHandlerAsync;

return HandlerWrapper.GetHandlerWrapper(handlerFunc, defaultLambdaJsonSerializer);
}
case "ApplicationLoadBalancerFunctionEntryPoint":
{
var entryPoint = new ApplicationLoadBalancerFunctionEntryPoint();
Func<ApplicationLoadBalancerRequest, ILambdaContext, Task<ApplicationLoadBalancerResponse>> handlerFunc = entryPoint.FunctionHandlerAsync;

return HandlerWrapper.GetHandlerWrapper(handlerFunc, defaultLambdaJsonSerializer);
}
case "APIGatewayHttpApiV2ProxyFunctionEntryPoint":
{
var entryPoint = new APIGatewayHttpApiV2ProxyFunctionEntryPoint();
Func<APIGatewayHttpApiV2ProxyRequest, ILambdaContext, Task<APIGatewayHttpApiV2ProxyResponse>> handlerFunc = entryPoint.FunctionHandlerAsync;

return HandlerWrapper.GetHandlerWrapper(handlerFunc, defaultLambdaJsonSerializer);
}
default:
throw new ArgumentException($"Handler not found: {_handlerToInvoke}");
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2020 New Relic, Inc. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

namespace AspNetCoreWebApiLambdaApplication
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

//app.UseHttpsRedirection();

app.UseRouting();

//app.UseAuthorization();

app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapGet("/", async context =>
{
await context.Response.WriteAsync("Welcome to running ASP.NET Core on AWS Lambda");
});
});
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"AWS": {
"Region": ""
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"Logging": {
"LogLevel": {
"Default": "Information"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"Information": [
"This file provides default values for the deployment wizard inside Visual Studio and the AWS Lambda commands added to the .NET Core CLI.",
"To learn more about the Lambda commands with the .NET Core CLI execute the following command at the command line in the project root directory.",
"dotnet lambda help",
"All the command line options for the Lambda command can be specified in this file."
],
"profile": "",
"region": "",
"configuration": "Release",
"s3-prefix": "AspNetCoreWebApiLambdaApplication/",
"template": "serverless.template",
"template-parameters": ""
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"AWSTemplateFormatVersion": "2010-09-09",
"Transform": "AWS::Serverless-2016-10-31",
"Description": "An AWS Serverless Application that uses the ASP.NET Core framework running in Amazon Lambda.",
"Parameters": {},
"Conditions": {},
"Resources": {
"AspNetCoreFunction": {
"Type": "AWS::Serverless::Function",
"Properties": {
"Handler": "AspNetCoreWebApiLambdaApplication::AspNetCoreWebApiLambdaApplication.APIGatewayHttpApiV2ProxyFunctionEntryPoint::FunctionHandlerAsync",
"Runtime": "dotnet8",
"CodeUri": "",
"MemorySize": 512,
"Timeout": 30,
"Role": null,
"Policies": [
"AWSLambda_FullAccess"
],
"Events": {
"ProxyResource": {
"Type": "Api",
"Properties": {
"Path": "/{proxy+}",
"Method": "ANY"
}
},
"RootResource": {
"Type": "Api",
"Properties": {
"Path": "/",
"Method": "ANY"
}
}
}
}
}
},
"Outputs": {
"ApiURL": {
"Description": "API endpoint URL for Prod environment",
"Value": {
"Fn::Sub": "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/"
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,11 @@ public virtual void Initialize()
throw new Exception(message);
}
}

catch (Exception ex)
{
TestLogger?.WriteLine("Exception occurred in Initialize: " + ex.ToString());
throw;
}
finally
{
if (AgentLogExpected)
Expand Down
Loading

0 comments on commit ceaefc5

Please sign in to comment.