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

Commit 73969b7

Browse files
authored
Stackdriver stats exporter: make opencensus_task optional. (#364)
Have the exporter fill it in by default, and move the code out of examples/ and into opencensus/common/internal/.
1 parent 36e10cb commit 73969b7

9 files changed

Lines changed: 175 additions & 31 deletions

File tree

examples/grpc/stackdriver.cc

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,54 +12,31 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
#include <limits.h>
16-
#include <unistd.h>
17-
#include <cerrno>
1815
#include <cstdlib>
19-
#include <cstring>
2016
#include <iostream>
2117

22-
#include "absl/strings/str_cat.h"
2318
#include "examples/grpc/stackdriver.h"
2419
#include "opencensus/exporters/stats/stackdriver/stackdriver_exporter.h"
2520
#include "opencensus/exporters/trace/stackdriver/stackdriver_exporter.h"
2621

27-
// OS X defines _POSIX_HOST_NAME_MAX instead.
28-
#ifndef HOST_NAME_MAX
29-
#ifdef _POSIX_HOST_NAME_MAX
30-
#define HOST_NAME_MAX _POSIX_HOST_NAME_MAX
31-
#else
32-
#define HOST_NAME_MAX 255 // SUSv2 says 255 is the limit.
33-
#endif
34-
#endif
35-
3622
void RegisterStackdriverExporters() {
3723
const char *project_id = getenv("STACKDRIVER_PROJECT_ID");
3824
if (project_id == nullptr) {
3925
std::cerr << "The STACKDRIVER_PROJECT_ID environment variable is not set: "
4026
"not exporting to Stackdriver.\n";
4127
return;
4228
}
43-
char hostname[HOST_NAME_MAX + 1];
44-
if (gethostname(hostname, sizeof(hostname)) == -1) {
45-
std::cerr << "gethostname() failed: " << strerror(errno) << "\n";
46-
strncpy(hostname, "hostname", sizeof(hostname) - 1);
47-
hostname[sizeof(hostname) - 1] = 0;
48-
}
29+
30+
std::cout << "RegisterStackdriverExporters:\n";
31+
std::cout << " project_id = \"" << project_id << "\"\n";
4932

5033
opencensus::exporters::stats::StackdriverOptions stats_opts;
5134
stats_opts.project_id = project_id;
52-
stats_opts.opencensus_task = absl::StrCat("cpp-", getpid(), "@", hostname);
53-
54-
std::cout << "RegisterStackdriverExporters:\n";
55-
std::cout << " project_id = \"" << stats_opts.project_id << "\"\n";
56-
std::cout << " opencensus_task = \"" << stats_opts.opencensus_task << "\"\n";
35+
opencensus::exporters::stats::StackdriverExporter::Register(
36+
std::move(stats_opts));
5737

5838
opencensus::exporters::trace::StackdriverOptions trace_opts;
5939
trace_opts.project_id = project_id;
60-
61-
opencensus::exporters::stats::StackdriverExporter::Register(
62-
std::move(stats_opts));
6340
opencensus::exporters::trace::StackdriverExporter::Register(
6441
std::move(trace_opts));
6542
}

opencensus/common/internal/BUILD

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ cc_library(
2626
copts = DEFAULT_COPTS,
2727
)
2828

29+
cc_library(
30+
name = "hostname",
31+
srcs = ["hostname.cc"],
32+
hdrs = ["hostname.h"],
33+
copts = DEFAULT_COPTS,
34+
deps = ["@com_google_absl//absl/strings"],
35+
)
36+
2937
cc_library(
3038
name = "random_lib",
3139
srcs = ["random.cc"],
@@ -60,6 +68,16 @@ cc_library(
6068
# Tests
6169
# ========================================================================= #
6270

71+
cc_test(
72+
name = "hostname_test",
73+
srcs = ["hostname_test.cc"],
74+
copts = TEST_COPTS,
75+
deps = [
76+
":hostname",
77+
"@com_google_googletest//:gtest_main",
78+
],
79+
)
80+
6381
cc_test(
6482
name = "random_test",
6583
srcs = ["random_test.cc"],

opencensus/common/internal/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@
1414

1515
opencensus_lib(common_hash_mix)
1616

17+
opencensus_lib(common_hostname
18+
SRCS
19+
hostname.cc
20+
DEPS
21+
absl::strings)
22+
1723
opencensus_lib(common_random
1824
SRCS
1925
random.cc
@@ -30,6 +36,8 @@ target_compile_definitions(opencensus_common_stats_object INTERFACE
3036

3137
opencensus_lib(common_string_vector_hash)
3238

39+
opencensus_test(common_hostname_test hostname_test.cc common_hostname)
40+
3341
opencensus_test(common_random_test random_test.cc common_random)
3442

3543
opencensus_benchmark(common_random_benchmark random_benchmark.cc common_random)
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright 2019, 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/hostname.h"
16+
17+
#if defined(_MSC_VER)
18+
#define WIN32_LEAN_AND_MEAN
19+
#include <process.h>
20+
#include <windows.h>
21+
#include <winsock2.h>
22+
#pragma comment(lib, "ws2_32.lib")
23+
#else
24+
#include <unistd.h>
25+
#endif
26+
27+
#include "absl/strings/str_cat.h"
28+
29+
namespace opencensus {
30+
namespace common {
31+
namespace {
32+
constexpr char kUnknownHostname[] = "unknown_hostname";
33+
} // namespace
34+
35+
#if defined(_MSC_VER)
36+
std::string Hostname() {
37+
WSADATA data;
38+
if (WSAStartup(MAKEWORD(2, 2), &data) != 0) {
39+
return kUnknownHostname;
40+
}
41+
// SUSv2 says 255 is the limit for hostnames.
42+
char buf[256];
43+
if (gethostname(buf, sizeof(buf)) != 0) {
44+
return kUnknownHostname;
45+
}
46+
// NUL terminate just in case.
47+
buf[sizeof(buf) - 1] = 0;
48+
WSACleanup();
49+
return buf;
50+
}
51+
#else
52+
std::string Hostname() {
53+
// SUSv2 says 255 is the limit for hostnames.
54+
char buf[256];
55+
if (gethostname(buf, sizeof(buf)) == -1) {
56+
return "unknown_hostname";
57+
}
58+
// gethostname() doesn't guarantee NUL termination.
59+
buf[sizeof(buf) - 1] = 0;
60+
return buf;
61+
}
62+
#endif
63+
64+
std::string OpenCensusTask() {
65+
return absl::StrCat("cpp-", getpid(), "@", Hostname());
66+
}
67+
68+
} // namespace common
69+
} // namespace opencensus
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2019, 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_HOST_H_
16+
#define OPENCENSUS_COMMON_INTERNAL_HOST_H_
17+
18+
#include <string>
19+
20+
namespace opencensus {
21+
namespace common {
22+
23+
// Returns the current hostname or "unknown_hostname" on error.
24+
std::string Hostname();
25+
26+
// Returns a default opencensus_task value for stats exporters, in
27+
// the format "cpp-{PID}@{HOSTNAME}"
28+
std::string OpenCensusTask();
29+
30+
} // namespace common
31+
} // namespace opencensus
32+
33+
#endif // OPENCENSUS_COMMON_INTERNAL_HOST_H_
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2019, 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/hostname.h"
16+
#include "gmock/gmock.h"
17+
#include "gtest/gtest.h"
18+
19+
#include <string>
20+
21+
namespace opencensus {
22+
namespace common {
23+
namespace {
24+
25+
TEST(HostnameTest, OpenCensusTaskSmokeTest) {
26+
const std::string task = OpenCensusTask();
27+
std::cout << "opencensus_task = \"" << task << "\"\n";
28+
EXPECT_THAT(task, ::testing::StartsWith("cpp-"));
29+
}
30+
31+
} // namespace
32+
} // namespace common
33+
} // namespace opencensus

opencensus/exporters/stats/stackdriver/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ cc_library(
2626
visibility = ["//visibility:public"],
2727
deps = [
2828
":stackdriver_utils",
29+
"//opencensus/common/internal:hostname",
2930
"//opencensus/common/internal/grpc:status",
3031
"//opencensus/common/internal/grpc:with_user_agent",
3132
"//opencensus/stats",

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "google/protobuf/empty.pb.h"
3131
#include "opencensus/common/internal/grpc/status.h"
3232
#include "opencensus/common/internal/grpc/with_user_agent.h"
33+
#include "opencensus/common/internal/hostname.h"
3334
#include "opencensus/exporters/stats/stackdriver/internal/stackdriver_utils.h"
3435
#include "opencensus/stats/stats.h"
3536

@@ -75,6 +76,10 @@ StackdriverOptions SetOptionDefaults(StackdriverOptions&& o) {
7576
// Prepend prefix because we always use this with a prefix.
7677
opts.project_id = absl::StrCat(kProjectIdPrefix, opts.project_id);
7778

79+
if (opts.opencensus_task.empty()) {
80+
opts.opencensus_task = opencensus::common::OpenCensusTask();
81+
}
82+
7883
if (opts.metric_name_prefix.empty()) {
7984
opts.metric_name_prefix = kDefaultMetricNamePrefix;
8085
}

opencensus/exporters/stats/stackdriver/stackdriver_exporter.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ struct StackdriverOptions {
3232
// The Stackdriver project ID to use.
3333
std::string project_id;
3434

35-
// The opencensus_task is used to uniquely identify the task in Stackdriver.
36-
// The recommended format is "{LANGUAGE}-{PID}@{HOSTNAME}". If PID is not
37-
// available, a random number may be used.
35+
// Optional: The opencensus_task is used to uniquely identify the task in
36+
// Stackdriver. If empty, the exporter will use "cpp-{PID}@{HOSTNAME}" by
37+
// default.
3838
std::string opencensus_task;
3939

4040
// The RPC deadline to use when exporting to Stackdriver.

0 commit comments

Comments
 (0)