-
Notifications
You must be signed in to change notification settings - Fork 361
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(storage): single span for
ReadObject()
(#14435)
Keep the span created for `ReadObject()` open until the download completes or is interrupted. This also groups resumed downloads and backoff spans under one umbrella.
- Loading branch information
Showing
8 changed files
with
411 additions
and
2 deletions.
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 | ||
|
||
/** | ||
* | ||
*/ | ||
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.