Skip to content
This repository was archived by the owner on Jul 31, 2023. It is now read-only.

Commit 20b6f06

Browse files
authored
Factor out ToString(grpc::Status). (#136)
1 parent 6648c2e commit 20b6f06

8 files changed

Lines changed: 202 additions & 20 deletions

File tree

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# OpenCensus C++ internal libraries.
2+
#
3+
# Copyright 2018, OpenCensus Authors
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
load("//opencensus:copts.bzl", "DEFAULT_COPTS", "TEST_COPTS")
18+
19+
licenses(["notice"]) # Apache 2.0
20+
21+
package(default_visibility = ["//opencensus:__subpackages__"])
22+
23+
cc_library(
24+
name = "status",
25+
srcs = ["status.cc"],
26+
hdrs = ["status.h"],
27+
copts = DEFAULT_COPTS,
28+
deps = [
29+
"@com_github_grpc_grpc//:grpc++",
30+
"@com_google_absl//absl/strings",
31+
],
32+
)
33+
34+
# Tests
35+
# ========================================================================= #
36+
37+
cc_test(
38+
name = "status_test",
39+
srcs = ["status_test.cc"],
40+
copts = TEST_COPTS,
41+
deps = [
42+
":status",
43+
"@com_google_googletest//:gtest_main",
44+
],
45+
)
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// Copyright 2018, OpenCensus Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "opencensus/common/internal/grpc/status.h"
16+
17+
#include <string>
18+
19+
#include "absl/strings/str_cat.h"
20+
#include "absl/strings/string_view.h"
21+
#include "include/grpc++/support/status.h"
22+
23+
namespace opencensus {
24+
namespace common {
25+
26+
namespace {
27+
28+
absl::string_view StatusCodeName(grpc::StatusCode code) {
29+
switch (code) {
30+
case grpc::StatusCode::OK:
31+
return "OK";
32+
case grpc::StatusCode::CANCELLED:
33+
return "CANCELLED";
34+
case grpc::StatusCode::UNKNOWN:
35+
return "UNKNOWN";
36+
case grpc::StatusCode::INVALID_ARGUMENT:
37+
return "INVALID_ARGUMENT";
38+
case grpc::StatusCode::DEADLINE_EXCEEDED:
39+
return "DEADLINE_EXCEEDED";
40+
case grpc::StatusCode::NOT_FOUND:
41+
return "NOT_FOUND";
42+
case grpc::StatusCode::ALREADY_EXISTS:
43+
return "ALREADY_EXISTS";
44+
case grpc::StatusCode::PERMISSION_DENIED:
45+
return "PERMISSION_DENIED";
46+
case grpc::StatusCode::RESOURCE_EXHAUSTED:
47+
return "RESOURCE_EXHAUSTED";
48+
case grpc::StatusCode::FAILED_PRECONDITION:
49+
return "FAILED_PRECONDITION";
50+
case grpc::StatusCode::ABORTED:
51+
return "ABORTED";
52+
case grpc::StatusCode::OUT_OF_RANGE:
53+
return "OUT_OF_RANGE";
54+
case grpc::StatusCode::UNIMPLEMENTED:
55+
return "UNIMPLEMENTED";
56+
case grpc::StatusCode::INTERNAL:
57+
return "INTERNAL";
58+
case grpc::StatusCode::UNAVAILABLE:
59+
return "UNAVAILABLE";
60+
case grpc::StatusCode::DATA_LOSS:
61+
return "DATA_LOSS";
62+
case grpc::StatusCode::UNAUTHENTICATED:
63+
return "UNAUTHENTICATED";
64+
default:
65+
return "invalid status code value";
66+
}
67+
}
68+
69+
} // namespace
70+
71+
std::string ToString(const grpc::Status& status) {
72+
if (status.ok()) {
73+
return "OK";
74+
}
75+
return absl::StrCat(StatusCodeName(status.error_code()), ": ",
76+
status.error_message());
77+
}
78+
79+
} // namespace common
80+
} // namespace opencensus
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2018, OpenCensus Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef OPENCENSUS_COMMON_INTERNAL_GRPC_STATUS_H_
16+
#define OPENCENSUS_COMMON_INTERNAL_GRPC_STATUS_H_
17+
18+
#include <string>
19+
20+
#include "include/grpc++/support/status.h"
21+
22+
namespace opencensus {
23+
namespace common {
24+
25+
// Returns a string of the status code name and message.
26+
std::string ToString(const grpc::Status& status);
27+
28+
} // namespace common
29+
} // namespace opencensus
30+
31+
#endif // OPENCENSUS_COMMON_INTERNAL_GRPC_STATUS_H_
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2018, OpenCensus Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "opencensus/common/internal/grpc/status.h"
16+
17+
#include "gtest/gtest.h"
18+
#include "include/grpc++/support/status.h"
19+
20+
namespace opencensus {
21+
namespace common {
22+
namespace {
23+
24+
TEST(StatusTest, OK) { EXPECT_EQ("OK", ToString(grpc::Status())); }
25+
26+
TEST(StatusTest, NotOK) {
27+
EXPECT_EQ("RESOURCE_EXHAUSTED: You must construct additional pylons.",
28+
ToString(grpc::Status(grpc::StatusCode::RESOURCE_EXHAUSTED,
29+
"You must construct additional pylons.")));
30+
}
31+
32+
} // namespace
33+
} // namespace common
34+
} // namespace opencensus

opencensus/exporters/stats/stackdriver/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ cc_library(
2727
deps = [
2828
":stackdriver_utils",
2929
"//google/monitoring/v3:metric_service",
30+
"//opencensus/common/internal/grpc:status",
3031
"//opencensus/stats",
3132
"@com_github_grpc_grpc//:grpc++",
3233
"@com_google_absl//absl/strings",

opencensus/exporters/stats/stackdriver/internal/stackdriver_exporter.cc

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "google/monitoring/v3/metric_service.grpc.pb.h"
2121
#include "google/protobuf/empty.pb.h"
2222
#include "include/grpc++/grpc++.h"
23+
#include "opencensus/common/internal/grpc/status.h"
2324
#include "opencensus/exporters/stats/stackdriver/internal/stackdriver_utils.h"
2425
#include "opencensus/stats/stats.h"
2526

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

37-
std::string ToString(const grpc::Status& status) {
38-
return absl::StrCat("status code ", status.error_code(), " details \"",
39-
status.error_message(), "\"");
40-
}
41-
4238
} // namespace
4339

4440
class StackdriverExporter::Handler
@@ -109,8 +105,8 @@ void StackdriverExporter::Handler::ExportViewData(
109105
::grpc::Status status =
110106
stub_->CreateTimeSeries(&context, request, &response);
111107
if (!status.ok()) {
112-
std::cerr << "CreateTimeSeries request failed: " << ToString(status)
113-
<< "\n";
108+
std::cerr << "CreateTimeSeries request failed: "
109+
<< opencensus::common::ToString(status) << "\n";
114110
}
115111
}
116112
}
@@ -137,8 +133,8 @@ bool StackdriverExporter::Handler::MaybeRegisterView(
137133
::grpc::Status status =
138134
stub_->CreateMetricDescriptor(&context, request, &response);
139135
if (!status.ok()) {
140-
std::cerr << "CreateMetricDescriptor request failed: " << ToString(status)
141-
<< "\n";
136+
std::cerr << "CreateMetricDescriptor request failed: "
137+
<< opencensus::common::ToString(status) << "\n";
142138
return false;
143139
}
144140
registered_descriptors_.emplace_hint(it, descriptor.name(), descriptor);

opencensus/exporters/trace/stackdriver/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ cc_library(
2929
visibility = ["//visibility:public"],
3030
deps = [
3131
"//google/devtools/cloudtrace/v2:tracing_proto",
32+
"//opencensus/common/internal/grpc:status",
3233
"//opencensus/trace",
3334
"@com_github_grpc_grpc//:grpc++",
3435
"@com_google_absl//absl/memory",

opencensus/exporters/trace/stackdriver/internal/stackdriver_exporter.cc

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,10 @@
2323
#include "absl/time/clock.h"
2424
#include "google/devtools/cloudtrace/v2/tracing.grpc.pb.h"
2525
#include "include/grpc++/grpc++.h"
26+
#include "opencensus/common/internal/grpc/status.h"
2627
#include "opencensus/trace/exporter/span_data.h"
2728
#include "opencensus/trace/exporter/span_exporter.h"
2829

29-
using grpc::ClientContext;
30-
using grpc::Status;
31-
3230
namespace opencensus {
3331
namespace exporters {
3432
namespace trace {
@@ -42,11 +40,6 @@ constexpr char kGoogleStackdriverTraceAddress[] = "cloudtrace.googleapis.com";
4240
constexpr char kAgentKey[] = "g.co/agent";
4341
constexpr char kAgentValue[] = "opencensus-cpp";
4442

45-
std::string ToString(const grpc::Status& status) {
46-
return absl::StrCat("status code ", status.error_code(), " details \"",
47-
status.error_message(), "\"");
48-
}
49-
5043
gpr_timespec ConvertToTimespec(absl::Time time) {
5144
gpr_timespec g_time;
5245
int64_t secs = absl::ToUnixSeconds(time);
@@ -272,12 +265,13 @@ void Handler::Export(
272265
request.set_name(absl::StrCat("projects/", project_id_));
273266
ConvertSpans(spans, project_id_, &request);
274267
::google::protobuf::Empty response;
275-
ClientContext context;
268+
grpc::ClientContext context;
276269
context.set_deadline(
277270
ConvertToTimespec(absl::Now() + absl::Milliseconds(3000)));
278-
Status status = stub_->BatchWriteSpans(&context, request, &response);
271+
grpc::Status status = stub_->BatchWriteSpans(&context, request, &response);
279272
if (!status.ok()) {
280-
std::cerr << "BatchWriteSpans failed: " << ToString(status) << "\n";
273+
std::cerr << "BatchWriteSpans failed: "
274+
<< opencensus::common::ToString(status) << "\n";
281275
}
282276
}
283277

0 commit comments

Comments
 (0)