-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
chore(storage): Instrument existing metadata ops with storage trace package #11107
Open
cojenco
wants to merge
15
commits into
googleapis:main
Choose a base branch
from
cojenco:trace-existing-metadata
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.
+388
−44
Open
Changes from 14 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
f88cc9b
chore: introduce storage trace package
cojenco 0b2055c
add tests and review comments
cojenco f74dae6
move tests and bucket instrumentation to separate PR
cojenco 2223483
Apply suggestions from code review
cojenco a29def8
update docstring comments
cojenco 7430778
update tests address comments
cojenco 720ab16
move development flag to instrumentation PR
cojenco 86a94cd
chore: instrument existing metadata ops with storage trace pkg
cojenco 8db3176
remove http client trace for consistency
cojenco fee7fb3
add trace tests
cojenco 1f03c77
merge changes
cojenco be1594b
rename emulated tests
cojenco b400455
move dev check into trace functions
cojenco cd7e94c
fix vet
cojenco 53726f1
refactor tests and review comments
cojenco 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 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 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 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 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 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 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 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 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,87 @@ | ||
// Copyright 2024 Google LLC | ||
// | ||
// 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 storage | ||
|
||
import ( | ||
"context" | ||
"os" | ||
|
||
internalTrace "cloud.google.com/go/internal/trace" | ||
"cloud.google.com/go/storage/internal" | ||
"go.opentelemetry.io/otel" | ||
"go.opentelemetry.io/otel/attribute" | ||
otelcodes "go.opentelemetry.io/otel/codes" | ||
"go.opentelemetry.io/otel/trace" | ||
) | ||
|
||
const ( | ||
storageOtelTracingDevVar = "GO_STORAGE_DEV_OTEL_TRACING" | ||
defaultTracerName = "cloud.google.com/go/storage" | ||
gcpClientRepo = "googleapis/google-cloud-go" | ||
gcpClientArtifact = "cloud.google.com/go/storage" | ||
) | ||
|
||
// isOTelTracingDevEnabled checks the development flag until experimental feature is launched. | ||
// TODO: Remove development flag upon experimental launch. | ||
func isOTelTracingDevEnabled() bool { | ||
return os.Getenv(storageOtelTracingDevVar) == "true" | ||
} | ||
|
||
func tracer() trace.Tracer { | ||
return otel.Tracer(defaultTracerName, trace.WithInstrumentationVersion(internal.Version)) | ||
} | ||
|
||
// startSpan creates a span and a context.Context containing the newly-created span. | ||
// If the context.Context provided in `ctx` contains a span then the newly-created | ||
// span will be a child of that span, otherwise it will be a root span. | ||
func startSpan(ctx context.Context, name string, opts ...trace.SpanStartOption) (context.Context, trace.Span) { | ||
// TODO: Remove internalTrace upon experimental launch. | ||
if !isOTelTracingDevEnabled() { | ||
ctx = internalTrace.StartSpan(ctx, name) | ||
return ctx, nil | ||
} | ||
opts = append(opts, getCommonTraceOptions()...) | ||
ctx, span := tracer().Start(ctx, name, opts...) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just to be clear, we are leaving out I'd suggest at least adding it back to the internalTrace implementation. |
||
return ctx, span | ||
} | ||
|
||
// endSpan retrieves the current span from ctx and completes the span. | ||
// If an error occurs, the error is recorded as an exception span event for this span, | ||
// and the span status is set in the form of a code and a description. | ||
func endSpan(ctx context.Context, err error) { | ||
// TODO: Remove internalTrace upon experimental launch. | ||
if !isOTelTracingDevEnabled() { | ||
internalTrace.EndSpan(ctx, err) | ||
} else { | ||
span := trace.SpanFromContext(ctx) | ||
if err != nil { | ||
span.SetStatus(otelcodes.Error, err.Error()) | ||
span.RecordError(err) | ||
} | ||
span.End() | ||
} | ||
} | ||
|
||
// getCommonTraceOptions includes the common attributes used for Cloud Trace adoption tracking. | ||
func getCommonTraceOptions() []trace.SpanStartOption { | ||
opts := []trace.SpanStartOption{ | ||
trace.WithAttributes( | ||
attribute.String("gcp.client.version", internal.Version), | ||
attribute.String("gcp.client.repo", gcpClientRepo), | ||
attribute.String("gcp.client.artifact", gcpClientArtifact), | ||
), | ||
} | ||
return opts | ||
} |
Oops, something went wrong.
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.
So it looks like we're dropping the actual returned Span. I assume we will use this later if we want to start adding more context pieces?
Also, does it make sense to have a separate
endSpan
method vs. callingSpan.End
directly on the span that was returned fromstartSpan
? I recognize that you are basically just replicating the internal package pattern currently...