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

Detect service attributes for GCE instances. #297

Open
Tasty213 opened this issue Nov 3, 2023 · 1 comment
Open

Detect service attributes for GCE instances. #297

Tasty213 opened this issue Nov 3, 2023 · 1 comment
Labels
priority: p3 question Further information is requested

Comments

@Tasty213
Copy link
Contributor

Tasty213 commented Nov 3, 2023

The OpenTelemetry semantic conventions defines a number of attributes that be logical to set as resource attributes for GCE compute instances. Currently using these on an internal project but i appreciate their applicability might just be due to how our GCP setup works.

  • service.instance.id The string ID of the service instance, this is the four characters at the end of a GCE VM Instance.
  • service.namespace A string value having a meaning that helps to distinguish a group of services, in our case this is the project the VM is in.
  • service.name Must be the same for all instances of horizontally scaled services, we've set this to the instance group name
  • service.version The version string of the service API or implementation, and this is the instance template name each increment of which is a new version of the service.

This means that the combination of service namespace, service name and instance id is a globally unique identifier for that VM.

Service attributes spec

Quick demo of how these values could be acquired there's probably a better way to do this.

class ServiceResourceDetector(GoogleCloudResourceDetector):
    def detect(self) -> Resource:
        try:
            resources = super().detect()
        except NoGoogleResourcesFound:
            resources = Resource.create({})

        custom_resources = Resource.create(
            {
                ResourceAttributes.SERVICE_INSTANCE_ID: self.get_instance_id(),
                ResourceAttributes.SERVICE_NAME: self.get_service_name(),
                ResourceAttributes.SERVICE_NAMESPACE: self.get_project_id(),
            }
        )
        return resources.merge(custom_resources)

    @staticmethod
    def get_instance_id():
        return ServiceResourceDetector._get_host_name()[-4:]

    @staticmethod
    def get_service_name():
        return ServiceResourceDetector._get_host_name()[:-5]

    @staticmethod
    def _get_host_name():
        return socket.gethostname()

Essentially is GCE instance group setups widely standard enough that we can reliably use instance group/template/project to define the attributes?

@aabmass aabmass added question Further information is requested priority: p3 labels Nov 20, 2023
@aabmass
Copy link
Collaborator

aabmass commented Jan 5, 2024

Sorry for the slow reply here

Essentially is GCE instance group setups widely standard enough that we can reliably use instance group/template/project to define the attributes?

If each VM is only running one process reporting telemetry, this could be a reasonable approach. I think it's pretty common for other users to have multiple applications running in one VM, which would cause conflicts. Ofc it also depends on you naming your VMs in such a way that service.name means something to your developers. I'm not sure if this would be widely applicable to other users.

Hope that answers your question.

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

No branches or pull requests

2 participants