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

Adding resource tags dinamically after initialization #1470

Closed
tiagonapoli opened this issue Nov 5, 2020 · 6 comments
Closed

Adding resource tags dinamically after initialization #1470

tiagonapoli opened this issue Nov 5, 2020 · 6 comments
Labels
question Further information is requested

Comments

@tiagonapoli
Copy link

tiagonapoli commented Nov 5, 2020

Question

First of all, congratulations on the awesome work!

On OpenTelemetry initialization we do something similar to the following:

var resource = Resources.CreateServiceResource(conf.ServiceName, serviceVersion: conf.ServiceVersion)
 .Merge(new Resource(new Dictionary<string, object>
 {
    {"deployment.environment", conf.DeploymentEnvironment},
    {"os.type", conf.OsType},
    {"os.description", conf.OsDescription},
    {"cloud.zone", conf.CloudZone},
    {"host.id", conf.HostID},
    {"host.name", conf.HostName},
    {"host.type", conf.HostType},
}));
builder.SetResource(resource);
...

However, to fetch host and cloud information we have to block the initialization process to make calls to AWS API. We would prefer not to block initialization because of this.

Is there a way to add tags to the resource after initialization? Do you guys think of an alternative solution?

@tiagonapoli tiagonapoli added the question Further information is requested label Nov 5, 2020
@eddynaka
Copy link
Contributor

eddynaka commented Nov 5, 2020

@tiagonapoli , depending on what kind of instrumentation you are using, you could Enrich from the options to do something like this:

private static void ActivityEnrichment(Activity activity, string method, object obj)
{
    switch (method)
    {
        case "OnStartActivity":
            var resource = activity.GetResource();
            resource.Merge(new Resources.Resource(new Dictionary<string, object>()));
            activity.SetResource(resource);
            break;

        case "OnStopActivity":
            Assert.True(obj is HttpResponse);
            break;

        default:
            break;
    }
}

@tiagonapoli
Copy link
Author

tiagonapoli commented Nov 5, 2020

Currently we're using AspNetCore and Http auto instrumentations. About the enrich solution, we would have to merge the resource for every activity created, do you think the overhead would be considerable?

Does caching the merged resource would be better?

public static class GlobalResource {
  // this initially is set with some tags
  // once data is fetched from AWS this is replaced with a new resource with all data
  public static Resource Data;
}
private static void ActivityEnrichment(Activity activity, string method, object obj)
{
    switch (method)
    {
        case "OnStartActivity":
            activity.SetResource(GlobalResource.Data);
            break;

        case "OnStopActivity":
            // is this necessary?
            Assert.True(obj is HttpResponse);
            break;

        default:
            break;
    }
}

Also, the enrichment would work for Activities created using a ActivitySource? Should we use a processor for this?

@eddynaka
Copy link
Contributor

eddynaka commented Nov 5, 2020

For the caching strategy, yes, that would help with the allocation since we would only need to allocate the new Resource once.

For the enrichment part, it will affect performance when you use Enrich.

I thought I did commit the benchmark for that. I will create a new PR adding it. But you can look here: #1261 (comment)

For the processor question, yes that should work as well.

@tiagonapoli
Copy link
Author

Awesome! Thanks a lot!

@cijothomas
Copy link
Member

As per the current spec for Resource, resource is associated with a TracerProvider/MeterProvider, and is same for all the traces/metrics from that provider, and cannot be changed after provider creation.
It is a bug that we allow overriding resource on a per span/activity level. This will very likely be fixed with one of the pending PRs. So do not count on this behaviour.

@cijothomas
Copy link
Member

Closing this issue as no pending items here.

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

No branches or pull requests

3 participants