I'm using NewRelic to gain insight on my system, In order to add custom attribute to the new-relic's transaction, I'm resolving the IAgent
interface with NewRelic.Api.Agent.NewRelic.GetAgent()
(docs)
Like:
public async Task<IActionResult> MyMethod([FromBody]MyRequest request)
{
//Do Some logic.
IAgent agent = NewRelic.Api.Agent.NewRelic.GetAgent();
ITransaction transaction = agent.CurrentTransaction;
transaction.AddCustomAttribute("MyCustomAttribute", SomeValue)
return Ok(...);
}
Is it possible to use the dependency injection container in order to resolve the IAgent
interface? like:
[ApiController]
public class MyController
{
private readonly IAgent _agnet;
public MyController(IAgent agent)
{
_agent = agent;
}
public async Task<IActionResult> MyMethod([FromBody]MyRequest request)
{
//Do Some logic.
ITransaction transaction = this._agent.CurrentTransaction;
transaction.AddCustomAttribute("MyCustomAttribute", SomeValue)
return Ok(...);
}
}
Hence it's a static instance, it can be registered as a singleton:
services.AddSingletone<IAgent>(NewRelic.Api.Agent.NewRelic.GetAgent());
or:
services.AddSingletone(NewRelic.Api.Agent.NewRelic.GetAgent());
Don't forget to configure the agent first, then add it to IServiceCollection
.