Repository navigation
Make OTel instanceID work with Cloud Run too #986
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
Open
AlCutter
wants to merge
2
commits into
transparency-dev:main
Choose a base branch
from
AlCutter:otel_instance_id
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| // Copyright 2026 The Tessera authors. All Rights Reserved. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "testing" | ||
|
|
||
| "github.com/google/uuid" | ||
| "go.opentelemetry.io/otel/attribute" | ||
| "go.opentelemetry.io/otel/sdk/resource" | ||
| semconv "go.opentelemetry.io/otel/semconv/v1.4.0" | ||
| ) | ||
|
|
||
| type fakeDetector struct { | ||
| attrs []attribute.KeyValue | ||
| } | ||
|
|
||
| func (f fakeDetector) Detect(context.Context) (*resource.Resource, error) { | ||
| return resource.NewSchemaless(f.attrs...), nil | ||
| } | ||
|
|
||
| func TestDetectResources(t *testing.T) { | ||
| for _, test := range []struct { | ||
| desc string | ||
| detectorAttrs []attribute.KeyValue | ||
| hostname func() (string, error) | ||
| wantNoServiceInstance bool | ||
| wantServiceInstance string | ||
| wantUUIDInstance bool | ||
| }{ | ||
| { | ||
| desc: "Cloud Run supplies faas.instance, service.instance.id not set", | ||
| detectorAttrs: []attribute.KeyValue{ | ||
| semconv.FaaSInstanceKey.String("00bf4bf02da24966c43a39d0c6d3bd884d3b81c4"), | ||
| }, | ||
| hostname: func() (string, error) { return "localhost", nil }, | ||
| wantNoServiceInstance: true, | ||
| }, | ||
| { | ||
| desc: "GCE supplies host.id, service.instance.id not set", | ||
| detectorAttrs: []attribute.KeyValue{ | ||
| semconv.HostIDKey.String("1234567890123456789"), | ||
| }, | ||
| hostname: func() (string, error) { return "gce-vm-1", nil }, | ||
| wantNoServiceInstance: true, | ||
| }, | ||
| { | ||
| desc: "fallback to non-localhost hostname when detector supplies no instance ID", | ||
| hostname: func() (string, error) { return "workstation-01", nil }, | ||
| wantServiceInstance: "workstation-01", | ||
| }, | ||
| { | ||
| desc: "fallback to UUID when hostname is localhost", | ||
| hostname: func() (string, error) { return "localhost", nil }, | ||
| wantUUIDInstance: true, | ||
| }, | ||
| { | ||
| desc: "fallback to UUID when os.Hostname fails", | ||
| hostname: func() (string, error) { return "", errors.New("hostname error") }, | ||
| wantUUIDInstance: true, | ||
| }, | ||
| } { | ||
| t.Run(test.desc, func(t *testing.T) { | ||
| t.Setenv("OTEL_RESOURCE_ATTRIBUTES", "") | ||
| res, err := detectResources(t.Context(), "example.com/log", "my-project", test.hostname, fakeDetector{attrs: test.detectorAttrs}) | ||
| if err != nil { | ||
| t.Fatalf("detectResources() unexpected error: %v", err) | ||
| } | ||
|
|
||
| s := res.Set() | ||
| val, hasServiceInstance := s.Value(semconv.ServiceInstanceIDKey) | ||
| if test.wantNoServiceInstance { | ||
| if hasServiceInstance { | ||
| t.Errorf("service.instance.id = %q, want unset", val.AsString()) | ||
| } | ||
| return | ||
| } | ||
|
|
||
| if !hasServiceInstance { | ||
| t.Fatalf("service.instance.id not set on resource: %v", res) | ||
| } | ||
| got := val.AsString() | ||
| if test.wantUUIDInstance { | ||
| if _, err := uuid.Parse(got); err != nil { | ||
| t.Errorf("service.instance.id = %q, want valid UUID: %v", got, err) | ||
| } | ||
| } else if got != test.wantServiceInstance { | ||
| t.Errorf("service.instance.id = %q, want %q", got, test.wantServiceInstance) | ||
| } | ||
| }) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
InstanceIDto 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?There was a problem hiding this comment.
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#ServiceInstanceIDKeySo 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).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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:
ServiceInstanceIDKeyis the default field, which can be set with an environment variable?FaaSInstanceKeyis for CloudRunHostIDKeyis for GCE(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.