Skip to content

Commit

Permalink
Merge pull request grpc#1903 from ctiller/maybe-i-want-to-change-my-mind
Browse files Browse the repository at this point in the history
Enable runtime configuration of tracers
  • Loading branch information
yang-g committed Jun 4, 2015
2 parents bed8a06 + 2f300e2 commit 491a67b
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 18 deletions.
10 changes: 10 additions & 0 deletions include/grpc/grpc.h
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,16 @@ void grpc_server_shutdown_and_notify(grpc_server *server, void *tag);
Implies grpc_server_shutdown() if one was not previously performed. */
void grpc_server_destroy(grpc_server *server);

/** Enable or disable a tracer.
Tracers (usually controlled by the environment variable GRPC_TRACE)
allow printf-style debugging on GRPC internals, and are useful for
tracking down problems in the field.
Use of this function is not strictly thread-safe, but the
thread-safety issues raised by it should not be of concern. */
int grpc_tracer_set_enabled(const char *name, int enabled);

#ifdef __cplusplus
}
#endif
Expand Down
45 changes: 27 additions & 18 deletions src/core/debug/trace.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

#include <string.h>

#include <grpc/grpc.h>
#include <grpc/support/alloc.h>
#include <grpc/support/log.h>
#include "src/core/support/env.h"
Expand Down Expand Up @@ -80,27 +81,10 @@ static void parse(const char *s) {
char **strings = NULL;
size_t nstrings = 0;
size_t i;
tracer *t;
split(s, &strings, &nstrings);

for (i = 0; i < nstrings; i++) {
const char *s = strings[i];
if (0 == strcmp(s, "all")) {
for (t = tracers; t; t = t->next) {
*t->flag = 1;
}
} else {
int found = 0;
for (t = tracers; t; t = t->next) {
if (0 == strcmp(s, t->name)) {
*t->flag = 1;
found = 1;
}
}
if (!found) {
gpr_log(GPR_ERROR, "Unknown trace var: '%s'", s);
}
}
grpc_tracer_set_enabled(strings[i], 1);
}

for (i = 0; i < nstrings; i++) {
Expand All @@ -115,9 +99,34 @@ void grpc_tracer_init(const char *env_var) {
parse(e);
gpr_free(e);
}
}

void grpc_tracer_shutdown(void) {
while (tracers) {
tracer *t = tracers;
tracers = t->next;
gpr_free(t);
}
}

int grpc_tracer_set_enabled(const char *name, int enabled) {
tracer *t;
if (0 == strcmp(name, "all")) {
for (t = tracers; t; t = t->next) {
*t->flag = 1;
}
} else {
int found = 0;
for (t = tracers; t; t = t->next) {
if (0 == strcmp(name, t->name)) {
*t->flag = enabled;
found = 1;
}
}
if (!found) {
gpr_log(GPR_ERROR, "Unknown trace var: '%s'", name);
return 0; /* early return */
}
}
return 1;
}
1 change: 1 addition & 0 deletions src/core/debug/trace.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,6 @@

void grpc_register_tracer(const char *name, int *flag);
void grpc_tracer_init(const char *env_var_name);
void grpc_tracer_shutdown(void);

#endif /* GRPC_INTERNAL_CORE_DEBUG_TRACE_H */
1 change: 1 addition & 0 deletions src/core/surface/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ void grpc_shutdown(void) {
grpc_iomgr_shutdown();
census_shutdown();
grpc_timers_global_destroy();
grpc_tracer_shutdown();
}
gpr_mu_unlock(&g_init_mu);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ int main(int argc, char **argv) {
grpc_test_init(argc, argv);
grpc_init();

GPR_ASSERT(0 == grpc_tracer_set_enabled("also-doesnt-exist", 0));
GPR_ASSERT(1 == grpc_tracer_set_enabled("http", 1));
GPR_ASSERT(1 == grpc_tracer_set_enabled("all", 1));

for (i = 0; i < sizeof(configs) / sizeof(*configs); i++) {
grpc_end2end_tests(configs[i]);
}
Expand Down

0 comments on commit 491a67b

Please sign in to comment.