From 5f5dda860a78152574f71f4f1095248707e8c7e3 Mon Sep 17 00:00:00 2001 From: Marty T <120425148+tippmar-nr@users.noreply.github.com> Date: Thu, 1 Aug 2024 14:44:02 -0500 Subject: [PATCH] feat: Improve serverless mode detection (#2661) --- src/Agent/NewRelic/Home/Home.csproj | 2 +- .../InstrumentationConfiguration.h | 26 +++++++++------ .../InstrumentationConfigurationTest.cpp | 32 +++++++++---------- 3 files changed, 34 insertions(+), 26 deletions(-) diff --git a/src/Agent/NewRelic/Home/Home.csproj b/src/Agent/NewRelic/Home/Home.csproj index 11f2614b9b..d14d4762ed 100644 --- a/src/Agent/NewRelic/Home/Home.csproj +++ b/src/Agent/NewRelic/Home/Home.csproj @@ -13,7 +13,7 @@ - + diff --git a/src/Agent/NewRelic/Profiler/Configuration/InstrumentationConfiguration.h b/src/Agent/NewRelic/Profiler/Configuration/InstrumentationConfiguration.h index eee0f162ab..bcc6fbeeb4 100644 --- a/src/Agent/NewRelic/Profiler/Configuration/InstrumentationConfiguration.h +++ b/src/Agent/NewRelic/Profiler/Configuration/InstrumentationConfiguration.h @@ -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()); @@ -167,6 +173,8 @@ namespace NewRelic { namespace Profiler { namespace Configuration (*_instrumentationPointsMap)[instrumentationPoint->GetMatchKey()].insert(instrumentationPoint); _instrumentationPointsSet->insert(instrumentationPoint); + + return true; } private: diff --git a/src/Agent/NewRelic/Profiler/ConfigurationTest/InstrumentationConfigurationTest.cpp b/src/Agent/NewRelic/Profiler/ConfigurationTest/InstrumentationConfigurationTest.cpp index 0c9c941a3f..01a91d5b13 100644 --- a/src/Agent/NewRelic/Profiler/ConfigurationTest/InstrumentationConfigurationTest.cpp +++ b/src/Agent/NewRelic/Profiler/ConfigurationTest/InstrumentationConfigurationTest.cpp @@ -956,7 +956,7 @@ namespace NewRelic { namespace Profiler { namespace Configuration { namespace Te xmlSet->emplace(L"filename", L""); 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()); Assert::IsFalse(instrumentationPoint == nullptr); @@ -968,21 +968,21 @@ namespace NewRelic { namespace Profiler { namespace Configuration { namespace Te xmlSet->emplace(L"filename", L""); 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()); Assert::IsFalse(instrumentationPoint == nullptr);