Skip to content

Explicitly set OTel Service Instance ID#700

Merged
mhutchinson merged 2 commits intotransparency-dev:mainfrom
mhutchinson:b699-hostname
Feb 16, 2026
Merged

Explicitly set OTel Service Instance ID#700
mhutchinson merged 2 commits intotransparency-dev:mainfrom
mhutchinson:b699-hostname

Conversation

@mhutchinson
Copy link
Contributor

This uses os.Hostname, falling back on a UUID if for some reason it can't be looked up. This should work on VMs and CloudRun instances.

Fixes #699.

This uses os.Hostname, falling back on a UUID if for some reason it can't be looked up. This should work on VMs and CloudRun instances.

Fixes transparency-dev#699.
@mhutchinson mhutchinson requested a review from a team as a code owner February 12, 2026 16:42
@mhutchinson mhutchinson requested review from AlCutter and removed request for a team February 12, 2026 16:42
@mhutchinson mhutchinson requested a review from phbnf February 12, 2026 16:42
}
}

instanceID, err := os.Hostname()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this returns the instanceName, right? In which case s/instanceID/instanceName.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is intended to be used as an Instance ID though when we set it in OTel. As in, we ultimately call semconv.ServiceInstanceIDKey.String(instanceID).

}
resources, err := resource.New(ctx,
resource.WithTelemetrySDK(),
resource.WithFromEnv(), // unpacks OTEL_RESOURCE_ATTRIBUTES
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What takes precedence, WithFromEnv or resource.WithAttributes? Today, we can set the InstanceID with https://opentelemetry.io/docs/specs/otel/configuration/sdk-environment-variables/, and we tell folks to do so in the codelab. Will it still work after this PR?

Copy link
Collaborator

@phbnf phbnf Feb 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mhutchinson I think your commit and my PR crossed each other: I like your initial implementation without the 8 random characters better. If we can, let's reconcile this with a solution that ensures folks going through the codelab don't face log entries with errors and broken monitoring straight away?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Backed this out, and swapped the order of initializing the environment variables and code configured values. This addresses your comment, and ultimately gives the user more flexibility and control.

Whichever is last wins. Previously we used the env variables as the base, and then the binary would replace them. This way around is more user-first: if the user is strongly opinionated that in their telemetry they want, e.g. service name to be something different, they can do.
@mhutchinson mhutchinson merged commit d0c58c3 into transparency-dev:main Feb 16, 2026
10 checks passed
@mhutchinson mhutchinson deleted the b699-hostname branch February 16, 2026 10:41
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

Successfully merging this pull request may close these issues.

GCP OTel should export a service instance ID

2 participants