diff --git a/plugins/ekuiper/plugin_ekuiper.c b/plugins/ekuiper/plugin_ekuiper.c index b1d93e3dd..582f0144e 100644 --- a/plugins/ekuiper/plugin_ekuiper.c +++ b/plugins/ekuiper/plugin_ekuiper.c @@ -316,7 +316,7 @@ static int ekuiper_plugin_request(neu_plugin_t * plugin, neu_otel_trace_ctx trace = NULL; neu_otel_scope_ctx scope = NULL; - if (neu_otel_control_is_started()) { + if (neu_otel_control_is_started() && header->ctx) { trace = neu_otel_find_trace(header->ctx); if (trace) { scope = neu_otel_add_span(trace); diff --git a/plugins/mqtt/mqtt_plugin_intf.c b/plugins/mqtt/mqtt_plugin_intf.c index f08621d9f..e5372a77e 100644 --- a/plugins/mqtt/mqtt_plugin_intf.c +++ b/plugins/mqtt/mqtt_plugin_intf.c @@ -463,7 +463,7 @@ int mqtt_plugin_request(neu_plugin_t *plugin, neu_reqresp_head_t *head, neu_otel_trace_ctx trace = NULL; neu_otel_scope_ctx scope = NULL; char new_span_id[36] = { 0 }; - if (neu_otel_control_is_started()) { + if (neu_otel_control_is_started() && head->ctx) { trace = neu_otel_find_trace(head->ctx); if (trace) { scope = neu_otel_add_span(trace); diff --git a/plugins/restful/rest.c b/plugins/restful/rest.c index bd1d7dded..e70184280 100644 --- a/plugins/restful/rest.c +++ b/plugins/restful/rest.c @@ -176,7 +176,7 @@ static int dashb_plugin_request(neu_plugin_t * plugin, neu_otel_trace_ctx trace = NULL; neu_otel_scope_ctx scope = NULL; - if (neu_otel_control_is_started()) { + if (neu_otel_control_is_started() && header->ctx) { trace = neu_otel_find_trace(header->ctx); if (trace) { scope = neu_otel_add_span(trace); diff --git a/src/otel/otel_manager.c b/src/otel/otel_manager.c index 0df7b5828..53f9c11a4 100644 --- a/src/otel/otel_manager.c +++ b/src/otel/otel_manager.c @@ -458,7 +458,16 @@ neu_otel_trace_ctx neu_otel_find_trace(void *req_ctx) pthread_mutex_unlock(&table_mutex); if (find) { - return (void *) find->ctx; + if (find->ctx->final && + find->ctx->span_num == + find->ctx->trace_data.resource_spans[0] + ->scope_spans[0] + ->n_spans && + find->ctx->expected_span_num <= 0) { + return NULL; + } else { + return (void *) find->ctx; + } } else { return NULL; } @@ -932,8 +941,10 @@ int neu_otel_trace_pack(neu_otel_trace_ctx ctx, uint8_t *out) void neu_otel_new_span_id(char *id) { + int64_t p_id = (int64_t) pthread_self(); for (int i = SPAN_ID_LENGTH - 1; i >= 0; i--) { - id[i] = ID_CHARSET[rand() % 16]; + int64_t rand_id = (int64_t) rand() + neu_time_ns() + p_id; + id[i] = ID_CHARSET[rand_id % 16]; } id[SPAN_ID_LENGTH] = '\0'; } @@ -1009,6 +1020,8 @@ static int otel_timer_cb(void *data) HASH_DEL(traces_table, el); neu_otel_free_trace(el->ctx); free(el); + } else { + break; } } else if (neu_time_ms() - el->ctx->ts >= TRACE_TIME_OUT) { nlog_debug("trace:%s time out", (char *) el->ctx->trace_id); @@ -1035,7 +1048,7 @@ void neu_otel_start() param.usr_data = NULL; param.second = 0; - param.millisecond = 100; + param.millisecond = 80; param.cb = otel_timer_cb; param.type = NEU_EVENT_TIMER_BLOCK;