Skip to content

Commit

Permalink
Revised according to comments
Browse files Browse the repository at this point in the history
  • Loading branch information
oandreeva-nv committed Jul 6, 2023
1 parent 9d6970c commit dcea33a
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 82 deletions.
129 changes: 68 additions & 61 deletions src/tracer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<otel_trace_api::Span>
TraceManager::Trace::StartSpan(
std::string display_name, const uint64_t& raw_timestamp_ns,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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_trace_api::Span>>(
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_trace_api::Span>>(
otel_context_.GetValue(span_key));
span->AddEvent(event, time_offset_ + std::chrono::nanoseconds{timestamp});
}

otel_context_ = otel_context_.SetValue(span_key, span);
}
#endif

Expand Down
58 changes: 37 additions & 21 deletions src/tracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
};

Expand Down

0 comments on commit dcea33a

Please sign in to comment.