Skip to content

Make OTel instanceID work with Cloud Run too - #986

Open
AlCutter wants to merge 2 commits into
transparency-dev:mainfrom
AlCutter:otel_instance_id
Open

AlCutter wants to merge 2 commits into
transparency-dev:mainfrom
AlCutter:otel_instance_id

Conversation

@AlCutter

@AlCutter AlCutter commented Nov 9, 2026

Copy link
Copy Markdown
Collaborator

This PR improves the exporting of OTel metrics when running under Cloud Run.

Previously, Cloud Run v2 would always show an instance ID of localhost.

@AlCutter
AlCutter requested a review from phbnf November 9, 2026 17:26
@AlCutter
AlCutter requested a review from a team as a code owner November 9, 2026 17:26
Comment thread cmd/tesseract/gcp/otel.go Outdated
if allAttrsEmpty(resources.Set(), semconv.ServiceInstanceIDKey, semconv.FaaSInstanceKey, semconv.HostIDKey) {
instanceID, err := hostname()
if err != nil {
slog.ErrorContext(ctx, "os.Hostname() failed, setting OTel service instance ID to UUID", slog.Any("error", err))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nit: hostname()

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Good spot!

Comment thread cmd/tesseract/gcp/otel.go
return nil, err
}
// Set an instance ID if the detectors and env extractor above didn't come up with one.
if allAttrsEmpty(resources.Set(), semconv.ServiceInstanceIDKey, semconv.FaaSInstanceKey, semconv.HostIDKey) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Is it possible for an InstanceID to be set without any of these resources being set? This looks a bit brittle at first sight and seems to be CloudRun specific, so maybe it calls for a comment and could be extracted in its own function?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I don't think so, no: the first of these (semconv.ServiceInstanceIDKey) is the key which carries the service instance ID: https://pkg.go.dev/go.opentelemetry.io/otel/semconv/v1.20.0#ServiceInstanceIDKey

So this condition is only true if the detectors/env didn't come up with any sort of local instance identifier (i.e an explicit instance ID, a Cloud Run style instance ID, or a host ID key).

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

the first of these (semconv.ServiceInstanceIDKey) is the key which carries the service instance ID

Is that the case for CloudRun? My understanding chatting with Gemini is that it's in FaaSInstanceKey (but I might be wrong).

Also if OpenTelemetry changed how this works, this code might now work as intended anymore. Otel could for instance remove one of these keys, or introduce a new one.

I'm not sure we can write better code, but maybe we could add comments to explain that:

  • ServiceInstanceIDKey is the default field, which can be set with an environment variable?
  • FaaSInstanceKey is for CloudRun
  • HostIDKey is for GCE
  • GKE and more are not supported

(That's the conclusion that I came to chatting with Gemini about this.)

Ideally, there would be a way to fetch these keys from Otel / to check whether the Instance ID has been set... but I'm not sure whether we can do that.

Approving since the priority is to unblock CloudRun, and this PR does that. We can polish later.

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.

2 participants