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

Commit e233b3f

Browse files
authored
examples/grpc: use gethostname() to determine hostname. (#201)
Also report project_id and opencensus_task. This was useful for debugging.
1 parent 42c65bd commit e233b3f

2 files changed

Lines changed: 22 additions & 5 deletions

File tree

examples/grpc/stackdriver.cc

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

15+
#include <limits.h>
1516
#include <unistd.h>
17+
#include <cerrno>
1618
#include <cstdlib>
19+
#include <cstring>
1720
#include <iostream>
1821

1922
#include "absl/strings/str_cat.h"
2023
#include "examples/grpc/stackdriver.h"
2124
#include "opencensus/exporters/stats/stackdriver/stackdriver_exporter.h"
2225
#include "opencensus/exporters/trace/stackdriver/stackdriver_exporter.h"
2326

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+
2436
void RegisterStackdriverExporters() {
2537
const char *project_id = getenv("STACKDRIVER_PROJECT_ID");
2638
if (project_id == nullptr) {
2739
std::cerr << "The STACKDRIVER_PROJECT_ID environment variable is not set: "
2840
"not exporting to Stackdriver.\n";
2941
return;
3042
}
31-
const char *hostname = getenv("HOSTNAME");
32-
if (hostname == nullptr) hostname = "hostname";
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+
}
3349

3450
opencensus::exporters::stats::StackdriverOptions stats_opts;
3551
stats_opts.project_id = project_id;
3652
stats_opts.opencensus_task = absl::StrCat("cpp-", getpid(), "@", hostname);
3753

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";
57+
3858
opencensus::exporters::trace::StackdriverOptions trace_opts;
3959
trace_opts.project_id = project_id;
4060

opencensus/exporters/stats/stackdriver/README.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,6 @@ Add a BUILD dependency on:
5454
In your application's initialization code, register the exporter:
5555

5656
```c++
57-
const char* hostname = getenv("HOSTNAME");
58-
if (hostname == nullptr) hostname = "hostname";
59-
6057
opencensus::exporters::stats::StackdriverOptions opts;
6158
opts.project_id = "my-stackdriver-project-id";
6259
opts.opencensus_task = absl::StrCat("cpp-", getpid(), "@", hostname);

0 commit comments

Comments
 (0)