-
Notifications
You must be signed in to change notification settings - Fork 378
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
feat(storage): single span for ReadObject()
#14435
Merged
coryan
merged 1 commit into
googleapis:main
from
coryan:feat-storage-single-span-for-resumed-downloads
Jul 8, 2024
Merged
Changes from all commits
Commits
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
83 changes: 83 additions & 0 deletions
83
google/cloud/storage/internal/tracing_object_read_source.cc
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,83 @@ | ||
// 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 | ||
// | ||
// https://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. | ||
|
||
#ifdef GOOGLE_CLOUD_CPP_HAVE_OPENTELEMETRY | ||
|
||
#include "google/cloud/storage/internal/tracing_object_read_source.h" | ||
#include "google/cloud/internal/opentelemetry.h" | ||
#include "google/cloud/status.h" | ||
#include <opentelemetry/trace/scope.h> | ||
#include <chrono> | ||
#include <utility> | ||
|
||
namespace google { | ||
namespace cloud { | ||
namespace storage_internal { | ||
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN | ||
|
||
TracingObjectReadSource::TracingObjectReadSource( | ||
opentelemetry::nostd::shared_ptr<opentelemetry::trace::Span> span, | ||
std::unique_ptr<storage::internal::ObjectReadSource> child) | ||
: span_(std::move(span)), child_(std::move(child)) {} | ||
|
||
TracingObjectReadSource::~TracingObjectReadSource() { | ||
// A download may be abandoned by the application, or kept open after all | ||
// the data is received. Note that if there was an unrecoverable error the | ||
// span is already finished in `TracingObjectReadOSource::Read()`. | ||
internal::EndSpan(*span_); | ||
} | ||
|
||
bool TracingObjectReadSource::IsOpen() const { | ||
auto scope = opentelemetry::trace::Scope(span_); | ||
return child_->IsOpen(); | ||
} | ||
|
||
StatusOr<storage::internal::HttpResponse> TracingObjectReadSource::Close() { | ||
auto scope = opentelemetry::trace::Scope(span_); | ||
span_->AddEvent("gl-cpp.close"); | ||
return child_->Close(); | ||
} | ||
|
||
StatusOr<storage::internal::ReadSourceResult> TracingObjectReadSource::Read( | ||
char* buf, std::size_t n) { | ||
auto scope = opentelemetry::trace::Scope(span_); | ||
auto const start = std::chrono::system_clock::now(); | ||
auto response = child_->Read(buf, n); | ||
auto const latency = std::chrono::duration_cast<std::chrono::microseconds>( | ||
std::chrono::system_clock::now() - start) | ||
.count(); | ||
|
||
if (!response) { | ||
auto const code = StatusCodeToString(response.status().code()); | ||
span_->AddEvent("gl-cpp.read", | ||
opentelemetry::common::SystemTimestamp(start), | ||
{{"read.status.code", code}, | ||
{"read.buffer.size", static_cast<std::int64_t>(n)}, | ||
{"read.latency.us", static_cast<std::int64_t>(latency)}}); | ||
return google::cloud::internal::EndSpan(*span_, std::move(response)); | ||
} | ||
span_->AddEvent("gl-cpp.read", opentelemetry::common::SystemTimestamp(start), | ||
{{"read.buffer.size", static_cast<std::int64_t>(n)}, | ||
{"read.returned.size", | ||
static_cast<std::int64_t>(response->bytes_received)}, | ||
{"read.latency.us", static_cast<std::int64_t>(latency)}}); | ||
return response; | ||
} | ||
|
||
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END | ||
} // namespace storage_internal | ||
} // namespace cloud | ||
} // namespace google | ||
|
||
#endif // GOOGLE_CLOUD_CPP_HAVE_OPENTELEMETRY |
57 changes: 57 additions & 0 deletions
57
google/cloud/storage/internal/tracing_object_read_source.h
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,57 @@ | ||
// 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 | ||
// | ||
// https://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. | ||
|
||
#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_INTERNAL_TRACING_OBJECT_READ_SOURCE_H | ||
#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_INTERNAL_TRACING_OBJECT_READ_SOURCE_H | ||
|
||
#ifdef GOOGLE_CLOUD_CPP_HAVE_OPENTELEMETRY | ||
|
||
#include "google/cloud/storage/internal/object_read_source.h" | ||
#include "google/cloud/storage/version.h" | ||
#include "google/cloud/internal/opentelemetry.h" | ||
#include <memory> | ||
|
||
namespace google { | ||
namespace cloud { | ||
namespace storage_internal { | ||
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN | ||
|
||
/** | ||
* | ||
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. ? |
||
*/ | ||
class TracingObjectReadSource : public storage::internal::ObjectReadSource { | ||
public: | ||
TracingObjectReadSource( | ||
opentelemetry::nostd::shared_ptr<opentelemetry::trace::Span> span, | ||
std::unique_ptr<storage::internal::ObjectReadSource> child); | ||
~TracingObjectReadSource() override; | ||
|
||
bool IsOpen() const override; | ||
StatusOr<storage::internal::HttpResponse> Close() override; | ||
StatusOr<storage::internal::ReadSourceResult> Read(char* buf, | ||
std::size_t n) override; | ||
|
||
private: | ||
opentelemetry::nostd::shared_ptr<opentelemetry::trace::Span> span_; | ||
std::unique_ptr<storage::internal::ObjectReadSource> child_; | ||
}; | ||
|
||
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END | ||
} // namespace storage_internal | ||
} // namespace cloud | ||
} // namespace google | ||
|
||
#endif // GOOGLE_CLOUD_CPP_HAVE_OPENTELEMETRY | ||
|
||
#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_INTERNAL_TRACING_OBJECT_READ_SOURCE_H |
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.
comment: this is probably good practice in case we add any instrumentation to lower layers. But just an FYI: that creating a scope does a heap allocation, so it is relatively expensive.
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 true even of Noop spans?
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.
Yes. They do not check whether it is a valid span before pushing to the context stack (and creating a token to track it).
https://github.com/open-telemetry/opentelemetry-cpp/blob/6dbfdb52ebca82d8705392fe88dadd9c133374e4/api/include/opentelemetry/trace/scope.h#L32-L35