Skip to content
This repository was archived by the owner on Jul 31, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions opencensus/common/internal/grpc/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# OpenCensus C++ internal libraries.
#
# Copyright 2018, OpenCensus Authors
#
# 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.

load("//opencensus:copts.bzl", "DEFAULT_COPTS", "TEST_COPTS")

licenses(["notice"]) # Apache 2.0

package(default_visibility = ["//opencensus:__subpackages__"])

cc_library(
name = "status",
srcs = ["status.cc"],
hdrs = ["status.h"],
copts = DEFAULT_COPTS,
deps = [
"@com_github_grpc_grpc//:grpc++",
"@com_google_absl//absl/strings",
],
)

# Tests
# ========================================================================= #

cc_test(
name = "status_test",
srcs = ["status_test.cc"],
copts = TEST_COPTS,
deps = [
":status",
"@com_google_googletest//:gtest_main",
],
)
80 changes: 80 additions & 0 deletions opencensus/common/internal/grpc/status.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright 2018, OpenCensus Authors
//
// 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.

#include "opencensus/common/internal/grpc/status.h"

#include <string>

#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "include/grpc++/support/status.h"

namespace opencensus {
namespace common {

namespace {

absl::string_view StatusCodeName(grpc::StatusCode code) {
switch (code) {
case grpc::StatusCode::OK:
return "OK";
case grpc::StatusCode::CANCELLED:
return "CANCELLED";
case grpc::StatusCode::UNKNOWN:
return "UNKNOWN";
case grpc::StatusCode::INVALID_ARGUMENT:
return "INVALID_ARGUMENT";
case grpc::StatusCode::DEADLINE_EXCEEDED:
return "DEADLINE_EXCEEDED";
case grpc::StatusCode::NOT_FOUND:
return "NOT_FOUND";
case grpc::StatusCode::ALREADY_EXISTS:
return "ALREADY_EXISTS";
case grpc::StatusCode::PERMISSION_DENIED:
return "PERMISSION_DENIED";
case grpc::StatusCode::RESOURCE_EXHAUSTED:
return "RESOURCE_EXHAUSTED";
case grpc::StatusCode::FAILED_PRECONDITION:
return "FAILED_PRECONDITION";
case grpc::StatusCode::ABORTED:
return "ABORTED";
case grpc::StatusCode::OUT_OF_RANGE:
return "OUT_OF_RANGE";
case grpc::StatusCode::UNIMPLEMENTED:
return "UNIMPLEMENTED";
case grpc::StatusCode::INTERNAL:
return "INTERNAL";
case grpc::StatusCode::UNAVAILABLE:
return "UNAVAILABLE";
case grpc::StatusCode::DATA_LOSS:
return "DATA_LOSS";
case grpc::StatusCode::UNAUTHENTICATED:
return "UNAUTHENTICATED";
default:
return "invalid status code value";
}
}

} // namespace

std::string ToString(const grpc::Status& status) {
if (status.ok()) {
return "OK";
}
return absl::StrCat(StatusCodeName(status.error_code()), ": ",
status.error_message());
}

} // namespace common
} // namespace opencensus
31 changes: 31 additions & 0 deletions opencensus/common/internal/grpc/status.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2018, OpenCensus Authors
//
// 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.

#ifndef OPENCENSUS_COMMON_INTERNAL_GRPC_STATUS_H_
#define OPENCENSUS_COMMON_INTERNAL_GRPC_STATUS_H_

#include <string>

#include "include/grpc++/support/status.h"

namespace opencensus {
namespace common {

// Returns a string of the status code name and message.
std::string ToString(const grpc::Status& status);

} // namespace common
} // namespace opencensus

#endif // OPENCENSUS_COMMON_INTERNAL_GRPC_STATUS_H_
34 changes: 34 additions & 0 deletions opencensus/common/internal/grpc/status_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2018, OpenCensus Authors
//
// 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.

#include "opencensus/common/internal/grpc/status.h"

#include "gtest/gtest.h"
#include "include/grpc++/support/status.h"

