|
12 | 12 | // See the License for the specific language governing permissions and |
13 | 13 | // limitations under the License. |
14 | 14 |
|
| 15 | +#include <limits.h> |
15 | 16 | #include <unistd.h> |
| 17 | +#include <cerrno> |
16 | 18 | #include <cstdlib> |
| 19 | +#include <cstring> |
17 | 20 | #include <iostream> |
18 | 21 |
|
19 | 22 | #include "absl/strings/str_cat.h" |
20 | 23 | #include "examples/grpc/stackdriver.h" |
21 | 24 | #include "opencensus/exporters/stats/stackdriver/stackdriver_exporter.h" |
22 | 25 | #include "opencensus/exporters/trace/stackdriver/stackdriver_exporter.h" |
23 | 26 |
|
| 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 | + |
24 | 36 | void RegisterStackdriverExporters() { |
25 | 37 | const char *project_id = getenv("STACKDRIVER_PROJECT_ID"); |
26 | 38 | if (project_id == nullptr) { |
27 | 39 | std::cerr << "The STACKDRIVER_PROJECT_ID environment variable is not set: " |
28 | 40 | "not exporting to Stackdriver.\n"; |
29 | 41 | return; |
30 | 42 | } |
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 | + } |
33 | 49 |
|
34 | 50 | opencensus::exporters::stats::StackdriverOptions stats_opts; |
35 | 51 | stats_opts.project_id = project_id; |
36 | 52 | stats_opts.opencensus_task = absl::StrCat("cpp-", getpid(), "@", hostname); |
37 | 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"; |
| 57 | + |
38 | 58 | opencensus::exporters::trace::StackdriverOptions trace_opts; |
39 | 59 | trace_opts.project_id = project_id; |
40 | 60 |
|
|
0 commit comments