diff --git a/src/tracer.cc b/src/tracer.cc index 72b4094021b..73508684c75 100644 --- a/src/tracer.cc +++ b/src/tracer.cc @@ -380,6 +380,53 @@ TraceManager::Trace::InitTracer( otel_context_ = opentelemetry::context::Context({kRootSpan, root_span}); } +void +TraceManager::Trace::StartSpan( + std::string span_key, TRITONSERVER_InferenceTrace* trace, + TRITONSERVER_InferenceTraceActivity activity, uint64_t timestamp_ns, + uint64_t trace_id) +{ + // Currently, only 2 types of sub-spans are supported: + // request span and compute span. Compute span is a leaf span + // and can not be a parent of any sub-span. If parent_id==0, + // then current model is either a standalone model, or an ensemble model. + // In both cases, the parent of the new request sub-span is the kRootSpan. + // If parent_id > 0, then this is a child trace, spawned from + // the ensamble's main request. For this instance, the parent + // span is the ensembles's request span. + uint64_t parent_id; + LOG_TRITONSERVER_ERROR( + TRITONSERVER_InferenceTraceParentId(trace, &parent_id), + "getting trace parent id"); + std::string parent_span_key = + (parent_id != 0) ? kRequestSpan + std::to_string(parent_id) : kRootSpan; + + std::string display_name = "compute"; + const char* model_name; + if (activity == TRITONSERVER_TRACE_REQUEST_START) { + LOG_TRITONSERVER_ERROR( + TRITONSERVER_InferenceTraceModelName(trace, &model_name), + "getting model name"); + display_name = model_name; + } + + auto span = StartSpan( + display_name, timestamp_ns, false /*is_root_span*/, parent_span_key); + + if (activity == TRITONSERVER_TRACE_REQUEST_START) { + int64_t model_version; + LOG_TRITONSERVER_ERROR( + TRITONSERVER_InferenceTraceModelVersion(trace, &model_version), + "getting model version"); + span->SetAttribute("triton.model_name", model_name); + span->SetAttribute("triton.model_version", model_version); + span->SetAttribute("triton.trace_id", trace_id); + span->SetAttribute("triton.trace_parent_id", parent_id); + } + + otel_context_ = otel_context_.SetValue(span_key, span); +} + opentelemetry::nostd::shared_ptr TraceManager::Trace::StartSpan( std::string display_name, const uint64_t& raw_timestamp_ns, @@ -447,16 +494,7 @@ TraceManager::Trace::ReportToOpenTelemetry( return; } - MaybeStartSpan(current_span_key, trace, activity, timestamp_ns, id); - - AddEvent( - current_span_key, TRITONSERVER_InferenceTraceActivityString(activity), - timestamp_ns); - - if (activity == TRITONSERVER_TRACE_REQUEST_END || - activity == TRITONSERVER_TRACE_COMPUTE_END) { - EndSpan(current_span_key, timestamp_ns); - } + AddEvent(current_span_key, trace, activity, timestamp_ns, id); } std::string @@ -495,66 +533,35 @@ TraceManager::Trace::GetSpanKeyForActivity( void TraceManager::Trace::AddEvent( - std::string span_key, std::string event, uint64_t timestamp) -{ - if (otel_context_.HasKey(span_key)) { - auto span = opentelemetry::nostd::get< - opentelemetry::nostd::shared_ptr>( - otel_context_.GetValue(span_key)); - span->AddEvent(event, time_offset_ + std::chrono::nanoseconds{timestamp}); - } -} - -void -TraceManager::Trace::MaybeStartSpan( std::string span_key, TRITONSERVER_InferenceTrace* trace, TRITONSERVER_InferenceTraceActivity activity, uint64_t timestamp_ns, - uint64_t trace_id) + uint64_t id) { - if (activity != TRITONSERVER_TRACE_REQUEST_START && - activity != TRITONSERVER_TRACE_COMPUTE_START) { - return; + if (activity == TRITONSERVER_TRACE_REQUEST_START || + activity == TRITONSERVER_TRACE_COMPUTE_START) { + StartSpan(span_key, trace, activity, timestamp_ns, id); } - // Currently, only 2 types of sub-spans are supported: - // request span and compute span. Compute span is a leaf span - // and can not be a parent of any sub-span. If parent_id==0, - // then current model is either a standalone model, or an ensemble model. - // In both cases, the parent of the new request sub-span is the kRootSpan. - // If parent_id > 0, then this is a child trace, spawned from - // the ensamble's main request. For this instance, the parent - // span is the ensembles's request span. - uint64_t parent_id; - LOG_TRITONSERVER_ERROR( - TRITONSERVER_InferenceTraceParentId(trace, &parent_id), - "getting trace parent id"); - std::string parent_span_key = - (parent_id != 0) ? kRequestSpan + std::to_string(parent_id) : kRootSpan; + AddEvent( + span_key, TRITONSERVER_InferenceTraceActivityString(activity), + timestamp_ns); - std::string display_name = "compute"; - const char* model_name; - if (activity == TRITONSERVER_TRACE_REQUEST_START) { - LOG_TRITONSERVER_ERROR( - TRITONSERVER_InferenceTraceModelName(trace, &model_name), - "getting model name"); - display_name = model_name; + if (activity == TRITONSERVER_TRACE_REQUEST_END || + activity == TRITONSERVER_TRACE_COMPUTE_END) { + EndSpan(span_key, timestamp_ns); } +} - auto span = StartSpan( - display_name, timestamp_ns, false /*is_root_span*/, parent_span_key); - - if (activity == TRITONSERVER_TRACE_REQUEST_START) { - int64_t model_version; - LOG_TRITONSERVER_ERROR( - TRITONSERVER_InferenceTraceModelVersion(trace, &model_version), - "getting model version"); - span->SetAttribute("triton.model_name", model_name); - span->SetAttribute("triton.model_version", model_version); - span->SetAttribute("triton.trace_id", trace_id); - span->SetAttribute("triton.trace_parent_id", parent_id); +void +TraceManager::Trace::AddEvent( + std::string span_key, std::string event, uint64_t timestamp) +{ + if (otel_context_.HasKey(span_key)) { + auto span = opentelemetry::nostd::get< + opentelemetry::nostd::shared_ptr>( + otel_context_.GetValue(span_key)); + span->AddEvent(event, time_offset_ + std::chrono::nanoseconds{timestamp}); } - - otel_context_ = otel_context_.SetValue(span_key, span); } #endif diff --git a/src/tracer.h b/src/tracer.h index 41653ec9970..15893417f28 100644 --- a/src/tracer.h +++ b/src/tracer.h @@ -224,7 +224,26 @@ class TraceManager { // OTel context to store spans, created in the current trace opentelemetry::context::Context otel_context_; - /// Starts a span with the provided timestamp. + /// Starts a compute or request span based on `activity`. + /// For request spans, it will add the following attributes to the span: + /// `model_name`, `model_version`, `trace_id`, `parent_id`. + /// + /// \param span_key Span's key to retrieve the corresponding span from the + /// OpenTelemetry context. + /// \param trace TRITONSERVER_InferenceTrace, used to request model's name, + /// version, trace parent_id from the backend. + /// \param activity Trace activity. + /// \param timestamp_ns Steady timestamp, which is used to calculate + /// OpenTelemetry SystemTimestamp to display span on a timeline, and + /// OpenTelemetry SteadyTimestamp to calculate the duration on the span + /// with better precision. + /// \param trace_id Trace id. + void StartSpan( + std::string span_key, TRITONSERVER_InferenceTrace* trace, + TRITONSERVER_InferenceTraceActivity activity, uint64_t timestamp_ns, + uint64_t trace_id); + + /// Starts a span with the provided timestamp and name. /// /// \param display_name Span's name, which will be shown in the trace. /// \param raw_timestamp_ns Steady timestamp, which is used to calculate @@ -263,35 +282,32 @@ class TraceManager { std::string GetSpanKeyForActivity( TRITONSERVER_InferenceTraceActivity activity, uint64_t trace_id); - /// Adds event to the span. + /// Adds event to the span, which is retrieved from OpenTelemetry context + /// with the provided `span_key`. If activity is + /// TRITONSERVER_TRACE_REQUEST_START, or TRITONSERVER_TRACE_COMPUTE_START, + /// starts a new span and adds it to `otel_context_`. /// /// \param span_key Span's key to retrieve the corresponding span from the /// OpenTelemetry context. - /// \param event An event to add to the span. + /// \param trace TRITONSERVER_InferenceTrace, used to request model's name, + /// version, trace parent_id from the backend. + /// \param activity Trace activity. /// \param timestamp_ns Timestamp of the provided event. + /// \param id Trace id. void AddEvent( - std::string span_key, std::string event, uint64_t timestamp_ns); + std::string span_key, TRITONSERVER_InferenceTrace* trace, + TRITONSERVER_InferenceTraceActivity activity, uint64_t timestamp_ns, + uint64_t id); - /// If activity is TRITONSERVER_TRACE_REQUEST_START, or - /// TRITONSERVER_TRACE_COMPUTE_START, starts a new span and adds it to - /// `otel_context_`. If the newly started span is a request span, then - /// it will set `model_name`, `model_version`, `trace_id`, - /// `trace_parent_id`, as span's attributes. + /// Adds event to the OpenTelemetry span, retrieved from an OpenTelementry + /// context with the provided `span_key`. /// /// \param span_key Span's key to retrieve the corresponding span from the /// OpenTelemetry context. - /// \param trace TRITONSERVER_InferenceTrace, used to request model's name, - /// version, trace parent_id from the backend. - /// \param activity Trace activity. - /// \param timestamp_ns Steady timestamp, which is used to calculate - /// OpenTelemetry SystemTimestamp to display span on a timeline, and - /// OpenTelemetry SteadyTimestamp to calculate the duration on the span - /// with better precision. - /// \param trace_id Trace id. - void MaybeStartSpan( - std::string span_key, TRITONSERVER_InferenceTrace* trace, - TRITONSERVER_InferenceTraceActivity activity, uint64_t timestamp_ns, - uint64_t trace_id); + /// \param event An event to add to the span. + /// \param timestamp_ns Timestamp of the provided event. + void AddEvent( + std::string span_key, std::string event, uint64_t timestamp_ns); #endif };