namespace opencensus {
namespace common {
namespace {

TEST(StatusTest, OK) { EXPECT_EQ("OK", ToString(grpc::Status())); }

TEST(StatusTest, NotOK) {
EXPECT_EQ("RESOURCE_EXHAUSTED: You must construct additional pylons.",
ToString(grpc::Status(grpc::StatusCode::RESOURCE_EXHAUSTED,
"You must construct additional pylons.")));
}

} // namespace
} // namespace common
} // namespace opencensus
1 change: 1 addition & 0 deletions opencensus/exporters/stats/stackdriver/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ cc_library(
deps = [
":stackdriver_utils",
"//google/monitoring/v3:metric_service",
"//opencensus/common/internal/grpc:status",
"//opencensus/stats",
"@com_github_grpc_grpc//:grpc++",
"@com_google_absl//absl/strings",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "google/monitoring/v3/metric_service.grpc.pb.h"
#include "google/protobuf/empty.pb.h"
#include "include/grpc++/grpc++.h"
#include "opencensus/common/internal/grpc/status.h"
#include "opencensus/exporters/stats/stackdriver/internal/stackdriver_utils.h"
#include "opencensus/stats/stats.h"

Expand All @@ -34,11 +35,6 @@ constexpr char kProjectIdPrefix[] = "projects/";
// Stackdriver limits a single CreateTimeSeries request to 200 series.
constexpr int kTimeSeriesBatchSize = 200;

std::string ToString(const grpc::Status& status) {
return absl::StrCat("status code ", status.error_code(), " details \"",
status.error_message(), "\"");
}

} // namespace

class StackdriverExporter::Handler
Expand Down Expand Up @@ -109,8 +105,8 @@ void StackdriverExporter::Handler::ExportViewData(
::grpc::Status status =
stub_->CreateTimeSeries(&context, request, &response);
if (!status.ok()) {
std::cerr << "CreateTimeSeries request failed: " << ToString(status)
<< "\n";
std::cerr << "CreateTimeSeries request failed: "
<< opencensus::common::ToString(status) << "\n";
}
}
}
Expand All @@ -137,8 +133,8 @@ bool StackdriverExporter::Handler::MaybeRegisterView(
::grpc::Status status =
stub_->CreateMetricDescriptor(&context, request, &response);
if (!status.ok()) {
std::cerr << "CreateMetricDescriptor request failed: " << ToString(status)
<< "\n";
std::cerr << "CreateMetricDescriptor request failed: "
<< opencensus::common::ToString(status) << "\n";
return false;
}
registered_descriptors_.emplace_hint(it, descriptor.name(), descriptor);
Expand Down
1 change: 1 addition & 0 deletions opencensus/exporters/trace/stackdriver/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ cc_library(
visibility = ["//visibility:public"],
deps = [
"//google/devtools/cloudtrace/v2:tracing_proto",
"//opencensus/common/internal/grpc:status",
"//opencensus/trace",
"@com_github_grpc_grpc//:grpc++",
"@com_google_absl//absl/memory",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,10 @@
#include "absl/time/clock.h"
#include "google/devtools/cloudtrace/v2/tracing.grpc.pb.h"
#include "include/grpc++/grpc++.h"
#include "opencensus/common/internal/grpc/status.h"
#include "opencensus/trace/exporter/span_data.h"
#include "opencensus/trace/exporter/span_exporter.h"

using grpc::ClientContext;
using grpc::Status;

namespace opencensus {
namespace exporters {
namespace trace {
Expand All @@ -42,11 +40,6 @@ constexpr char kGoogleStackdriverTraceAddress[] = "cloudtrace.googleapis.com";
constexpr char kAgentKey[] = "g.co/agent";
constexpr char kAgentValue[] = "opencensus-cpp";

std::string ToString(const grpc::Status& status) {
return absl::StrCat("status code ", status.error_code(), " details \"",
status.error_message(), "\"");
}

gpr_timespec ConvertToTimespec(absl::Time time) {
gpr_timespec g_time;
int64_t secs = absl::ToUnixSeconds(time);
Expand Down Expand Up @@ -272,12 +265,13 @@ void Handler::Export(
request.set_name(absl::StrCat("projects/", project_id_));
ConvertSpans(spans, project_id_, &request);
::google::protobuf::Empty response;
ClientContext context;
grpc::ClientContext context;
context.set_deadline(
ConvertToTimespec(absl::Now() + absl::Milliseconds(3000)));
Status status = stub_->BatchWriteSpans(&context, request, &response);
grpc::Status status = stub_->BatchWriteSpans(&context, request, &response);
if (!status.ok()) {
std::cerr << "BatchWriteSpans failed: " << ToString(status) << "\n";
std::cerr << "BatchWriteSpans failed: "
<< opencensus::common::ToString(status) << "\n";
}
}

Expand Down