apm-agent-dotnet icon indicating copy to clipboard operation
apm-agent-dotnet copied to clipboard

[Feature Request] Allow AspNetCore users hook Agent setup process

Open yhvicey opened this issue 5 years ago • 3 comments

Is your feature request related to a problem? Please describe. For now AspNetCore users can only configure the agent setup process by using IConfiguration, there's no way to setup an customized IApmAgent and pass it to the middleware.

Describe the solution you'd like Provide some interfaces like below to allow users to hook the agent setup process:

// Extension method in ApmMiddlewareExtensions.cs
public static IApplicationBuilder UseElasticApm(
    this IApplicationBuilder builder,
    IConfiguration configuration = null,
    params IDiagnosticsSubscriber[] subscribers)
    => UseElasticApm(builder, _ => { }, configuration, subscribers);

public static IApplicationBuilder UseElasticApm(
    this IApplicationBuilder builder,
    Action<AgentComponents> configureComponents,
    IConfiguration configuration = null,
    params IDiagnosticsSubscriber[] subscribers)
{
    ...
    var config = new AgentComponents(configurationReader: configReader, logger: logger);
    UpdateServiceInformation(config.Service);
    configureComponents?.Invoke(config);
    ...
}

// Users' Startup class
public void Configure(IApplicationBuilder app)
{
    ...
    app.UseElasticApm(
        Configuration,
        components =>
        {
            // Hook agent setup process
        }
    );
    ...
}

Describe alternatives you've considered Or allow us pass IApmAgent instance to the middleware when using them

yhvicey avatar Nov 21 '19 07:11 yhvicey

Thanks for opening @yhvicey,

just out of interest, what would you like to do in Action<AgentComponents> configureComponents?

gregkalapos avatar Nov 21 '19 09:11 gregkalapos

I'd like to build a wrapper project integrating ElasticApm into Generic Host using IServiceProvider only (only hooks HttpDiagnosticsSubscriber), then build an wrapper project for AspNetCore base on that project using ApmMiddleware using the Agent build in first project.

To achieve this, the approach I mentioned above might need to go deeper into the Agent.Setup:

// Agent.cs
public static void Setup(
    AgentComponents agentComponents,
    Action<AgentComponents> configureComponents = null)
{
    ...
    _components = agentComponents;
    configureComponents?.Invoke(_components);
    ...
}

// Extension method in ApmMiddlewareExtensions.cs
public static IApplicationBuilder UseElasticApm(
    this IApplicationBuilder builder,
    IConfiguration configuration = null,
    params IDiagnosticsSubscriber[] subscribers)
    => UseElasticApm(builder, _ => { }, configuration, subscribers);

public static IApplicationBuilder UseElasticApm(
    this IApplicationBuilder builder,
    Action<AgentComponents> configureComponents,
    IConfiguration configuration = null,
    params IDiagnosticsSubscriber[] subscribers)
{
    ...
    Agent.Setup(config, configureComponents);
    ...
}

// Users' Startup class
public void Configure(IApplicationBuilder app)
{
    ...
    app.UseElasticApm(
        Configuration,
        components =>
        {
            // Hook agent setup process
        }
    );
    ...
}

The major pain point here is that we cannot use our own IApmAgent instance in the middleware, neither can we get the singleton in Agent class. Besides, the middleware accepts arguments of type Tracer rather than ITracer, which makes it even harder to use the implementation with our own IApmAgent instance (which provides ITracer tracer only).

yhvicey avatar Nov 21 '19 09:11 yhvicey

I'd like to build a wrapper project integrating ElasticApm into Generic Host using IServiceProvider only (only hooks HttpDiagnosticsSubscriber), then build an wrapper project for AspNetCore base on that project using ApmMiddleware

Struggling with this too. Did you ever find a solution/workaround? I am surprised it is harder to setup agent + use asp.net ie for example use with background workers.

Swoorup avatar May 30 '23 06:05 Swoorup