Skip to content

Commit

Permalink
baggage 替换 tracestate
Browse files Browse the repository at this point in the history
  • Loading branch information
kilingzhang committed Dec 22, 2021
1 parent 71fed1f commit 74e2833
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 77 deletions.
2 changes: 1 addition & 1 deletion include/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ void opentelemetry_request_shutdown();
/**
*
*/
void start_tracer(std::string traceparent, std::string tracestate, opentelemetry::proto::trace::v1::Span_SpanKind kind);
void start_tracer(std::string traceparent, std::string tracebaggage, opentelemetry::proto::trace::v1::Span_SpanKind kind);

/**
*
Expand Down
12 changes: 6 additions & 6 deletions include/provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ class Provider {

void clean();

void addTraceStates(const std::string &key, const std::string &value);
void addBaggage(const std::string &key, const std::string &value);

tsl::robin_map<std::string, std::string> getTraceStates();
tsl::robin_map<std::string, std::string> getBaggage();

void parseTraceParent(const std::string &traceparent);
void parseTraceState(const std::string &tracestate);
void parseBaggage(const std::string &tracebaggage);

std::string formatTraceParentHeader(opentelemetry::proto::trace::v1::Span *span);
static std::string formatTraceStateHeader();
std::string formatBaggageHeader();

static void okEnd(opentelemetry::proto::trace::v1::Span *span);
static void errorEnd(opentelemetry::proto::trace::v1::Span *span, const std::string &message);
Expand All @@ -72,8 +72,8 @@ class Provider {
tsl::robin_map<pid_t, bool> sampled_map;
tsl::robin_map<pid_t, std::string> redisExceptions;
std::string redisException;
tsl::robin_map<std::string, std::string> states;
tsl::robin_map<pid_t, tsl::robin_map<std::string, std::string>> states_map;
tsl::robin_map<std::string, std::string> baggage;
tsl::robin_map<pid_t, tsl::robin_map<std::string, std::string>> baggage_map;
};

#endif //OPENTELEMETRY_TRACING_H
4 changes: 2 additions & 2 deletions include/sdk.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ PHP_FUNCTION (opentelemetry_shutdown_cli_tracer);

PHP_FUNCTION (opentelemetry_get_traceparent);

PHP_FUNCTION (opentelemetry_get_tracestate);
PHP_FUNCTION (opentelemetry_get_baggage);

PHP_FUNCTION (opentelemetry_add_tracestate);
PHP_FUNCTION (opentelemetry_add_baggage);

PHP_FUNCTION (opentelemetry_set_sample_ratio_based);

Expand Down
4 changes: 2 additions & 2 deletions opentelemetry.cc
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ const zend_function_entry opentelemetry_functions[] = {
PHP_FE(opentelemetry_start_cli_tracer, NULL)
PHP_FE(opentelemetry_shutdown_cli_tracer, NULL)
PHP_FE(opentelemetry_get_traceparent, NULL)
PHP_FE(opentelemetry_add_tracestate, NULL)
PHP_FE(opentelemetry_get_tracestate, NULL)
PHP_FE(opentelemetry_add_baggage, NULL)
PHP_FE(opentelemetry_get_baggage, NULL)
PHP_FE(opentelemetry_set_sample_ratio_based, NULL)
PHP_FE(opentelemetry_get_service_name, NULL)
PHP_FE(opentelemetry_get_service_ip, NULL)
Expand Down
12 changes: 5 additions & 7 deletions src/core.cc
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ void opentelemetry_request_shutdown() {
shutdown_tracer();
}

void start_tracer(std::string traceparent, std::string tracestate, opentelemetry::proto::trace::v1::Span_SpanKind kind) {
void start_tracer(std::string traceparent, std::string tracebaggage, opentelemetry::proto::trace::v1::Span_SpanKind kind) {
if ((is_cli_sapi() && !is_cli_enabled()) || (is_cli_sapi() && is_cli_enabled() && !is_started_cli_tracer())) {
return;
}
Expand All @@ -217,8 +217,6 @@ void start_tracer(std::string traceparent, std::string tracestate, opentelemetry
OPENTELEMETRY_G(provider) = new Provider();
}

init_consumers();

if (is_has_provider()) {
OPENTELEMETRY_G(provider)->clean();
OPENTELEMETRY_G(provider)->increase();
Expand Down Expand Up @@ -248,7 +246,7 @@ void start_tracer(std::string traceparent, std::string tracestate, opentelemetry
}
auto span = OPENTELEMETRY_G(provider)->createFirstSpan(uri, kind);
OPENTELEMETRY_G(provider)->parseTraceParent(traceparent);
OPENTELEMETRY_G(provider)->parseTraceState(tracestate);
OPENTELEMETRY_G(provider)->parseBaggage(tracebaggage);
set_string_attribute(span->add_attributes(), "process.executable.name", uri);
set_string_attribute(span->add_attributes(), "process.command_args", opentelemetry_json_encode(&copy_value));
set_string_attribute(span->add_attributes(), "process.pid", std::to_string(getpid()));
Expand All @@ -264,10 +262,10 @@ void start_tracer(std::string traceparent, std::string tracestate, opentelemetry
uri = request_uri;
}
traceparent = find_server_string("HTTP_TRACEPARENT", sizeof("HTTP_TRACEPARENT") - 1);
tracestate = find_server_string("HTTP_TRACESTATE", sizeof("HTTP_TRACESTATE") - 1);
tracebaggage = find_server_string("HTTP_BAGGAGE", sizeof("HTTP_BAGGAGE") - 1);
auto span = OPENTELEMETRY_G(provider)->createFirstSpan(uri, kind);
OPENTELEMETRY_G(provider)->parseTraceParent(traceparent);
OPENTELEMETRY_G(provider)->parseTraceState(tracestate);
OPENTELEMETRY_G(provider)->parseBaggage(tracebaggage);
std::string request_method = find_server_string("REQUEST_METHOD", sizeof("REQUEST_METHOD") - 1);
std::string request_query = find_server_string("QUERY_STRING", sizeof("QUERY_STRING") - 1);
std::string request_http_host = find_server_string("HTTP_HOST", sizeof("HTTP_HOST") - 1);
Expand All @@ -292,7 +290,7 @@ void start_tracer(std::string traceparent, std::string tracestate, opentelemetry
}
uri.shrink_to_fit();
traceparent.shrink_to_fit();
tracestate.shrink_to_fit();
tracebaggage.shrink_to_fit();

}

Expand Down
71 changes: 37 additions & 34 deletions src/provider.cc
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ void Provider::clean() {
delete request;
request = nullptr;
is_sampled = false;
states.clear();
baggage.clear();
redisException.clear();
redisException.shrink_to_fit();
}
Expand All @@ -187,51 +187,34 @@ void Provider::clean() {
delete requests[ppid];
requests.erase(ppid);
}
if (states_map.find(ppid) != states_map.end()) {
states_map[ppid].clear();
if (baggage_map.find(ppid) != baggage_map.end()) {
baggage_map[ppid].clear();
}
if (redisExceptions.find(ppid) != redisExceptions.end()) {
redisExceptions.erase(ppid);
}
}
}

void Provider::addTraceStates(const std::string &key, const std::string &value) {
tsl::robin_map<std::string, std::string> traceStates;
std::string statesHeader;
void Provider::addBaggage(const std::string &key, const std::string &value) {
if (!is_cli_sapi()) {
states[key] = value;
traceStates = states;
baggage[key] = value;
} else {
pid_t ppid = get_current_ppid();
states_map[ppid][key] = value;
traceStates = states_map[ppid];
baggage_map[ppid][key] = value;
}
set_string_attribute(getResource()->add_attributes(), key, value);
auto iter = traceStates.begin();
bool first = true;
while (iter != traceStates.end()) {
if (!first) {
statesHeader = statesHeader.append(",");
}
first = false;
statesHeader = statesHeader.append(iter->first);
statesHeader = statesHeader.append("=");
statesHeader = statesHeader.append(iter->second);
iter++;
}
OPENTELEMETRY_G(provider)->firstOneSpan()->set_trace_state(statesHeader);
}

tsl::robin_map<std::string, std::string> Provider::getTraceStates() {
tsl::robin_map<std::string, std::string> Provider::getBaggage() {
if (!is_cli_sapi()) {
return states;
return baggage;
} else {
pid_t ppid = get_current_ppid();
if (states_map.find(ppid) == states_map.end()) {
states_map[ppid] = tsl::robin_map<std::string, std::string>();
if (baggage_map.find(ppid) == baggage_map.end()) {
baggage_map[ppid] = tsl::robin_map<std::string, std::string>();
}
return states_map[ppid];
return baggage_map[ppid];
}
}

Expand All @@ -253,12 +236,12 @@ void Provider::parseTraceParent(const std::string &traceparent) {
}
}

void Provider::parseTraceState(const std::string &tracestate) {
if (!tracestate.empty()) {
for (const auto &item: explode(",", tracestate)) {
void Provider::parseBaggage(const std::string &tracebaggage) {
if (!tracebaggage.empty()) {
for (const auto &item: explode(",", tracebaggage)) {
std::vector<std::string> kv = explode("=", item);
if (kv.size() == 2) {
addTraceStates(kv[0], kv[1]);
addBaggage(kv[0], kv[1]);
}
}
}
Expand All @@ -268,8 +251,28 @@ std::string Provider::formatTraceParentHeader(Span *span) {
return "00-" + traceId(*span) + "-" + spanId(*span) + "-" + (isSampled() ? "01" : "00");
}

std::string Provider::formatTraceStateHeader() {
return OPENTELEMETRY_G(provider)->firstOneSpan()->trace_state();
std::string Provider::formatBaggageHeader() {
tsl::robin_map<std::string, std::string> baggageValue;
std::string baggageHeader;
if (!is_cli_sapi()) {
baggageValue = baggage;
} else {
pid_t ppid = get_current_ppid();
baggageValue = baggage_map[ppid];
}
auto iter = baggageValue.begin();
bool first = true;
while (iter != baggageValue.end()) {
if (!first) {
baggageHeader = baggageHeader.append(",");
}
first = false;
baggageHeader = baggageHeader.append(iter->first);
baggageHeader = baggageHeader.append("=");
baggageHeader = baggageHeader.append(iter->second);
iter++;
}
return baggageHeader;
}

void Provider::okEnd(Span *span) {
Expand Down
26 changes: 13 additions & 13 deletions src/sdk.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,23 @@ PHP_FUNCTION (opentelemetry_start_cli_tracer) {

char *traceparent = nullptr;
size_t traceparent_len;
char *tracestate = nullptr;
size_t tracestate_len;
char *baggage = nullptr;
size_t baggage_len;
long kind = 0;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|ssl",
&traceparent, &traceparent_len, &tracestate, &tracestate_len, &kind) == FAILURE) {
&traceparent, &traceparent_len, &baggage, &baggage_len, &kind) == FAILURE) {
RETURN_FALSE;
}

OPENTELEMETRY_G(is_started_cli_tracer) = true;

if (traceparent && tracestate) {
start_tracer(traceparent, tracestate, (Span_SpanKind) kind);
} else if (traceparent && !tracestate) {
if (traceparent && baggage) {
start_tracer(traceparent, baggage, (Span_SpanKind) kind);
} else if (traceparent && !baggage) {
start_tracer(traceparent, "", (Span_SpanKind) kind);
} else if (!traceparent && tracestate) {
start_tracer("", tracestate, (Span_SpanKind) kind);
} else if (!traceparent && baggage) {
start_tracer("", baggage, (Span_SpanKind) kind);
} else {
start_tracer("", "", (Span_SpanKind) kind);
}
Expand Down Expand Up @@ -85,15 +85,15 @@ PHP_FUNCTION (opentelemetry_get_traceparent) {
* @param execute_data
* @param return_value
*/
PHP_FUNCTION (opentelemetry_get_tracestate) {
PHP_FUNCTION (opentelemetry_get_baggage) {
if (is_has_provider()) {
std::string tracestate = Provider::formatTraceStateHeader();
RETURN_STRING(string2char(tracestate));
std::string baggage = OPENTELEMETRY_G(provider)->formatBaggageHeader();
RETURN_STRING(string2char(baggage));
}
RETURN_STRING("");
}

PHP_FUNCTION (opentelemetry_add_tracestate) {
PHP_FUNCTION (opentelemetry_add_baggage) {

if (is_has_provider()) {
char *key = nullptr;
Expand All @@ -106,7 +106,7 @@ PHP_FUNCTION (opentelemetry_add_tracestate) {
RETURN_FALSE;
}

OPENTELEMETRY_G(provider)->addTraceStates(key, value);
OPENTELEMETRY_G(provider)->addBaggage(key, value);

RETURN_TRUE;
}
Expand Down
8 changes: 4 additions & 4 deletions src/zend_hook_curl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,13 @@ void opentelemetry_curl_exec_handler(INTERNAL_FUNCTION_PARAMETERS) {

std::string traceparent = OPENTELEMETRY_G(provider)->formatTraceParentHeader(span);
add_next_index_string(option, ("traceparent: " + traceparent).c_str());
std::string tracestate = Provider::formatTraceStateHeader();
add_next_index_string(option, ("tracestate: " + tracestate).c_str());
std::string baggage = OPENTELEMETRY_G(provider)->formatBaggageHeader();
add_next_index_string(option, ("baggage: " + baggage).c_str());

// set_string_attribute(span->add_attributes(), "http.header", opentelemetry_json_encode(option));
set_string_attribute(span->add_attributes(), "http.header", opentelemetry_json_encode(option));

traceparent.shrink_to_fit();
tracestate.shrink_to_fit();
baggage.shrink_to_fit();

zval argv[3];
zval ret;
Expand Down
16 changes: 8 additions & 8 deletions src/zend_hook_yar.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ void opentelemetry_yar_client_handler(INTERNAL_FUNCTION_PARAMETERS) {
function_name = std::string(ZSTR_VAL(zf->internal_function.function_name));
}
name = find_trace_add_scope_name(zf->internal_function.function_name, zf->internal_function.scope,
zf->internal_function.fn_flags);
zf->internal_function.fn_flags);

std::string cmd = function_name;
std::transform(function_name.begin(), function_name.end(), cmd.begin(), ::tolower);
Expand Down Expand Up @@ -63,8 +63,8 @@ void opentelemetry_yar_client_handler(INTERNAL_FUNCTION_PARAMETERS) {
array_init(yar_headers);
std::string traceparent = OPENTELEMETRY_G(provider)->formatTraceParentHeader(span);
add_next_index_string(yar_headers, ("traceparent: " + traceparent).c_str());
std::string tracestate = Provider::formatTraceStateHeader();
add_next_index_string(yar_headers, ("tracestate: " + tracestate).c_str());
std::string baggage = OPENTELEMETRY_G(provider)->formatBaggageHeader();
add_next_index_string(yar_headers, ("baggage: " + baggage).c_str());
ZVAL_COPY(&params[1], yar_headers);

zend_call_method(obj, Z_OBJCE_P(self), nullptr, ZEND_STRL("setopt"), nullptr, 2, &params[0], &params[1]);
Expand Down Expand Up @@ -158,7 +158,7 @@ void opentelemetry_yar_server_handler(INTERNAL_FUNCTION_PARAMETERS) {
function_name = std::string(ZSTR_VAL(zf->internal_function.function_name));
}
name = find_trace_add_scope_name(zf->internal_function.function_name, zf->internal_function.scope,
zf->internal_function.fn_flags);
zf->internal_function.fn_flags);

std::string cmd = function_name;
std::transform(function_name.begin(), function_name.end(), cmd.begin(), ::tolower);
Expand Down Expand Up @@ -206,7 +206,7 @@ void register_zend_hook_yar() {
zend_function *old_function;

if ((old_class = OPENTELEMETRY_OLD_CN("yar_client")) != nullptr) {
for (const auto &item : clientKeysCommands) {
for (const auto &item: clientKeysCommands) {
if ((old_function = OPENTELEMETRY_OLD_FN_TABLE(
&old_class->function_table, item.c_str())) !=
nullptr) {
Expand All @@ -217,7 +217,7 @@ void register_zend_hook_yar() {
}

if ((old_class = OPENTELEMETRY_OLD_CN("yar_server")) != nullptr) {
for (const auto &item : serverKeysCommands) {
for (const auto &item: serverKeysCommands) {
if ((old_function = OPENTELEMETRY_OLD_FN_TABLE(
&old_class->function_table, item.c_str())) !=
nullptr) {
Expand All @@ -232,7 +232,7 @@ void unregister_zend_hook_yar() {
zend_class_entry *old_class;
zend_function *old_function;
if ((old_class = OPENTELEMETRY_OLD_CN("yar_client")) != nullptr) {
for (const auto &item : clientKeysCommands) {
for (const auto &item: clientKeysCommands) {
if ((old_function = OPENTELEMETRY_OLD_FN_TABLE(
&old_class->function_table, item.c_str())) !=
nullptr) {
Expand All @@ -242,7 +242,7 @@ void unregister_zend_hook_yar() {
}

if ((old_class = OPENTELEMETRY_OLD_CN("yar_server")) != nullptr) {
for (const auto &item : serverKeysCommands) {
for (const auto &item: serverKeysCommands) {
if ((old_function = OPENTELEMETRY_OLD_FN_TABLE(
&old_class->function_table, item.c_str())) !=
nullptr) {
Expand Down

0 comments on commit 74e2833

Please sign in to comment.