Skip to content

Commit

Permalink
feat: Improve serverless mode detection (#2661)
Browse files Browse the repository at this point in the history
  • Loading branch information
tippmar-nr authored Aug 1, 2024
1 parent 7608af3 commit 5f5dda8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/Agent/NewRelic/Home/Home.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
</Target>

<ItemGroup>
<PackageReference Include="NewRelic.Agent.Internal.Profiler" Version="10.25.1.10"/>
<PackageReference Include="NewRelic.Agent.Internal.Profiler" Version="10.27.0.15"/>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -126,30 +126,36 @@ namespace NewRelic { namespace Profiler { namespace Configuration
return;
}

auto lambdaInstPoint = _systemCalls->TryGetEnvironmentVariable(_X("_HANDLER"));
// give precedence to the NEW_RELIC_LAMBDA_HANDLER environment variable
auto lambdaInstPoint = _systemCalls->TryGetEnvironmentVariable(_X("NEW_RELIC_LAMBDA_HANDLER"));
if (lambdaInstPoint != nullptr)
{
AddInstrumentationPointToCollectionFromEnvironment(*lambdaInstPoint);
_foundServerlessInstrumentationPoint = true;
return;
LogDebug("Found NEW_RELIC_LAMBDA_HANDLER environment variable: ", *lambdaInstPoint);
if (TryAddInstrumentationPointToCollectionFromEnvironment(*lambdaInstPoint))
{
_foundServerlessInstrumentationPoint = true;
return;
}
}

lambdaInstPoint = _systemCalls->TryGetEnvironmentVariable(_X("NEW_RELIC_LAMBDA_HANDLER"));
lambdaInstPoint = _systemCalls->TryGetEnvironmentVariable(_X("_HANDLER"));
if (lambdaInstPoint != nullptr)
{
AddInstrumentationPointToCollectionFromEnvironment(*lambdaInstPoint);
_foundServerlessInstrumentationPoint = true;
LogDebug("Found _HANDLER environment variable: ", *lambdaInstPoint);
if (TryAddInstrumentationPointToCollectionFromEnvironment(*lambdaInstPoint))
_foundServerlessInstrumentationPoint = true;
}
}

void AddInstrumentationPointToCollectionFromEnvironment(xstring_t text)
bool TryAddInstrumentationPointToCollectionFromEnvironment(xstring_t text)
{
auto segments = Strings::Split(text, _X("::"));
if (segments.size() != 3)
{
LogWarn(text, L" is not a valid method descriptor. It must be in the format 'assembly::class::method'");
return;
return false;
}

LogInfo(L"Serverless mode detected. Assembly: ", segments[0], L" Class: ", segments[1], L" Method: ", segments[2]);

InstrumentationPointPtr instrumentationPoint(new InstrumentationPoint());
Expand All @@ -167,6 +173,8 @@ namespace NewRelic { namespace Profiler { namespace Configuration

(*_instrumentationPointsMap)[instrumentationPoint->GetMatchKey()].insert(instrumentationPoint);
_instrumentationPointsSet->insert(instrumentationPoint);

return true;
}

private:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -956,7 +956,7 @@ namespace NewRelic { namespace Profiler { namespace Configuration { namespace Te
xmlSet->emplace(L"filename", L"<?xml version=\"1.0\" encoding=\"utf-8\"?>");

InstrumentationConfiguration instrumentation(xmlSet, nullptr);
instrumentation.AddInstrumentationPointToCollectionFromEnvironment(_X("MyAssembly::MyNamespace.MyClass::MyMethod"));
instrumentation.TryAddInstrumentationPointToCollectionFromEnvironment(_X("MyAssembly::MyNamespace.MyClass::MyMethod"));

auto instrumentationPoint = instrumentation.TryGetInstrumentationPoint(std::make_shared<MethodRewriter::Test::MockFunction>());
Assert::IsFalse(instrumentationPoint == nullptr);
Expand All @@ -968,21 +968,21 @@ namespace NewRelic { namespace Profiler { namespace Configuration { namespace Te
xmlSet->emplace(L"filename", L"<?xml version=\"1.0\" encoding=\"utf-8\"?>");

InstrumentationConfiguration instrumentation(xmlSet, nullptr);
instrumentation.AddInstrumentationPointToCollectionFromEnvironment(_X("MyAssembly::MyNamespace.MyClass::"));
instrumentation.AddInstrumentationPointToCollectionFromEnvironment(_X("MyAssembly::MyNamespace.MyClass::WrongMethod"));
instrumentation.AddInstrumentationPointToCollectionFromEnvironment(_X("MyAssembly::MyMethod"));
instrumentation.AddInstrumentationPointToCollectionFromEnvironment(_X("MyNamespace.MyClass:MyMethod"));
instrumentation.AddInstrumentationPointToCollectionFromEnvironment(_X("MyMethod"));
instrumentation.AddInstrumentationPointToCollectionFromEnvironment(_X(":::MyMethod"));
instrumentation.AddInstrumentationPointToCollectionFromEnvironment(_X("::::::MyMethod"));
instrumentation.AddInstrumentationPointToCollectionFromEnvironment(_X(":::MyMethod::"));
instrumentation.AddInstrumentationPointToCollectionFromEnvironment(_X("MyAssembly:MyNamespace.MyClass:MyMethod"));
instrumentation.AddInstrumentationPointToCollectionFromEnvironment(_X("MyAssembly/MyNamespace.MyClass/MyMethod"));
instrumentation.AddInstrumentationPointToCollectionFromEnvironment(_X("MyAssembly_MyNamespace.MyClass_MyMethod"));
instrumentation.AddInstrumentationPointToCollectionFromEnvironment(_X("MyAssembly MyNamespace.MyClass MyMethod"));
instrumentation.AddInstrumentationPointToCollectionFromEnvironment(_X(" MyAssembly::MyNamespace.MyClass:MyMethod"));
instrumentation.AddInstrumentationPointToCollectionFromEnvironment(_X("MyAssembly::MyNamespace.MyClass::MyMethod"));
instrumentation.AddInstrumentationPointToCollectionFromEnvironment(_X("MyAssembly::MyNamespace .MyClass:: MyMethod"));
instrumentation.TryAddInstrumentationPointToCollectionFromEnvironment(_X("MyAssembly::MyNamespace.MyClass::"));
instrumentation.TryAddInstrumentationPointToCollectionFromEnvironment(_X("MyAssembly::MyNamespace.MyClass::WrongMethod"));
instrumentation.TryAddInstrumentationPointToCollectionFromEnvironment(_X("MyAssembly::MyMethod"));
instrumentation.TryAddInstrumentationPointToCollectionFromEnvironment(_X("MyNamespace.MyClass:MyMethod"));
instrumentation.TryAddInstrumentationPointToCollectionFromEnvironment(_X("MyMethod"));
instrumentation.TryAddInstrumentationPointToCollectionFromEnvironment(_X(":::MyMethod"));
instrumentation.TryAddInstrumentationPointToCollectionFromEnvironment(_X("::::::MyMethod"));
instrumentation.TryAddInstrumentationPointToCollectionFromEnvironment(_X(":::MyMethod::"));
instrumentation.TryAddInstrumentationPointToCollectionFromEnvironment(_X("MyAssembly:MyNamespace.MyClass:MyMethod"));
instrumentation.TryAddInstrumentationPointToCollectionFromEnvironment(_X("MyAssembly/MyNamespace.MyClass/MyMethod"));
instrumentation.TryAddInstrumentationPointToCollectionFromEnvironment(_X("MyAssembly_MyNamespace.MyClass_MyMethod"));
instrumentation.TryAddInstrumentationPointToCollectionFromEnvironment(_X("MyAssembly MyNamespace.MyClass MyMethod"));
instrumentation.TryAddInstrumentationPointToCollectionFromEnvironment(_X(" MyAssembly::MyNamespace.MyClass:MyMethod"));
instrumentation.TryAddInstrumentationPointToCollectionFromEnvironment(_X("MyAssembly::MyNamespace.MyClass::MyMethod"));
instrumentation.TryAddInstrumentationPointToCollectionFromEnvironment(_X("MyAssembly::MyNamespace .MyClass:: MyMethod"));

auto instrumentationPoint = instrumentation.TryGetInstrumentationPoint(std::make_shared<MethodRewriter::Test::MockFunction>());
Assert::IsFalse(instrumentationPoint == nullptr);
Expand Down

0 comments on commit 5f5dda8

Please sign in to comment.