HTRACE-217. Rename ProcessId to TracerId (cmccabe)
Project: http://git-wip-us.apache.org/repos/asf/incubator-htrace/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-htrace/commit/959a6ef0 Tree: http://git-wip-us.apache.org/repos/asf/incubator-htrace/tree/959a6ef0 Diff: http://git-wip-us.apache.org/repos/asf/incubator-htrace/diff/959a6ef0 Branch: refs/heads/master Commit: 959a6ef03fec92865bab493bac2a5f20b882fdde Parents: 98aecf8 Author: Colin Patrick Mccabe <[email protected]> Authored: Thu Jul 30 11:22:17 2015 -0700 Committer: Colin Patrick Mccabe <[email protected]> Committed: Thu Jul 30 11:22:17 2015 -0700 ---------------------------------------------------------------------- htrace-c/src/CMakeLists.txt | 6 +- htrace-c/src/core/conf.c | 2 +- htrace-c/src/core/htrace.h | 2 +- htrace-c/src/core/htracer.c | 14 +- htrace-c/src/core/htracer.h | 4 +- htrace-c/src/core/span.c | 14 +- htrace-c/src/core/span.h | 4 +- htrace-c/src/receiver/htraced.c | 8 +- htrace-c/src/receiver/local_file.c | 6 +- htrace-c/src/test/cmp_util-unit.c | 4 +- htrace-c/src/test/process_id-unit.c | 80 ----- htrace-c/src/test/rtest.c | 18 +- htrace-c/src/test/rtestpp.cc | 18 +- htrace-c/src/test/sampler-unit.c | 2 +- htrace-c/src/test/span-unit.c | 10 +- htrace-c/src/test/span_table.c | 6 +- htrace-c/src/test/span_table.h | 4 +- htrace-c/src/test/span_util.c | 14 +- htrace-c/src/test/tracer_id-unit.c | 80 +++++ htrace-c/src/util/process_id.c | 304 ------------------- htrace-c/src/util/process_id.h | 58 ---- htrace-c/src/util/tracer_id.c | 304 +++++++++++++++++++ htrace-c/src/util/tracer_id.h | 58 ++++ .../src/main/java/org/apache/htrace/Span.java | 10 +- .../main/java/org/apache/htrace/TraceTree.java | 20 +- .../htrace/impl/LocalFileSpanReceiver.java | 8 +- .../java/org/apache/htrace/impl/MilliSpan.java | 24 +- .../java/org/apache/htrace/impl/ProcessId.java | 291 ------------------ .../java/org/apache/htrace/impl/TracerId.java | 291 ++++++++++++++++++ .../htrace/impl/TestLocalFileSpanReceiver.java | 4 +- .../org/apache/htrace/impl/TestMilliSpan.java | 10 +- .../org/apache/htrace/impl/TestProcessId.java | 47 --- .../org/apache/htrace/impl/TestTracerId.java | 47 +++ .../apache/htrace/impl/FlumeSpanReceiver.java | 10 +- .../htrace/impl/TestFlumeSpanReceiver.java | 2 +- .../apache/htrace/impl/HBaseSpanReceiver.java | 10 +- .../htrace/impl/TestHBaseSpanReceiver.java | 8 +- .../go/src/org/apache/htrace/client/client.go | 4 +- .../go/src/org/apache/htrace/common/query.go | 4 +- .../go/src/org/apache/htrace/common/rpc.go | 4 +- .../go/src/org/apache/htrace/common/span.go | 2 +- .../src/org/apache/htrace/common/span_test.go | 8 +- .../src/org/apache/htrace/htrace/file_test.go | 4 +- .../src/org/apache/htrace/htrace/graph_test.go | 6 +- .../src/org/apache/htrace/htraced/datastore.go | 8 +- .../org/apache/htrace/htraced/datastore_test.go | 6 +- .../go/src/org/apache/htrace/htraced/hrpc.go | 6 +- .../go/src/org/apache/htrace/htraced/rest.go | 10 +- .../go/src/org/apache/htrace/test/random.go | 2 +- .../apache/htrace/impl/HTracedRESTReceiver.java | 6 +- .../htrace/impl/TestHTracedRESTReceiver.java | 16 +- htrace-webapp/src/main/web/app/span.js | 6 +- htrace-webapp/src/main/web/app/span_widget.js | 6 +- .../apache/htrace/impl/ZipkinSpanReceiver.java | 8 +- .../htrace/zipkin/HTraceToZipkinConverter.java | 2 +- .../htrace/TestHTraceSpanToZipkinSpan.java | 8 +- 56 files changed, 959 insertions(+), 959 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/959a6ef0/htrace-c/src/CMakeLists.txt ---------------------------------------------------------------------- diff --git a/htrace-c/src/CMakeLists.txt b/htrace-c/src/CMakeLists.txt index 57558a5..c34a7c2 100644 --- a/htrace-c/src/CMakeLists.txt +++ b/htrace-c/src/CMakeLists.txt @@ -88,7 +88,7 @@ set(SRC_ALL util/cmp_util.c util/htable.c util/log.c - util/process_id.c + util/tracer_id.c util/string.c util/terror.c util/time.c @@ -175,8 +175,8 @@ add_utest(mini_htraced-unit test/mini_htraced-unit.c ) -add_utest(process_id-unit - test/process_id-unit.c +add_utest(tracer_id-unit + test/tracer_id-unit.c ) if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux") http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/959a6ef0/htrace-c/src/core/conf.c ---------------------------------------------------------------------- diff --git a/htrace-c/src/core/conf.c b/htrace-c/src/core/conf.c index d9eed2a..08fbb8c 100644 --- a/htrace-c/src/core/conf.c +++ b/htrace-c/src/core/conf.c @@ -32,7 +32,7 @@ ";" HTRACED_FLUSH_INTERVAL_MS_KEY "=120000"\ ";" HTRACED_WRITE_TIMEO_MS_KEY "=60000"\ ";" HTRACED_READ_TIMEO_MS_KEY "=60000"\ - ";" HTRACE_PROCESS_ID "=%{tname}/%{ip}"\ + ";" HTRACE_TRACER_ID "=%{tname}/%{ip}"\ ";" HTRACED_ADDRESS_KEY "=localhost:9095"\ ";" HTRACED_BUFFER_SEND_TRIGGER_FRACTION "=0.50"\ ) http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/959a6ef0/htrace-c/src/core/htrace.h ---------------------------------------------------------------------- diff --git a/htrace-c/src/core/htrace.h b/htrace-c/src/core/htrace.h index 816c222..e7dc93b 100644 --- a/htrace-c/src/core/htrace.h +++ b/htrace-c/src/core/htrace.h @@ -150,7 +150,7 @@ extern "C" { * * Defaults to %{tname}/%{ip} */ -#define HTRACE_PROCESS_ID "process.id" +#define HTRACE_TRACER_ID "tracer.id" /** * The sampler to use. http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/959a6ef0/htrace-c/src/core/htracer.c ---------------------------------------------------------------------- diff --git a/htrace-c/src/core/htracer.c b/htrace-c/src/core/htracer.c index 3305beb..b1bcb61 100644 --- a/htrace-c/src/core/htracer.c +++ b/htrace-c/src/core/htracer.c @@ -23,9 +23,9 @@ #include "core/span.h" #include "receiver/receiver.h" #include "util/log.h" -#include "util/process_id.h" #include "util/rand.h" #include "util/string.h" +#include "util/tracer_id.h" #include <errno.h> #include <stdint.h> @@ -67,17 +67,17 @@ struct htracer *htracer_create(const char *tname, htracer_free(tracer); return NULL; } - tracer->prid = calculate_process_id(tracer->lg, - htrace_conf_get(cnf, HTRACE_PROCESS_ID), tname); - if (!tracer->prid) { + tracer->trid = calculate_tracer_id(tracer->lg, + htrace_conf_get(cnf, HTRACE_TRACER_ID), tname); + if (!tracer->trid) { htrace_log(tracer->lg, "htracer_create: failed to " "create process id string.\n"); htracer_free(tracer); return NULL; } - if (!validate_json_string(tracer->lg, tracer->prid)) { + if (!validate_json_string(tracer->lg, tracer->trid)) { htrace_log(tracer->lg, "htracer_create: process ID string '%s' is " - "problematic.\n", tracer->prid); + "problematic.\n", tracer->trid); htracer_free(tracer); return NULL; } @@ -117,7 +117,7 @@ void htracer_free(struct htracer *tracer) } random_src_free(tracer->rnd); free(tracer->tname); - free(tracer->prid); + free(tracer->trid); htrace_log_free(tracer->lg); free(tracer); } http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/959a6ef0/htrace-c/src/core/htracer.h ---------------------------------------------------------------------- diff --git a/htrace-c/src/core/htracer.h b/htrace-c/src/core/htracer.h index 2acdf70..76fbbac 100644 --- a/htrace-c/src/core/htracer.h +++ b/htrace-c/src/core/htracer.h @@ -50,9 +50,9 @@ struct htracer { char *tname; /** - * The process id of this context. + * The tracer id of this context. */ - char *prid; + char *trid; /** * The random source to use in this context. http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/959a6ef0/htrace-c/src/core/span.c ---------------------------------------------------------------------- diff --git a/htrace-c/src/core/span.c b/htrace-c/src/core/span.c index 790afa5..b82f4f1 100644 --- a/htrace-c/src/core/span.c +++ b/htrace-c/src/core/span.c @@ -54,7 +54,7 @@ struct htrace_span *htrace_span_alloc(const char *desc, span->begin_ms = begin_ms; span->end_ms = 0; span->span_id = span_id; - span->prid = NULL; + span->trid = NULL; span->num_parents = 0; span->parent.single = 0; span->parent.list = NULL; @@ -67,7 +67,7 @@ void htrace_span_free(struct htrace_span *span) return; } free(span->desc); - free(span->prid); + free(span->trid); if (span->num_parents > 1) { free(span->parent.list); } @@ -152,8 +152,8 @@ static int span_json_sprintf_impl(const struct htrace_span *span, if (span->desc[0]) { ret += fwdprintf(&buf, &max, "\"d\":\"%s\",", span->desc); } - if (span->prid) { - ret += fwdprintf(&buf, &max, "\"r\":\"%s\",", span->prid); + if (span->trid) { + ret += fwdprintf(&buf, &max, "\"r\":\"%s\",", span->trid); } num_parents = span->num_parents; if (num_parents == 0) { @@ -196,7 +196,7 @@ int span_write_msgpack(const struct htrace_span *span, cmp_ctx_t *ctx) 1; // span_id num_parents = span->num_parents; - if (span->prid) { + if (span->trid) { map_size++; } if (num_parents > 0) { @@ -229,11 +229,11 @@ int span_write_msgpack(const struct htrace_span *span, cmp_ctx_t *ctx) if (!cmp_write_u64(ctx, span->span_id)) { return 0; } - if (span->prid) { + if (span->trid) { if (!cmp_write_fixstr(ctx, "r", 1)) { return 0; } - if (!cmp_write_str16(ctx, span->prid, strlen(span->prid))) { + if (!cmp_write_str16(ctx, span->trid, strlen(span->trid))) { return 0; } } http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/959a6ef0/htrace-c/src/core/span.h ---------------------------------------------------------------------- diff --git a/htrace-c/src/core/span.h b/htrace-c/src/core/span.h index 710cff7..4d28e75 100644 --- a/htrace-c/src/core/span.h +++ b/htrace-c/src/core/span.h @@ -55,10 +55,10 @@ struct htrace_span { uint64_t span_id; /** - * The process ID of this trace scope. + * The tracer ID of this trace scope. * Dynamically allocated. May be null. */ - char *prid; + char *trid; /** * The number of parents. http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/959a6ef0/htrace-c/src/receiver/htraced.c ---------------------------------------------------------------------- diff --git a/htrace-c/src/receiver/htraced.c b/htrace-c/src/receiver/htraced.c index 8f392b7..d92518d 100644 --- a/htrace-c/src/receiver/htraced.c +++ b/htrace-c/src/receiver/htraced.c @@ -488,8 +488,8 @@ static int should_xmit(struct htraced_rcv *rcv, uint64_t now) return 0; // Let's wait. } -#define DEFAULT_PID_STR "DefaultPid" -#define DEFAULT_PID_STR_LEN (sizeof(DEFAULT_PID_STR) - 1) +#define DEFAULT_TRID_STR "DefaultTrid" +#define DEFAULT_TRID_STR_LEN (sizeof(DEFAULT_TRID_STR) - 1) #define SPANS_STR "Spans" #define SPANS_STR_LEN (sizeof(SPANS_STR) - 1) @@ -505,10 +505,10 @@ static int add_writespans_prequel(struct htraced_rcv *rcv, if (!cmp_write_fixmap(ctx, 2)) { return -1; } - if (!cmp_write_fixstr(ctx, DEFAULT_PID_STR, DEFAULT_PID_STR_LEN)) { + if (!cmp_write_fixstr(ctx, DEFAULT_TRID_STR, DEFAULT_TRID_STR_LEN)) { return -1; } - if (!cmp_write_str(ctx, rcv->tracer->prid, strlen(rcv->tracer->prid))) { + if (!cmp_write_str(ctx, rcv->tracer->trid, strlen(rcv->tracer->trid))) { return -1; } if (!cmp_write_fixstr(ctx, SPANS_STR, SPANS_STR_LEN)) { http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/959a6ef0/htrace-c/src/receiver/local_file.c ---------------------------------------------------------------------- diff --git a/htrace-c/src/receiver/local_file.c b/htrace-c/src/receiver/local_file.c index 667da29..05fabee 100644 --- a/htrace-c/src/receiver/local_file.c +++ b/htrace-c/src/receiver/local_file.c @@ -114,16 +114,16 @@ static void local_file_rcv_add_span(struct htrace_rcv *r, char *buf; struct local_file_rcv *rcv = (struct local_file_rcv *)r; - span->prid = rcv->tracer->prid; + span->trid = rcv->tracer->trid; len = span_json_size(span); buf = malloc(len + 1); if (!buf) { - span->prid = NULL; + span->trid = NULL; htrace_log(rcv->tracer->lg, "local_file_rcv_add_span: OOM\n"); return; } span_json_sprintf(span, len, buf); - span->prid = NULL; + span->trid = NULL; buf[len - 1] = '\n'; buf[len] = '\0'; pthread_mutex_lock(&rcv->lock); http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/959a6ef0/htrace-c/src/test/cmp_util-unit.c ---------------------------------------------------------------------- diff --git a/htrace-c/src/test/cmp_util-unit.c b/htrace-c/src/test/cmp_util-unit.c index bfe4edc..4675ca8 100644 --- a/htrace-c/src/test/cmp_util-unit.c +++ b/htrace-c/src/test/cmp_util-unit.c @@ -46,7 +46,7 @@ static struct htrace_span **setup_test_spans(void) spans[1]->begin_ms = 1950; spans[1]->end_ms = 2000; spans[1]->span_id = 0xffffffffffffffffULL; - spans[1]->prid = xstrdup("SecondSpanProc"); + spans[1]->trid = xstrdup("SecondSpanProc"); spans[1]->num_parents = 1; spans[1]->parent.single = 1; @@ -55,7 +55,7 @@ static struct htrace_span **setup_test_spans(void) spans[2]->begin_ms = 1969; spans[2]->end_ms = 1997; spans[2]->span_id = 0xcfcfcfcfcfcfcfcfULL; - spans[2]->prid = xstrdup("ThirdSpanProc"); + spans[2]->trid = xstrdup("ThirdSpanProc"); spans[2]->num_parents = 2; spans[2]->parent.list = xcalloc(sizeof(uint64_t) * 2); spans[2]->parent.list[0] = 1; http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/959a6ef0/htrace-c/src/test/process_id-unit.c ---------------------------------------------------------------------- diff --git a/htrace-c/src/test/process_id-unit.c b/htrace-c/src/test/process_id-unit.c deleted file mode 100644 index 5454eb0..0000000 --- a/htrace-c/src/test/process_id-unit.c +++ /dev/null @@ -1,80 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "core/conf.h" -#include "test/test.h" -#include "util/log.h" -#include "util/process_id.h" - -#include <inttypes.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <sys/types.h> -#include <unistd.h> - -static struct htrace_conf *g_cnf; - -static struct htrace_log *g_lg; - -static int test_calculate_process_id(const char *expected, const char *fmt) -{ - char *process_id; - - process_id = calculate_process_id(g_lg, fmt, "fooproc"); - EXPECT_NONNULL(process_id); - EXPECT_STR_EQ(expected, process_id); - free(process_id); - - return EXIT_SUCCESS; -} - -static int test_calculate_process_ids(void) -{ - char buf[128]; - - EXPECT_INT_ZERO(test_calculate_process_id("my.name", "my.name")); - EXPECT_INT_ZERO(test_calculate_process_id("my.fooproc", "my.%{tname}")); - EXPECT_INT_ZERO(test_calculate_process_id("\%foo", "\%foo")); - EXPECT_INT_ZERO(test_calculate_process_id("fooproc", "%{tname}")); - EXPECT_INT_ZERO(test_calculate_process_id("\\fooproc", "\\\\%{tname}")); - EXPECT_INT_ZERO(test_calculate_process_id("\%{tname}", "\\%{tname}")); - - snprintf(buf, sizeof(buf), "me.%lld", (long long)getpid()); - EXPECT_INT_ZERO(test_calculate_process_id(buf, "me.%{pid}")); - - get_best_ip(g_lg, buf, sizeof(buf)); - EXPECT_INT_ZERO(test_calculate_process_id(buf, "%{ip}")); - - return EXIT_SUCCESS; -} - -int main(void) -{ - g_cnf = htrace_conf_from_strs("", ""); - EXPECT_NONNULL(g_cnf); - g_lg = htrace_log_alloc(g_cnf); - EXPECT_NONNULL(g_lg); - EXPECT_INT_ZERO(test_calculate_process_ids()); - htrace_log_free(g_lg); - htrace_conf_free(g_cnf); - - return EXIT_SUCCESS; -} - -// vim: ts=4:sw=4:tw=79:et http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/959a6ef0/htrace-c/src/test/rtest.c ---------------------------------------------------------------------- diff --git a/htrace-c/src/test/rtest.c b/htrace-c/src/test/rtest.c index b299376..ccae241 100644 --- a/htrace-c/src/test/rtest.c +++ b/htrace-c/src/test/rtest.c @@ -45,9 +45,9 @@ struct rtest_data { struct htracer *tracer; }; -static void get_receiver_test_prid(char *prid, size_t prid_len) +static void get_receiver_test_trid(char *trid, size_t trid_len) { - snprintf(prid, prid_len, RECEIVER_TEST_TNAME "/%lld", (long long)getpid()); + snprintf(trid, trid_len, RECEIVER_TEST_TNAME "/%lld", (long long)getpid()); } static int rtest_data_init(const char *conf_str, struct rtest_data **out) @@ -55,7 +55,7 @@ static int rtest_data_init(const char *conf_str, struct rtest_data **out) char *econf_str = NULL; struct rtest_data *rdata = calloc(1, sizeof(*(rdata))); EXPECT_NONNULL(rdata); - if (asprintf(&econf_str, HTRACE_PROCESS_ID"=%%{tname}/%%{pid};sampler=always;" + if (asprintf(&econf_str, HTRACE_TRACER_ID"=%%{tname}/%%{pid};sampler=always;" "%s", conf_str) < 0) { fprintf(stderr, "asprintf(econf_str) failed: OOM\n"); return EXIT_FAILURE; @@ -123,24 +123,24 @@ int rtest_simple_verify(struct rtest *rt, struct span_table *st) { struct htrace_span *span; uint64_t doit_id, part2_id; - char prid[128]; + char trid[128]; EXPECT_INT_ZERO(rtest_verify_table_size(rt, st)); - get_receiver_test_prid(prid, sizeof(prid)); - EXPECT_INT_ZERO(span_table_get(st, &span, "doit", prid)); + get_receiver_test_trid(trid, sizeof(trid)); + EXPECT_INT_ZERO(span_table_get(st, &span, "doit", trid)); doit_id = span->span_id; EXPECT_INT_ZERO(span->num_parents); - EXPECT_INT_ZERO(span_table_get(st, &span, "part1", prid)); + EXPECT_INT_ZERO(span_table_get(st, &span, "part1", trid)); EXPECT_INT_EQ(1, span->num_parents); EXPECT_UINT64_EQ(doit_id, span->parent.single); - EXPECT_INT_ZERO(span_table_get(st, &span, "part2", prid)); + EXPECT_INT_ZERO(span_table_get(st, &span, "part2", trid)); EXPECT_INT_EQ(1, span->num_parents); part2_id = span->span_id; EXPECT_UINT64_EQ(doit_id, span->parent.single); - EXPECT_INT_ZERO(span_table_get(st, &span, "part2.5", prid)); + EXPECT_INT_ZERO(span_table_get(st, &span, "part2.5", trid)); EXPECT_INT_EQ(1, span->num_parents); EXPECT_UINT64_EQ(part2_id, span->parent.single); http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/959a6ef0/htrace-c/src/test/rtestpp.cc ---------------------------------------------------------------------- diff --git a/htrace-c/src/test/rtestpp.cc b/htrace-c/src/test/rtestpp.cc index 649826c..0cd7120 100644 --- a/htrace-c/src/test/rtestpp.cc +++ b/htrace-c/src/test/rtestpp.cc @@ -48,7 +48,7 @@ class RTestData { public: RTestData(struct rtest *rt, const char *conf_str) : rt_(rt), - cnf_(string(HTRACE_PROCESS_ID"=%{tname}/%{pid};sampler=always;") + + cnf_(string(HTRACE_TRACER_ID"=%{tname}/%{pid};sampler=always;") + string(conf_str).c_str()), tracer_(RECEIVER_TEST_TNAME, cnf_), always_(&tracer_, cnf_) @@ -72,9 +72,9 @@ private: RTestData& operator=(const RTestData &other); // disallow assignment }; -static void get_receiver_test_prid(char *prid, size_t prid_len) +static void get_receiver_test_trid(char *trid, size_t trid_len) { - snprintf(prid, prid_len, RECEIVER_TEST_TNAME "/%lld", (long long)getpid()); + snprintf(trid, trid_len, RECEIVER_TEST_TNAME "/%lld", (long long)getpid()); } static int rtest_generic_verify(struct rtest *rt, struct span_table *st) @@ -116,24 +116,24 @@ int rtestpp_simple_verify(struct rtest *rt, struct span_table *st) { struct htrace_span *span; uint64_t doit_id, part2_id; - char prid[128]; + char trid[128]; EXPECT_INT_ZERO(rtest_generic_verify(rt, st)); - get_receiver_test_prid(prid, sizeof(prid)); - EXPECT_INT_ZERO(span_table_get(st, &span, "doit", prid)); + get_receiver_test_trid(trid, sizeof(trid)); + EXPECT_INT_ZERO(span_table_get(st, &span, "doit", trid)); doit_id = span->span_id; EXPECT_INT_ZERO(span->num_parents); - EXPECT_INT_ZERO(span_table_get(st, &span, "part1", prid)); + EXPECT_INT_ZERO(span_table_get(st, &span, "part1", trid)); EXPECT_INT_EQ(1, span->num_parents); EXPECT_UINT64_EQ(doit_id, span->parent.single); - EXPECT_INT_ZERO(span_table_get(st, &span, "part2", prid)); + EXPECT_INT_ZERO(span_table_get(st, &span, "part2", trid)); EXPECT_INT_EQ(1, span->num_parents); part2_id = span->span_id; EXPECT_UINT64_EQ(doit_id, span->parent.single); - EXPECT_INT_ZERO(span_table_get(st, &span, "part2.5", prid)); + EXPECT_INT_ZERO(span_table_get(st, &span, "part2.5", trid)); EXPECT_INT_EQ(1, span->num_parents); EXPECT_UINT64_EQ(part2_id, span->parent.single); http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/959a6ef0/htrace-c/src/test/sampler-unit.c ---------------------------------------------------------------------- diff --git a/htrace-c/src/test/sampler-unit.c b/htrace-c/src/test/sampler-unit.c index 2a42292..b1f7391 100644 --- a/htrace-c/src/test/sampler-unit.c +++ b/htrace-c/src/test/sampler-unit.c @@ -115,7 +115,7 @@ static int test_prob_sampler(double target, double slop) int main(void) { - g_test_conf = htrace_conf_from_strs("", HTRACE_PROCESS_ID"=sampler-unit"); + g_test_conf = htrace_conf_from_strs("", HTRACE_TRACER_ID"=sampler-unit"); EXPECT_NONNULL(g_test_conf); g_test_lg = htrace_log_alloc(g_test_conf); EXPECT_NONNULL(g_test_lg); http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/959a6ef0/htrace-c/src/test/span-unit.c ---------------------------------------------------------------------- diff --git a/htrace-c/src/test/span-unit.c b/htrace-c/src/test/span-unit.c index da6ca66..5482b8d 100644 --- a/htrace-c/src/test/span-unit.c +++ b/htrace-c/src/test/span-unit.c @@ -43,7 +43,7 @@ static struct htracer *g_test_tracer; static struct htrace_span *create_span(const char *desc, uint64_t begin_ms, uint64_t end_ms, uint64_t span_id, - const char *prid, ...) __attribute__((sentinel)); + const char *trid, ...) __attribute__((sentinel)); static int test_span_to_json(const char *expected, struct htrace_span *span) @@ -83,14 +83,14 @@ static int test_span_to_json(const char *expected, static struct htrace_span *create_span(const char *desc, uint64_t begin_ms, uint64_t end_ms, uint64_t span_id, - const char *prid, ...) + const char *trid, ...) { struct htrace_span* span = NULL; uint64_t *parents, parent; int i, num_parents = 0; va_list ap, ap2; - va_start(ap, prid); + va_start(ap, trid); va_copy(ap2, ap); while (1) { parent = va_arg(ap2, uint64_t); @@ -110,7 +110,7 @@ static struct htrace_span *create_span(const char *desc, span = htrace_span_alloc(desc, begin_ms, span_id); span->end_ms = end_ms; span->span_id = span_id; - span->prid = xstrdup(prid); + span->trid = xstrdup(trid); span->num_parents = num_parents; if (num_parents == 1) { span->parent.single = parents[0]; @@ -155,7 +155,7 @@ static int test_spans_to_str(void) int main(void) { - g_test_conf = htrace_conf_from_strs("", HTRACE_PROCESS_ID"=span-unit"); + g_test_conf = htrace_conf_from_strs("", HTRACE_TRACER_ID"=span-unit"); EXPECT_NONNULL(g_test_conf); g_test_lg = htrace_log_alloc(g_test_conf); EXPECT_NONNULL(g_test_lg); http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/959a6ef0/htrace-c/src/test/span_table.c ---------------------------------------------------------------------- diff --git a/htrace-c/src/test/span_table.c b/htrace-c/src/test/span_table.c index 8840fbf..42ed5f6 100644 --- a/htrace-c/src/test/span_table.c +++ b/htrace-c/src/test/span_table.c @@ -41,7 +41,7 @@ struct span_table *span_table_alloc(void) } int span_table_get(struct span_table *st, struct htrace_span **out, - const char *desc, const char *prid) + const char *desc, const char *trid) { struct htable *ht = (struct htable *)st; struct htrace_span *span; @@ -51,8 +51,8 @@ int span_table_get(struct span_table *st, struct htrace_span **out, EXPECT_STR_EQ(desc, span->desc); EXPECT_UINT64_GE(span->begin_ms, span->end_ms); EXPECT_UINT64_GT(0L, span->span_id); - EXPECT_NONNULL(span->prid); - EXPECT_STR_EQ(prid, span->prid); + EXPECT_NONNULL(span->trid); + EXPECT_STR_EQ(trid, span->trid); *out = span; return EXIT_SUCCESS; } http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/959a6ef0/htrace-c/src/test/span_table.h ---------------------------------------------------------------------- diff --git a/htrace-c/src/test/span_table.h b/htrace-c/src/test/span_table.h index b55cd92..28399dd 100644 --- a/htrace-c/src/test/span_table.h +++ b/htrace-c/src/test/span_table.h @@ -45,12 +45,12 @@ struct span_table *span_table_alloc(void); * @param out (out param) the span. This pointer will be valid until * the span table is freed. * @param desc The span description to look for. - * @param prid The process ID to verify that the span has. + * @param trid The process ID to verify that the span has. * * @return 0 on success; nonzero otherwise. */ int span_table_get(struct span_table *st, struct htrace_span **out, - const char *desc, const char *prid); + const char *desc, const char *trid); /** * Add a span to the table. http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/959a6ef0/htrace-c/src/test/span_util.c ---------------------------------------------------------------------- diff --git a/htrace-c/src/test/span_util.c b/htrace-c/src/test/span_util.c index 2069cdc..625ba86 100644 --- a/htrace-c/src/test/span_util.c +++ b/htrace-c/src/test/span_util.c @@ -150,11 +150,11 @@ static void span_json_parse_impl(struct json_object *root, } } if (json_object_object_get_ex(root, "r", &r)) { - span->prid = strdup(json_object_get_string(r)); + span->trid = strdup(json_object_get_string(r)); } else { - span->prid = strdup(""); + span->trid = strdup(""); } - if (!span->prid) { + if (!span->trid) { snprintf(err, err_len, "out of memory allocating process id"); return; } @@ -285,7 +285,7 @@ int span_compare(struct htrace_span *a, struct htrace_span *b) if (c) { return c; } - c = strcmp_handle_null(a->prid, b->prid); + c = strcmp_handle_null(a->trid, b->trid); if (c) { return c; } @@ -444,10 +444,10 @@ struct htrace_span *span_read_msgpack(struct cmp_ctx_s *ctx, } break; case 'r': - if (span->prid) { - free(span->prid); + if (span->trid) { + free(span->trid); } - span->prid = cmp_read_malloced_string(ctx, "process_id", + span->trid = cmp_read_malloced_string(ctx, "tracer_id", err, err_len); if (err[0]) { goto error; http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/959a6ef0/htrace-c/src/test/tracer_id-unit.c ---------------------------------------------------------------------- diff --git a/htrace-c/src/test/tracer_id-unit.c b/htrace-c/src/test/tracer_id-unit.c new file mode 100644 index 0000000..fe034b4 --- /dev/null +++ b/htrace-c/src/test/tracer_id-unit.c @@ -0,0 +1,80 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "core/conf.h" +#include "test/test.h" +#include "util/log.h" +#include "util/tracer_id.h" + +#include <inttypes.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/types.h> +#include <unistd.h> + +static struct htrace_conf *g_cnf; + +static struct htrace_log *g_lg; + +static int test_calculate_tracer_id(const char *expected, const char *fmt) +{ + char *tracer_id; + + tracer_id = calculate_tracer_id(g_lg, fmt, "fooproc"); + EXPECT_NONNULL(tracer_id); + EXPECT_STR_EQ(expected, tracer_id); + free(tracer_id); + + return EXIT_SUCCESS; +} + +static int test_calculate_tracer_ids(void) +{ + char buf[128]; + + EXPECT_INT_ZERO(test_calculate_tracer_id("my.name", "my.name")); + EXPECT_INT_ZERO(test_calculate_tracer_id("my.fooproc", "my.%{tname}")); + EXPECT_INT_ZERO(test_calculate_tracer_id("\%foo", "\%foo")); + EXPECT_INT_ZERO(test_calculate_tracer_id("fooproc", "%{tname}")); + EXPECT_INT_ZERO(test_calculate_tracer_id("\\fooproc", "\\\\%{tname}")); + EXPECT_INT_ZERO(test_calculate_tracer_id("\%{tname}", "\\%{tname}")); + + snprintf(buf, sizeof(buf), "me.%lld", (long long)getpid()); + EXPECT_INT_ZERO(test_calculate_tracer_id(buf, "me.%{pid}")); + + get_best_ip(g_lg, buf, sizeof(buf)); + EXPECT_INT_ZERO(test_calculate_tracer_id(buf, "%{ip}")); + + return EXIT_SUCCESS; +} + +int main(void) +{ + g_cnf = htrace_conf_from_strs("", ""); + EXPECT_NONNULL(g_cnf); + g_lg = htrace_log_alloc(g_cnf); + EXPECT_NONNULL(g_lg); + EXPECT_INT_ZERO(test_calculate_tracer_ids()); + htrace_log_free(g_lg); + htrace_conf_free(g_cnf); + + return EXIT_SUCCESS; +} + +// vim: ts=4:sw=4:tw=79:et http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/959a6ef0/htrace-c/src/util/process_id.c ---------------------------------------------------------------------- diff --git a/htrace-c/src/util/process_id.c b/htrace-c/src/util/process_id.c deleted file mode 100644 index c11b693..0000000 --- a/htrace-c/src/util/process_id.c +++ /dev/null @@ -1,304 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "util/log.h" -#include "util/process_id.h" - -#include <arpa/inet.h> -#include <errno.h> -#include <ifaddrs.h> -#include <stdint.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <sys/socket.h> -#include <sys/types.h> -#include <unistd.h> - -/** - * @file process_id.c - * - * Implements process IDs for the HTrace C client. - */ - -/** - * The maximum number of bytes that can be in a process ID substitution - * variable. - */ -#define MAX_VAR_NAME 32 - -enum ip_addr_type { - ADDR_TYPE_IPV6_LOOPBACK = 0, - ADDR_TYPE_IPV4_LOOPBACK, - ADDR_TYPE_IPV6_OTHER, - ADDR_TYPE_IPV4_OTHER, - ADDR_TYPE_IPV6_SITE_LOCAL, - ADDR_TYPE_IPV4_SITE_LOCAL -}; - -static int handle_process_subst_var(struct htrace_log *lg, char **out, - const char *var, const char *tname); - -enum ip_addr_type get_ipv4_addr_type(const struct sockaddr_in *ip); - -enum ip_addr_type get_ipv6_addr_type(const struct sockaddr_in6 *ip); - -static int append_char(char **out, int *j, char c) -{ - char *nout = realloc(*out, *j + 2); // leave space for NULL - if (!nout) { - return 0; - } - *out = nout; - (*out)[*j] = c; - *j = *j + 1; - (*out)[*j] = '\0'; - return 1; -} - -char *calculate_process_id(struct htrace_log *lg, const char *fmt, - const char *tname) -{ - int i = 0, j = 0, escaping = 0, v = 0; - char *out = NULL, *var = NULL; - - out = strdup(""); - if (!out) { - goto oom; - } - while (1) { - char c = fmt[i++]; - if (c == '\0') { - break; - } else if (c == '\\') { - if (!escaping) { - escaping = 1; - continue; - } - } - switch (v) { - case 0: - if (c == '%') { - if (!escaping) { - if (!append_char(&var, &v, '%')) { - goto oom; - } - continue; - } - } - break; - case 1: - if (c == '{') { - if (!escaping) { - if (!append_char(&var, &v, '{')) { - goto oom; - } - continue; - } - } - if (!append_char(&out, &j, '%')) { - goto oom; - } - break; - default: - if (c == '}') { - if (!escaping) { - if (!append_char(&var, &v, '}')) { - goto oom; - } - var[v++] = '\0'; - if (!handle_process_subst_var(lg, &out, var, tname)) { - goto oom; - } - free(var); - var = NULL; - j = strlen(out); - v = 0; - continue; - } - } - escaping = 0; - if (!append_char(&var, &v, c)) { - goto oom; - } - continue; - } - escaping = 0; - v = 0; - if (!append_char(&out, &j, c)) { - goto oom; - } - } - out[j] = '\0'; - if (v > 0) { - htrace_log(lg, "calculate_process_id(%s): unterminated process ID " - "substitution variable at the end of the format string.", - fmt); - } - free(var); - return out; - -oom: - htrace_log(lg, "calculate_process_id(tname=%s): OOM\n", tname); - free(out); - free(var); - return NULL; -} - -static int handle_process_subst_var(struct htrace_log *lg, char **out, - const char *var, const char *tname) -{ - char *nout = NULL; - - if (strcmp(var, "%{tname}") == 0) { - if (asprintf(&nout, "%s%s", *out, tname) < 0) { - htrace_log(lg, "handle_process_subst_var(var=%s): OOM", var); - return 0; - } - free(*out); - *out = nout; - } else if (strcmp(var, "%{ip}") == 0) { - char ip_str[256]; - get_best_ip(lg, ip_str, sizeof(ip_str)); - if (asprintf(&nout, "%s%s", *out, ip_str) < 0) { - htrace_log(lg, "handle_process_subst_var(var=%s): OOM", var); - return 0; - } - free(*out); - *out = nout; - } else if (strcmp(var, "%{pid}") == 0) { - char pid_str[64]; - pid_t pid = getpid(); - - snprintf(pid_str, sizeof(pid_str), "%lld", (long long)pid); - if (asprintf(&nout, "%s%s", *out, pid_str) < 0) { - htrace_log(lg, "handle_process_subst_var(var=%s): OOM", var); - return 0; - } - free(*out); - *out = nout; - } else { - htrace_log(lg, "handle_process_subst_var(var=%s): unknown process " - "ID substitution variable.\n", var); - } - return 1; -} - -/** - * Get the "best" IP address for this node. - * - * This is complicated since nodes can have multiple network interfaces, - * and each network interface can have multiple IP addresses. What we're - * looking for here is an IP address that will serve to identify this node - * to HTrace. So we prefer site-local addresess (i.e. private ones on the - * LAN) to publicly routable interfaces. If there are multiple addresses - * to choose from, we select the one which comes first in textual sort - * order. This should ensure that we at least consistently call each node - * by a single name. - */ -void get_best_ip(struct htrace_log *lg, char *ip_str, size_t ip_str_len) -{ - struct ifaddrs *head, *ifa; - enum ip_addr_type ty = ADDR_TYPE_IPV4_LOOPBACK, nty; - char temp_ip_str[128]; - - snprintf(ip_str, ip_str_len, "%s", "127.0.0.1"); - if (getifaddrs(&head) < 0) { - int res = errno; - htrace_log(lg, "get_best_ip: getifaddrs failed: %s\n", terror(res)); - return; - } - for (ifa = head; ifa; ifa = ifa->ifa_next){ - if (!ifa->ifa_addr) { - continue; - } - if (ifa->ifa_addr->sa_family == AF_INET) { - struct sockaddr_in *addr = - (struct sockaddr_in *)ifa->ifa_addr; - nty = get_ipv4_addr_type(addr); - if (nty < ty) { - continue; - } - if (!inet_ntop(AF_INET, &addr->sin_addr, temp_ip_str, - sizeof(temp_ip_str))) { - htrace_log(lg, "get_best_ip_impl: inet_ntop(%s, AF_INET) " - "failed\n", ifa->ifa_name); - continue; - } - if ((nty == ty) && (strcmp(temp_ip_str, ip_str) > 0)) { - continue; - } - snprintf(ip_str, ip_str_len, "%s", temp_ip_str); - ty = nty; - } else if (ifa->ifa_addr->sa_family == AF_INET6) { - struct sockaddr_in6 *addr = - (struct sockaddr_in6 *)ifa->ifa_addr; - nty = get_ipv6_addr_type(addr); - if (nty < ty) { - continue; - } - if (!inet_ntop(AF_INET6, &addr->sin6_addr, temp_ip_str, - sizeof(temp_ip_str))) { - htrace_log(lg, "get_best_ip_impl: inet_ntop(%s, AF_INET6) " - "failed\n", ifa->ifa_name); - continue; - } - if ((nty == ty) && (strcmp(temp_ip_str, ip_str) > 0)) { - continue; - } - snprintf(ip_str, ip_str_len, "%s", temp_ip_str); - ty = nty; - } - } - freeifaddrs(head); -} - -enum ip_addr_type get_ipv4_addr_type(const struct sockaddr_in *ip) -{ - union { - uint8_t b[4]; - uint32_t addr; - } addr; - addr.addr = ip->sin_addr.s_addr; // always big-endian - if (addr.b[0] == 127) { // 127.0.0.0/24 - return ADDR_TYPE_IPV4_LOOPBACK; - } - if ((addr.b[0] == 10) && (addr.b[1] == 0) && (addr.b[2] == 0)) { - return ADDR_TYPE_IPV4_SITE_LOCAL; // 10.0.0.0/8 - } - if ((addr.b[0] == 192) && (addr.b[1] == 168)) { - return ADDR_TYPE_IPV4_SITE_LOCAL; // 192.168.0.0/16 - } - if ((addr.b[0] == 172) && (addr.b[1] == 16) && ((addr.b[2] & 0xf0) == 0)) { - return ADDR_TYPE_IPV4_SITE_LOCAL; // 172.16.0.0/12 - } - return ADDR_TYPE_IPV4_OTHER; -} - -enum ip_addr_type get_ipv6_addr_type(const struct sockaddr_in6 *ip) -{ - if (IN6_IS_ADDR_LOOPBACK(ip)) { - return ADDR_TYPE_IPV6_LOOPBACK; - } else if (IN6_IS_ADDR_SITELOCAL(ip)) { - return ADDR_TYPE_IPV6_SITE_LOCAL; - } else { - return ADDR_TYPE_IPV6_OTHER; - } -} - -// vim:ts=4:sw=4:et http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/959a6ef0/htrace-c/src/util/process_id.h ---------------------------------------------------------------------- diff --git a/htrace-c/src/util/process_id.h b/htrace-c/src/util/process_id.h deleted file mode 100644 index ff702ce..0000000 --- a/htrace-c/src/util/process_id.h +++ /dev/null @@ -1,58 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef APACHE_HTRACE_UTIL_PROCESS_ID_H -#define APACHE_HTRACE_UTIL_PROCESS_ID_H - -#include <unistd.h> /* for size_t */ - -/** - * @file process_id.h - * - * Implements process IDs for the HTrace C client. - * - * This is an internal header, not intended for external use. - */ - -struct htrace_log; - -/** - * Calculate the process ID. - * - * @param lg A log object which will be used to report warnings. - * @param fmt The user-provided string to use when calculating the - * process ID. - * @param tname The name supplied when creating the htracer. - * - * @return NULL on OOM; the process ID otherwise. - */ -char *calculate_process_id(struct htrace_log *lg, const char *fmt, - const char *tname); - -/** - * Get the best IP address representing this host. - * - * @param lg A log object which will be used to report warnings. - * @param ip_str (out param) output string - * @param ip_str_len Length of output string - */ -void get_best_ip(struct htrace_log *lg, char *ip_str, size_t ip_str_len); - -#endif - -// vim: ts=4: sw=4: et http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/959a6ef0/htrace-c/src/util/tracer_id.c ---------------------------------------------------------------------- diff --git a/htrace-c/src/util/tracer_id.c b/htrace-c/src/util/tracer_id.c new file mode 100644 index 0000000..9d4812d --- /dev/null +++ b/htrace-c/src/util/tracer_id.c @@ -0,0 +1,304 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "util/log.h" +#include "util/tracer_id.h" + +#include <arpa/inet.h> +#include <errno.h> +#include <ifaddrs.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/socket.h> +#include <sys/types.h> +#include <unistd.h> + +/** + * @file tracer_id.c + * + * Implements process IDs for the HTrace C client. + */ + +/** + * The maximum number of bytes that can be in a process ID substitution + * variable. + */ +#define MAX_VAR_NAME 32 + +enum ip_addr_type { + ADDR_TYPE_IPV6_LOOPBACK = 0, + ADDR_TYPE_IPV4_LOOPBACK, + ADDR_TYPE_IPV6_OTHER, + ADDR_TYPE_IPV4_OTHER, + ADDR_TYPE_IPV6_SITE_LOCAL, + ADDR_TYPE_IPV4_SITE_LOCAL +}; + +static int handle_process_subst_var(struct htrace_log *lg, char **out, + const char *var, const char *tname); + +enum ip_addr_type get_ipv4_addr_type(const struct sockaddr_in *ip); + +enum ip_addr_type get_ipv6_addr_type(const struct sockaddr_in6 *ip); + +static int append_char(char **out, int *j, char c) +{ + char *nout = realloc(*out, *j + 2); // leave space for NULL + if (!nout) { + return 0; + } + *out = nout; + (*out)[*j] = c; + *j = *j + 1; + (*out)[*j] = '\0'; + return 1; +} + +char *calculate_tracer_id(struct htrace_log *lg, const char *fmt, + const char *tname) +{ + int i = 0, j = 0, escaping = 0, v = 0; + char *out = NULL, *var = NULL; + + out = strdup(""); + if (!out) { + goto oom; + } + while (1) { + char c = fmt[i++]; + if (c == '\0') { + break; + } else if (c == '\\') { + if (!escaping) { + escaping = 1; + continue; + } + } + switch (v) { + case 0: + if (c == '%') { + if (!escaping) { + if (!append_char(&var, &v, '%')) { + goto oom; + } + continue; + } + } + break; + case 1: + if (c == '{') { + if (!escaping) { + if (!append_char(&var, &v, '{')) { + goto oom; + } + continue; + } + } + if (!append_char(&out, &j, '%')) { + goto oom; + } + break; + default: + if (c == '}') { + if (!escaping) { + if (!append_char(&var, &v, '}')) { + goto oom; + } + var[v++] = '\0'; + if (!handle_process_subst_var(lg, &out, var, tname)) { + goto oom; + } + free(var); + var = NULL; + j = strlen(out); + v = 0; + continue; + } + } + escaping = 0; + if (!append_char(&var, &v, c)) { + goto oom; + } + continue; + } + escaping = 0; + v = 0; + if (!append_char(&out, &j, c)) { + goto oom; + } + } + out[j] = '\0'; + if (v > 0) { + htrace_log(lg, "calculate_tracer_id(%s): unterminated process ID " + "substitution variable at the end of the format string.", + fmt); + } + free(var); + return out; + +oom: + htrace_log(lg, "calculate_tracer_id(tname=%s): OOM\n", tname); + free(out); + free(var); + return NULL; +} + +static int handle_process_subst_var(struct htrace_log *lg, char **out, + const char *var, const char *tname) +{ + char *nout = NULL; + + if (strcmp(var, "%{tname}") == 0) { + if (asprintf(&nout, "%s%s", *out, tname) < 0) { + htrace_log(lg, "handle_process_subst_var(var=%s): OOM", var); + return 0; + } + free(*out); + *out = nout; + } else if (strcmp(var, "%{ip}") == 0) { + char ip_str[256]; + get_best_ip(lg, ip_str, sizeof(ip_str)); + if (asprintf(&nout, "%s%s", *out, ip_str) < 0) { + htrace_log(lg, "handle_process_subst_var(var=%s): OOM", var); + return 0; + } + free(*out); + *out = nout; + } else if (strcmp(var, "%{pid}") == 0) { + char pid_str[64]; + pid_t pid = getpid(); + + snprintf(pid_str, sizeof(pid_str), "%lld", (long long)pid); + if (asprintf(&nout, "%s%s", *out, pid_str) < 0) { + htrace_log(lg, "handle_process_subst_var(var=%s): OOM", var); + return 0; + } + free(*out); + *out = nout; + } else { + htrace_log(lg, "handle_process_subst_var(var=%s): unknown process " + "ID substitution variable.\n", var); + } + return 1; +} + +/** + * Get the "best" IP address for this node. + * + * This is complicated since nodes can have multiple network interfaces, + * and each network interface can have multiple IP addresses. What we're + * looking for here is an IP address that will serve to identify this node + * to HTrace. So we prefer site-local addresess (i.e. private ones on the + * LAN) to publicly routable interfaces. If there are multiple addresses + * to choose from, we select the one which comes first in textual sort + * order. This should ensure that we at least consistently call each node + * by a single name. + */ +void get_best_ip(struct htrace_log *lg, char *ip_str, size_t ip_str_len) +{ + struct ifaddrs *head, *ifa; + enum ip_addr_type ty = ADDR_TYPE_IPV4_LOOPBACK, nty; + char temp_ip_str[128]; + + snprintf(ip_str, ip_str_len, "%s", "127.0.0.1"); + if (getifaddrs(&head) < 0) { + int res = errno; + htrace_log(lg, "get_best_ip: getifaddrs failed: %s\n", terror(res)); + return; + } + for (ifa = head; ifa; ifa = ifa->ifa_next){ + if (!ifa->ifa_addr) { + continue; + } + if (ifa->ifa_addr->sa_family == AF_INET) { + struct sockaddr_in *addr = + (struct sockaddr_in *)ifa->ifa_addr; + nty = get_ipv4_addr_type(addr); + if (nty < ty) { + continue; + } + if (!inet_ntop(AF_INET, &addr->sin_addr, temp_ip_str, + sizeof(temp_ip_str))) { + htrace_log(lg, "get_best_ip_impl: inet_ntop(%s, AF_INET) " + "failed\n", ifa->ifa_name); + continue; + } + if ((nty == ty) && (strcmp(temp_ip_str, ip_str) > 0)) { + continue; + } + snprintf(ip_str, ip_str_len, "%s", temp_ip_str); + ty = nty; + } else if (ifa->ifa_addr->sa_family == AF_INET6) { + struct sockaddr_in6 *addr = + (struct sockaddr_in6 *)ifa->ifa_addr; + nty = get_ipv6_addr_type(addr); + if (nty < ty) { + continue; + } + if (!inet_ntop(AF_INET6, &addr->sin6_addr, temp_ip_str, + sizeof(temp_ip_str))) { + htrace_log(lg, "get_best_ip_impl: inet_ntop(%s, AF_INET6) " + "failed\n", ifa->ifa_name); + continue; + } + if ((nty == ty) && (strcmp(temp_ip_str, ip_str) > 0)) { + continue; + } + snprintf(ip_str, ip_str_len, "%s", temp_ip_str); + ty = nty; + } + } + freeifaddrs(head); +} + +enum ip_addr_type get_ipv4_addr_type(const struct sockaddr_in *ip) +{ + union { + uint8_t b[4]; + uint32_t addr; + } addr; + addr.addr = ip->sin_addr.s_addr; // always big-endian + if (addr.b[0] == 127) { // 127.0.0.0/24 + return ADDR_TYPE_IPV4_LOOPBACK; + } + if ((addr.b[0] == 10) && (addr.b[1] == 0) && (addr.b[2] == 0)) { + return ADDR_TYPE_IPV4_SITE_LOCAL; // 10.0.0.0/8 + } + if ((addr.b[0] == 192) && (addr.b[1] == 168)) { + return ADDR_TYPE_IPV4_SITE_LOCAL; // 192.168.0.0/16 + } + if ((addr.b[0] == 172) && (addr.b[1] == 16) && ((addr.b[2] & 0xf0) == 0)) { + return ADDR_TYPE_IPV4_SITE_LOCAL; // 172.16.0.0/12 + } + return ADDR_TYPE_IPV4_OTHER; +} + +enum ip_addr_type get_ipv6_addr_type(const struct sockaddr_in6 *ip) +{ + if (IN6_IS_ADDR_LOOPBACK(ip)) { + return ADDR_TYPE_IPV6_LOOPBACK; + } else if (IN6_IS_ADDR_SITELOCAL(ip)) { + return ADDR_TYPE_IPV6_SITE_LOCAL; + } else { + return ADDR_TYPE_IPV6_OTHER; + } +} + +// vim:ts=4:sw=4:et http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/959a6ef0/htrace-c/src/util/tracer_id.h ---------------------------------------------------------------------- diff --git a/htrace-c/src/util/tracer_id.h b/htrace-c/src/util/tracer_id.h new file mode 100644 index 0000000..ccc0bbb --- /dev/null +++ b/htrace-c/src/util/tracer_id.h @@ -0,0 +1,58 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef APACHE_HTRACE_UTIL_TRACER_ID_H +#define APACHE_HTRACE_UTIL_TRACER_ID_H + +#include <unistd.h> /* for size_t */ + +/** + * @file tracer_id.h + * + * Implements process IDs for the HTrace C client. + * + * This is an internal header, not intended for external use. + */ + +struct htrace_log; + +/** + * Calculate the tracer ID. + * + * @param lg A log object which will be used to report warnings. + * @param fmt The user-provided string to use when calculating the + * tracer ID. + * @param tname The name supplied when creating the htracer. + * + * @return NULL on OOM; the tracer ID otherwise. + */ +char *calculate_tracer_id(struct htrace_log *lg, const char *fmt, + const char *tname); + +/** + * Get the best IP address representing this host. + * + * @param lg A log object which will be used to report warnings. + * @param ip_str (out param) output string + * @param ip_str_len Length of output string + */ +void get_best_ip(struct htrace_log *lg, char *ip_str, size_t ip_str_len); + +#endif + +// vim: ts=4: sw=4: et http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/959a6ef0/htrace-core/src/main/java/org/apache/htrace/Span.java ---------------------------------------------------------------------- diff --git a/htrace-core/src/main/java/org/apache/htrace/Span.java b/htrace-core/src/main/java/org/apache/htrace/Span.java index b28ff8c..f41da30 100644 --- a/htrace-core/src/main/java/org/apache/htrace/Span.java +++ b/htrace-core/src/main/java/org/apache/htrace/Span.java @@ -134,12 +134,12 @@ public interface Span { * * Will never be null. */ - String getProcessId(); + String getTracerId(); /** * Set the process id of a span. */ - void setProcessId(String s); + void setTracerId(String s); /** * Serialize to Json @@ -166,9 +166,9 @@ public interface Span { if (!span.getDescription().isEmpty()) { jgen.writeStringField("d", span.getDescription()); } - String processId = span.getProcessId(); - if (!processId.isEmpty()) { - jgen.writeStringField("r", processId); + String tracerId = span.getTracerId(); + if (!tracerId.isEmpty()) { + jgen.writeStringField("r", tracerId); } jgen.writeArrayFieldStart("p"); for (long parent : span.getParents()) { http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/959a6ef0/htrace-core/src/main/java/org/apache/htrace/TraceTree.java ---------------------------------------------------------------------- diff --git a/htrace-core/src/main/java/org/apache/htrace/TraceTree.java b/htrace-core/src/main/java/org/apache/htrace/TraceTree.java index 5edfde0..db781cb 100644 --- a/htrace-core/src/main/java/org/apache/htrace/TraceTree.java +++ b/htrace-core/src/main/java/org/apache/htrace/TraceTree.java @@ -99,7 +99,7 @@ public class TraceTree { } } - public static class SpansByProcessId { + public static class SpansByTracerId { /** * Compare two spans by process ID, and then by span ID. */ @@ -107,7 +107,7 @@ public class TraceTree { new Comparator<Span>() { @Override public int compare(Span a, Span b) { - int cmp = a.getProcessId().compareTo(b.getProcessId()); + int cmp = a.getTracerId().compareTo(b.getTracerId()); if (cmp != 0) { return cmp; } else if (a.getSpanId() < b.getSpanId()) { @@ -122,7 +122,7 @@ public class TraceTree { private final TreeSet<Span> treeSet; - SpansByProcessId(Collection<Span> spans) { + SpansByTracerId(Collection<Span> spans) { TreeSet<Span> treeSet = new TreeSet<Span>(COMPARATOR); for (Span span : spans) { treeSet.add(span); @@ -130,19 +130,19 @@ public class TraceTree { this.treeSet = treeSet; } - public List<Span> find(String processId) { + public List<Span> find(String tracerId) { List<Span> spans = new ArrayList<Span>(); Span span = new MilliSpan.Builder(). traceId(Long.MIN_VALUE). spanId(Long.MIN_VALUE). - processId(processId). + tracerId(tracerId). build(); while (true) { span = treeSet.higher(span); if (span == null) { break; } - if (span.getProcessId().equals(processId)) { + if (span.getTracerId().equals(tracerId)) { break; } spans.add(span); @@ -156,7 +156,7 @@ public class TraceTree { } private final SpansByParent spansByParent; - private final SpansByProcessId spansByProcessId; + private final SpansByTracerId spansByTracerId; /** * Create a new TraceTree @@ -166,15 +166,15 @@ public class TraceTree { */ public TraceTree(Collection<Span> spans) { this.spansByParent = new SpansByParent(spans); - this.spansByProcessId = new SpansByProcessId(spans); + this.spansByTracerId = new SpansByTracerId(spans); } public SpansByParent getSpansByParent() { return spansByParent; } - public SpansByProcessId getSpansByProcessId() { - return spansByProcessId; + public SpansByTracerId getSpansByTracerId() { + return spansByTracerId; } @Override http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/959a6ef0/htrace-core/src/main/java/org/apache/htrace/impl/LocalFileSpanReceiver.java ---------------------------------------------------------------------- diff --git a/htrace-core/src/main/java/org/apache/htrace/impl/LocalFileSpanReceiver.java b/htrace-core/src/main/java/org/apache/htrace/impl/LocalFileSpanReceiver.java index 95da72c..dfb701d 100644 --- a/htrace-core/src/main/java/org/apache/htrace/impl/LocalFileSpanReceiver.java +++ b/htrace-core/src/main/java/org/apache/htrace/impl/LocalFileSpanReceiver.java @@ -59,7 +59,7 @@ public class LocalFileSpanReceiver implements SpanReceiver { private final FileOutputStream stream; private final FileChannel channel; private final ReentrantLock channelLock = new ReentrantLock(); - private final ProcessId processId; + private final TracerId tracerId; public LocalFileSpanReceiver(HTraceConfiguration conf) { int capacity = conf.getInt(CAPACITY_KEY, CAPACITY_DEFAULT); @@ -94,7 +94,7 @@ public class LocalFileSpanReceiver implements SpanReceiver { LOG.debug("Created new LocalFileSpanReceiver with path = " + path + ", capacity = " + capacity); } - this.processId = new ProcessId(conf); + this.tracerId = new TracerId(conf); } /** @@ -137,8 +137,8 @@ public class LocalFileSpanReceiver implements SpanReceiver { @Override public void receiveSpan(Span span) { - if (span.getProcessId().isEmpty()) { - span.setProcessId(processId.get()); + if (span.getTracerId().isEmpty()) { + span.setTracerId(tracerId.get()); } // Serialize the span data into a byte[]. Note that we're not holding the http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/959a6ef0/htrace-core/src/main/java/org/apache/htrace/impl/MilliSpan.java ---------------------------------------------------------------------- diff --git a/htrace-core/src/main/java/org/apache/htrace/impl/MilliSpan.java b/htrace-core/src/main/java/org/apache/htrace/impl/MilliSpan.java index ffec117..3f6e700 100644 --- a/htrace-core/src/main/java/org/apache/htrace/impl/MilliSpan.java +++ b/htrace-core/src/main/java/org/apache/htrace/impl/MilliSpan.java @@ -62,7 +62,7 @@ public class MilliSpan implements Span { private long parents[]; private final long spanId; private Map<String, String> traceInfo = null; - private String processId; + private String tracerId; private List<TimelineAnnotation> timeline = null; private static long nonZeroRandom64() { @@ -83,7 +83,7 @@ public class MilliSpan implements Span { traceId(traceId). parents(new long[] {spanId}). spanId(nonZeroRandom64()). - processId(processId). + tracerId(tracerId). build(); } @@ -98,7 +98,7 @@ public class MilliSpan implements Span { private long parents[] = EMPTY_PARENT_ARRAY; private long spanId; private Map<String, String> traceInfo = null; - private String processId = EMPTY_STRING; + private String tracerId = EMPTY_STRING; private List<TimelineAnnotation> timeline = null; public Builder() { @@ -148,8 +148,8 @@ public class MilliSpan implements Span { return this; } - public Builder processId(String processId) { - this.processId = processId; + public Builder tracerId(String tracerId) { + this.tracerId = tracerId; return this; } @@ -171,7 +171,7 @@ public class MilliSpan implements Span { this.parents = EMPTY_PARENT_ARRAY; this.spanId = 0; this.traceInfo = null; - this.processId = EMPTY_STRING; + this.tracerId = EMPTY_STRING; this.timeline = null; } @@ -183,7 +183,7 @@ public class MilliSpan implements Span { this.parents = builder.parents; this.spanId = builder.spanId; this.traceInfo = builder.traceInfo; - this.processId = builder.processId; + this.tracerId = builder.tracerId; this.timeline = builder.timeline; } @@ -287,13 +287,13 @@ public class MilliSpan implements Span { } @Override - public String getProcessId() { - return processId; + public String getTracerId() { + return tracerId; } @Override - public void setProcessId(String processId) { - this.processId = processId; + public void setTracerId(String tracerId) { + this.tracerId = tracerId; } @Override @@ -341,7 +341,7 @@ public class MilliSpan implements Span { } JsonNode rNode = root.get("r"); if (rNode != null) { - builder.processId(rNode.asText()); + builder.tracerId(rNode.asText()); } JsonNode parentsNode = root.get("p"); LinkedList<Long> parents = new LinkedList<Long>(); http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/959a6ef0/htrace-core/src/main/java/org/apache/htrace/impl/ProcessId.java ---------------------------------------------------------------------- diff --git a/htrace-core/src/main/java/org/apache/htrace/impl/ProcessId.java b/htrace-core/src/main/java/org/apache/htrace/impl/ProcessId.java deleted file mode 100644 index a6a954d..0000000 --- a/htrace-core/src/main/java/org/apache/htrace/impl/ProcessId.java +++ /dev/null @@ -1,291 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.htrace.impl; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.apache.htrace.HTraceConfiguration; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.lang.management.ManagementFactory; -import java.net.InetAddress; -import java.net.NetworkInterface; -import java.net.SocketException; -import java.util.Enumeration; -import java.util.Locale; -import java.util.TreeSet; - -/** - * The HTrace process ID.<p/> - * - * HTrace process IDs are created from format strings. - * Format strings contain variables which the ProcessId class will - * replace with the correct values at runtime.<p/> - * - * <ul> - * <li>${ip}: will be replaced with an ip address.</li> - * <li>${pname}: will be replaced the process name obtained from java.</li> - * </ul><p/> - * - * For example, the string "${pname}/${ip}" will be replaced with something - * like: DataNode/192.168.0.1, assuming that the process' name is DataNode - * and its IP address is 192.168.0.1.<p/> - * - * Process ID strings can contain backslashes as escapes. - * For example, "\a" will map to "a". "\${ip}" will map to the literal - * string "${ip}", not the IP address. A backslash itself can be escaped by a - * preceding backslash. - */ -public final class ProcessId { - private static final Log LOG = LogFactory.getLog(ProcessId.class); - - /** - * The configuration key to use for process id - */ - public static final String PROCESS_ID_KEY = "process.id"; - - /** - * The default process ID to use if no other ID is configured. - */ - private static final String DEFAULT_PROCESS_ID = "${pname}/${ip}"; - - private final String processId; - - ProcessId(String fmt) { - StringBuilder bld = new StringBuilder(); - StringBuilder varBld = null; - boolean escaping = false; - int varSeen = 0; - for (int i = 0, len = fmt.length() ; i < len; i++) { - char c = fmt.charAt(i); - if (c == '\\') { - if (!escaping) { - escaping = true; - continue; - } - } - switch (varSeen) { - case 0: - if (c == '$') { - if (!escaping) { - varSeen = 1; - continue; - } - } - escaping = false; - varSeen = 0; - bld.append(c); - break; - case 1: - if (c == '{') { - if (!escaping) { - varSeen = 2; - varBld = new StringBuilder(); - continue; - } - } - escaping = false; - varSeen = 0; - bld.append("$").append(c); - break; - default: - if (c == '}') { - if (!escaping) { - String var = varBld.toString(); - bld.append(processShellVar(var)); - varBld = null; - varSeen = 0; - continue; - } - } - escaping = false; - varBld.append(c); - varSeen++; - break; - } - } - if (varSeen > 0) { - LOG.warn("Unterminated process ID substitution variable at the end " + - "of format string " + fmt); - } - this.processId = bld.toString(); - if (LOG.isTraceEnabled()) { - LOG.trace("ProcessID(fmt=" + fmt + "): computed process ID of \"" + - this.processId + "\""); - } - } - - public ProcessId(HTraceConfiguration conf) { - this(conf.get(PROCESS_ID_KEY, DEFAULT_PROCESS_ID)); - } - - private String processShellVar(String var) { - if (var.equals("pname")) { - return getProcessName(); - } else if (var.equals("ip")) { - return getBestIpString(); - } else if (var.equals("pid")) { - return Long.valueOf(getOsPid()).toString(); - } else { - LOG.warn("unknown ProcessID variable " + var); - return ""; - } - } - - static String getProcessName() { - String cmdLine = System.getProperty("sun.java.command"); - if (cmdLine != null && !cmdLine.isEmpty()) { - String fullClassName = cmdLine.split("\\s+")[0]; - String[] classParts = fullClassName.split("\\."); - cmdLine = classParts[classParts.length - 1]; - } - return (cmdLine == null || cmdLine.isEmpty()) ? "Unknown" : cmdLine; - } - - /** - * Get the best IP address that represents this node.<p/> - * - * This is complicated since nodes can have multiple network interfaces, - * and each network interface can have multiple IP addresses. What we're - * looking for here is an IP address that will serve to identify this node - * to HTrace. So we prefer site-local addresess (i.e. private ones on the - * LAN) to publicly routable interfaces. If there are multiple addresses - * to choose from, we select the one which comes first in textual sort - * order. This should ensure that we at least consistently call each node - * by a single name. - */ - static String getBestIpString() { - Enumeration<NetworkInterface> ifaces; - try { - ifaces = NetworkInterface.getNetworkInterfaces(); - } catch (SocketException e) { - LOG.error("Error getting network interfaces", e); - return "127.0.0.1"; - } - TreeSet<String> siteLocalCandidates = new TreeSet<String>(); - TreeSet<String> candidates = new TreeSet<String>(); - while (ifaces.hasMoreElements()) { - NetworkInterface iface = ifaces.nextElement(); - for (Enumeration<InetAddress> addrs = - iface.getInetAddresses(); addrs.hasMoreElements();) { - InetAddress addr = addrs.nextElement(); - if (!addr.isLoopbackAddress()) { - if (addr.isSiteLocalAddress()) { - siteLocalCandidates.add(addr.getHostAddress()); - } else { - candidates.add(addr.getHostAddress()); - } - } - } - } - if (!siteLocalCandidates.isEmpty()) { - return siteLocalCandidates.first(); - } - if (!candidates.isEmpty()) { - return candidates.first(); - } - return "127.0.0.1"; - } - - /** - * Get the process id from the operating system.<p/> - * - * Unfortunately, there is no simple method to get the process id in Java. - * The approach we take here is to use the shell method (see - * {ProcessId#getOsPidFromShellPpid}) unless we are on Windows, where the - * shell is not available. On Windows, we use - * {ProcessId#getOsPidFromManagementFactory}, which depends on some - * undocumented features of the JVM, but which doesn't require a shell. - */ - static long getOsPid() { - if ((System.getProperty("os.name", "generic").toLowerCase(Locale.ENGLISH)). - contains("windows")) { - return getOsPidFromManagementFactory(); - } else { - return getOsPidFromShellPpid(); - } - } - - /** - * Get the process ID by executing a shell and printing the PPID (parent - * process ID).<p/> - * - * This method of getting the process ID doesn't depend on any undocumented - * features of the virtual machine, and should work on almost any UNIX - * operating system. - */ - private static long getOsPidFromShellPpid() { - Process p = null; - StringBuilder sb = new StringBuilder(); - try { - p = new ProcessBuilder("/usr/bin/env", "sh", "-c", "echo $PPID"). - redirectErrorStream(true).start(); - BufferedReader reader = new BufferedReader( - new InputStreamReader(p.getInputStream())); - String line = ""; - while ((line = reader.readLine()) != null) { - sb.append(line.trim()); - } - int exitVal = p.waitFor(); - if (exitVal != 0) { - throw new IOException("Process exited with error code " + - Integer.valueOf(exitVal).toString()); - } - } catch (InterruptedException e) { - LOG.error("Interrupted while getting operating system pid from " + - "the shell.", e); - return 0L; - } catch (IOException e) { - LOG.error("Error getting operating system pid from the shell.", e); - return 0L; - } finally { - if (p != null) { - p.destroy(); - } - } - try { - return Long.parseLong(sb.toString()); - } catch (NumberFormatException e) { - LOG.error("Error parsing operating system pid from the shell.", e); - return 0L; - } - } - - /** - * Get the process ID by looking at the name of the managed bean for the - * runtime system of the Java virtual machine.<p/> - * - * Although this is undocumented, in the Oracle JVM this name is of the form - * [OS_PROCESS_ID]@[HOSTNAME]. - */ - private static long getOsPidFromManagementFactory() { - try { - return Long.parseLong(ManagementFactory.getRuntimeMXBean(). - getName().split("@")[0]); - } catch (NumberFormatException e) { - LOG.error("Failed to get the operating system process ID from the name " + - "of the managed bean for the JVM.", e); - return 0L; - } - } - - public String get() { - return processId; - } -} http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/959a6ef0/htrace-core/src/main/java/org/apache/htrace/impl/TracerId.java ---------------------------------------------------------------------- diff --git a/htrace-core/src/main/java/org/apache/htrace/impl/TracerId.java b/htrace-core/src/main/java/org/apache/htrace/impl/TracerId.java new file mode 100644 index 0000000..83fa558 --- /dev/null +++ b/htrace-core/src/main/java/org/apache/htrace/impl/TracerId.java @@ -0,0 +1,291 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.htrace.impl; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.htrace.HTraceConfiguration; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.lang.management.ManagementFactory; +import java.net.InetAddress; +import java.net.NetworkInterface; +import java.net.SocketException; +import java.util.Enumeration; +import java.util.Locale; +import java.util.TreeSet; + +/** + * The HTrace tracer ID.<p/> + * + * HTrace tracer IDs are created from format strings. + * Format strings contain variables which the TracerId class will + * replace with the correct values at runtime.<p/> + * + * <ul> + * <li>${ip}: will be replaced with an ip address.</li> + * <li>${pname}: will be replaced the process name obtained from java.</li> + * </ul><p/> + * + * For example, the string "${pname}/${ip}" will be replaced with something + * like: DataNode/192.168.0.1, assuming that the process' name is DataNode + * and its IP address is 192.168.0.1.<p/> + * + * Process ID strings can contain backslashes as escapes. + * For example, "\a" will map to "a". "\${ip}" will map to the literal + * string "${ip}", not the IP address. A backslash itself can be escaped by a + * preceding backslash. + */ +public final class TracerId { + private static final Log LOG = LogFactory.getLog(TracerId.class); + + /** + * The configuration key to use for process id + */ + public static final String TRACER_ID_KEY = "process.id"; + + /** + * The default process ID to use if no other ID is configured. + */ + private static final String DEFAULT_TRACER_ID = "${pname}/${ip}"; + + private final String tracerId; + + TracerId(String fmt) { + StringBuilder bld = new StringBuilder(); + StringBuilder varBld = null; + boolean escaping = false; + int varSeen = 0; + for (int i = 0, len = fmt.length() ; i < len; i++) { + char c = fmt.charAt(i); + if (c == '\\') { + if (!escaping) { + escaping = true; + continue; + } + } + switch (varSeen) { + case 0: + if (c == '$') { + if (!escaping) { + varSeen = 1; + continue; + } + } + escaping = false; + varSeen = 0; + bld.append(c); + break; + case 1: + if (c == '{') { + if (!escaping) { + varSeen = 2; + varBld = new StringBuilder(); + continue; + } + } + escaping = false; + varSeen = 0; + bld.append("$").append(c); + break; + default: + if (c == '}') { + if (!escaping) { + String var = varBld.toString(); + bld.append(processShellVar(var)); + varBld = null; + varSeen = 0; + continue; + } + } + escaping = false; + varBld.append(c); + varSeen++; + break; + } + } + if (varSeen > 0) { + LOG.warn("Unterminated process ID substitution variable at the end " + + "of format string " + fmt); + } + this.tracerId = bld.toString(); + if (LOG.isTraceEnabled()) { + LOG.trace("ProcessID(fmt=" + fmt + "): computed process ID of \"" + + this.tracerId + "\""); + } + } + + public TracerId(HTraceConfiguration conf) { + this(conf.get(TRACER_ID_KEY, DEFAULT_TRACER_ID)); + } + + private String processShellVar(String var) { + if (var.equals("pname")) { + return getProcessName(); + } else if (var.equals("ip")) { + return getBestIpString(); + } else if (var.equals("pid")) { + return Long.valueOf(getOsPid()).toString(); + } else { + LOG.warn("unknown ProcessID variable " + var); + return ""; + } + } + + static String getProcessName() { + String cmdLine = System.getProperty("sun.java.command"); + if (cmdLine != null && !cmdLine.isEmpty()) { + String fullClassName = cmdLine.split("\\s+")[0]; + String[] classParts = fullClassName.split("\\."); + cmdLine = classParts[classParts.length - 1]; + } + return (cmdLine == null || cmdLine.isEmpty()) ? "Unknown" : cmdLine; + } + + /** + * Get the best IP address that represents this node.<p/> + * + * This is complicated since nodes can have multiple network interfaces, + * and each network interface can have multiple IP addresses. What we're + * looking for here is an IP address that will serve to identify this node + * to HTrace. So we prefer site-local addresess (i.e. private ones on the + * LAN) to publicly routable interfaces. If there are multiple addresses + * to choose from, we select the one which comes first in textual sort + * order. This should ensure that we at least consistently call each node + * by a single name. + */ + static String getBestIpString() { + Enumeration<NetworkInterface> ifaces; + try { + ifaces = NetworkInterface.getNetworkInterfaces(); + } catch (SocketException e) { + LOG.error("Error getting network interfaces", e); + return "127.0.0.1"; + } + TreeSet<String> siteLocalCandidates = new TreeSet<String>(); + TreeSet<String> candidates = new TreeSet<String>(); + while (ifaces.hasMoreElements()) { + NetworkInterface iface = ifaces.nextElement(); + for (Enumeration<InetAddress> addrs = + iface.getInetAddresses(); addrs.hasMoreElements();) { + InetAddress addr = addrs.nextElement(); + if (!addr.isLoopbackAddress()) { + if (addr.isSiteLocalAddress()) { + siteLocalCandidates.add(addr.getHostAddress()); + } else { + candidates.add(addr.getHostAddress()); + } + } + } + } + if (!siteLocalCandidates.isEmpty()) { + return siteLocalCandidates.first(); + } + if (!candidates.isEmpty()) { + return candidates.first(); + } + return "127.0.0.1"; + } + + /** + * Get the process id from the operating system.<p/> + * + * Unfortunately, there is no simple method to get the process id in Java. + * The approach we take here is to use the shell method (see + * {TracerId#getOsPidFromShellPpid}) unless we are on Windows, where the + * shell is not available. On Windows, we use + * {TracerId#getOsPidFromManagementFactory}, which depends on some + * undocumented features of the JVM, but which doesn't require a shell. + */ + static long getOsPid() { + if ((System.getProperty("os.name", "generic").toLowerCase(Locale.ENGLISH)). + contains("windows")) { + return getOsPidFromManagementFactory(); + } else { + return getOsPidFromShellPpid(); + } + } + + /** + * Get the process ID by executing a shell and printing the PPID (parent + * process ID).<p/> + * + * This method of getting the process ID doesn't depend on any undocumented + * features of the virtual machine, and should work on almost any UNIX + * operating system. + */ + private static long getOsPidFromShellPpid() { + Process p = null; + StringBuilder sb = new StringBuilder(); + try { + p = new ProcessBuilder("/usr/bin/env", "sh", "-c", "echo $PPID"). + redirectErrorStream(true).start(); + BufferedReader reader = new BufferedReader( + new InputStreamReader(p.getInputStream())); + String line = ""; + while ((line = reader.readLine()) != null) { + sb.append(line.trim()); + } + int exitVal = p.waitFor(); + if (exitVal != 0) { + throw new IOException("Process exited with error code " + + Integer.valueOf(exitVal).toString()); + } + } catch (InterruptedException e) { + LOG.error("Interrupted while getting operating system pid from " + + "the shell.", e); + return 0L; + } catch (IOException e) { + LOG.error("Error getting operating system pid from the shell.", e); + return 0L; + } finally { + if (p != null) { + p.destroy(); + } + } + try { + return Long.parseLong(sb.toString()); + } catch (NumberFormatException e) { + LOG.error("Error parsing operating system pid from the shell.", e); + return 0L; + } + } + + /** + * Get the process ID by looking at the name of the managed bean for the + * runtime system of the Java virtual machine.<p/> + * + * Although this is undocumented, in the Oracle JVM this name is of the form + * [OS_PROCESS_ID]@[HOSTNAME]. + */ + private static long getOsPidFromManagementFactory() { + try { + return Long.parseLong(ManagementFactory.getRuntimeMXBean(). + getName().split("@")[0]); + } catch (NumberFormatException e) { + LOG.error("Failed to get the operating system process ID from the name " + + "of the managed bean for the JVM.", e); + return 0L; + } + } + + public String get() { + return tracerId; + } +} http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/959a6ef0/htrace-core/src/test/java/org/apache/htrace/impl/TestLocalFileSpanReceiver.java ---------------------------------------------------------------------- diff --git a/htrace-core/src/test/java/org/apache/htrace/impl/TestLocalFileSpanReceiver.java b/htrace-core/src/test/java/org/apache/htrace/impl/TestLocalFileSpanReceiver.java index 634bef8..311ddc6 100644 --- a/htrace-core/src/test/java/org/apache/htrace/impl/TestLocalFileSpanReceiver.java +++ b/htrace-core/src/test/java/org/apache/htrace/impl/TestLocalFileSpanReceiver.java @@ -57,7 +57,7 @@ public class TestLocalFileSpanReceiver { confMap.put(LocalFileSpanReceiver.PATH_KEY, traceFileName); confMap.put(SpanReceiverBuilder.SPAN_RECEIVER_CONF_KEY, LocalFileSpanReceiver.class.getName()); - confMap.put(ProcessId.PROCESS_ID_KEY, "testPid"); + confMap.put(TracerId.TRACER_ID_KEY, "testTrid"); SpanReceiver rcvr = new SpanReceiverBuilder(HTraceConfiguration.fromMap(confMap)) .logErrors(false).build(); @@ -70,6 +70,6 @@ public class TestLocalFileSpanReceiver { ObjectMapper mapper = new ObjectMapper(); MilliSpan span = mapper.readValue(new File(traceFileName), MilliSpan.class); assertEquals("testWriteToLocalFile", span.getDescription()); - assertEquals("testPid", span.getProcessId()); + assertEquals("testTrid", span.getTracerId()); } } http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/959a6ef0/htrace-core/src/test/java/org/apache/htrace/impl/TestMilliSpan.java ---------------------------------------------------------------------- diff --git a/htrace-core/src/test/java/org/apache/htrace/impl/TestMilliSpan.java b/htrace-core/src/test/java/org/apache/htrace/impl/TestMilliSpan.java index 2e0edde..74ee562 100644 --- a/htrace-core/src/test/java/org/apache/htrace/impl/TestMilliSpan.java +++ b/htrace-core/src/test/java/org/apache/htrace/impl/TestMilliSpan.java @@ -39,7 +39,7 @@ public class TestMilliSpan { assertEquals(expected.getDescription(), got.getDescription()); assertEquals(expected.getTraceId(), got.getTraceId()); assertEquals(expected.getSpanId(), got.getSpanId()); - assertEquals(expected.getProcessId(), got.getProcessId()); + assertEquals(expected.getTracerId(), got.getTracerId()); assertTrue(Arrays.equals(expected.getParents(), got.getParents())); Map<String, String> expectedT = expected.getKVAnnotations(); Map<String, String> gotT = got.getKVAnnotations(); @@ -75,7 +75,7 @@ public class TestMilliSpan { begin(123L). end(456L). parents(new long[] { 7L }). - processId("b2404.halxg.com:8080"). + tracerId("b2404.halxg.com:8080"). spanId(989L). traceId(444).build(); String json = span.toJson(); @@ -90,7 +90,7 @@ public class TestMilliSpan { begin(-1L). end(-1L). parents(new long[] { -1L }). - processId("b2404.halxg.com:8080"). + tracerId("b2404.halxg.com:8080"). spanId(-1L). traceId(-1L).build(); String json = span.toJson(); @@ -106,7 +106,7 @@ public class TestMilliSpan { begin(random.nextLong()). end(random.nextLong()). parents(new long[] { random.nextLong() }). - processId("b2404.halxg.com:8080"). + tracerId("b2404.halxg.com:8080"). spanId(random.nextLong()). traceId(random.nextLong()).build(); String json = span.toJson(); @@ -121,7 +121,7 @@ public class TestMilliSpan { begin(300). end(400). parents(new long[] { }). - processId("b2408.halxg.com:8080"). + tracerId("b2408.halxg.com:8080"). spanId(111111111L). traceId(4443); Map<String, String> traceInfo = new HashMap<String, String>();
