microsoft / ApplicationInsights-Kubernetes

Enrich the telemetry data for .NET applications running inside containers that are managed by Kubernetes.
Other
138 stars 54 forks source link

Investigation: Using a label to locate the current container #346

Closed xiaomi7732 closed 1 year ago

xiaomi7732 commented 1 year ago

Kudos to @TTmaister. See details: https://github.com/microsoft/ApplicationInsights-Kubernetes/issues/344#issuecomment-1484895959.

Container name looks like a good candidate for identifying the running container - considering it is more unreliable to get a container id from within the container.

The challenge is how to generalize the implementation for everyone to provide container name in the environment variable, and it worth investigating.

The example given in #344 will not always work - the container name happened to be the app label, but it can't be generalized.

Yaml anchor might be something worth looking into: https://stackoverflow.com/questions/46418441/is-it-possible-to-get-the-container-name-from-within-the-container-in-kubernetes

Also, it is possible to access all environment variables by querying the pods.

All those approaches require a level of user configuration.

Is there better way to do it?

xiaomi7732 commented 1 year ago

Looked into various options and I think the one proposed by @TTmaister in #344 is a pretty good solution. As long as the environment variable of ContainerName is set to the correct value (and I believe that is not a high bar), it should work well.

Also pointed out in #344, when containerName is missing, cloud_RoleName will be missing too in Application Insights. And depends on the way telemetries are organized, it can be important. That's a good motivation to address this issue.