Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to add annotations for every request? #219

Open
Kralizek opened this issue Nov 24, 2021 · 3 comments
Open

How to add annotations for every request? #219

Kralizek opened this issue Nov 24, 2021 · 3 comments

Comments

@Kralizek
Copy link

Kralizek commented Nov 24, 2021

I have an ASP.NET Core application and I want to add an annotation to the trace of each request flowing through this application and I wonder what's the best approach.

My first idea would be to use the Startup constructor but I'm not sure if that would work.

public Startup(IConfiguration configuration, IHostEnvironment hostEnvironment)
{
    AWSXRayRecorder.InitializeInstance(configuration);
    AWSXRayRecorder.RegisterLogger(GetLoggingOptions(configuration));
    AWSSDKHandler.RegisterXRayForAllServices();

    Configuration = configuration;
    HostEnvironment = hostEnvironment;

    AWSXRayRecorder.Instance.AddAnnotation("Project", "MY_SECRET_PROJECT");
    AWSXRayRecorder.Instance.AddAnnotation("Environment", HostEnvironment.EnvironmentName);
    AWSXRayRecorder.Instance.AddAnnotation("Workspace", GetWorkspaceName());
}

Alternatively, I was considering adding an extra step in the pipeline add the snippet below in the Configure method

app.Use(next =>
{
    AWSXRayRecorder.Instance.AddAnnotation("Project", "MY_SECRET_PROJECT");
    AWSXRayRecorder.Instance.AddAnnotation("Environment", HostEnvironment.EnvironmentName);
    AWSXRayRecorder.Instance.AddAnnotation("Workspace", GetWorkspaceName());
    return next;
});

Ideally, it would be nice if UseXRay had an overload that accepted a delegate of type Action<HttpContext, AWSXRayRecorder> for this kind of configuration (unless there is a better approach).

app.UseXRay("MY_COMPONENT", (_, recorder) => 
{
    recorder.AddAnnotation("Project", "MY_SECRET_PROJECT");
    recorder.AddAnnotation("Environment", HostEnvironment.EnvironmentName);
    recorder.AddAnnotation("Workspace", GetWorkspaceName());
});
@Kralizek Kralizek changed the title How to add annotation for every request? How to add annotations for every request? Dec 3, 2021
@lupengamzn
Copy link
Contributor

Hi @Kralizek,

I believe you can do so by adding an extra middleware specifically for adding annotation in the pipeline.

@Kralizek
Copy link
Author

Hi @lupengamzn

So something like the snippet below?

app​.Use​(next​ ​=>
{
    ​AWSXRayRecorder​.​Instance​.AddAnnotation​("​Project​", ​"​MY_SECRET_PROJECT​");
    ​AWSXRayRecorder​.​Instance​.AddAnnotation​("​Environment​", ​HostEnvironment​.​EnvironmentName​);
    ​AWSXRayRecorder​.​Instance​.AddAnnotation​("​Workspace​", ​GetWorkspaceName​());
    ​return​ ​next​;
});

@arash-bizcover
Copy link

How can we just add annotations into the Incoming requests? any config in the .useXray() we can use ?

@srprash do you know/ have any snippet that works?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants