Skip to content

Commit b1ed0a4

Browse files
committed
Added comments and small refactoring.
1 parent eaf9871 commit b1ed0a4

File tree

2 files changed

+87
-36
lines changed

2 files changed

+87
-36
lines changed

src/tracer.cc

+11-11
Original file line numberDiff line numberDiff line change
@@ -436,14 +436,14 @@ TraceManager::Trace::ReportToOpenTelemetry(
436436
TRITONSERVER_InferenceTrace* trace,
437437
TRITONSERVER_InferenceTraceActivity activity, uint64_t timestamp_ns)
438438
{
439-
auto current_span_key = GetSpanNameForActivity(activity);
440-
if (current_span_key.empty()) {
441-
return;
442-
}
443439
uint64_t id;
444440
LOG_TRITONSERVER_ERROR(
445441
TRITONSERVER_InferenceTraceId(trace, &id), "getting trace id");
446-
current_span_key = current_span_key + std::to_string(id);
442+
443+
auto current_span_key = GetSpanKeyForActivity(activity, id);
444+
if (current_span_key.empty()) {
445+
return;
446+
}
447447

448448
MaybeStartSpan(current_span_key, trace, activity, timestamp_ns, id);
449449

@@ -458,23 +458,23 @@ TraceManager::Trace::ReportToOpenTelemetry(
458458
}
459459

460460
std::string
461-
TraceManager::Trace::GetSpanNameForActivity(
462-
TRITONSERVER_InferenceTraceActivity activity)
461+
TraceManager::Trace::GetSpanKeyForActivity(
462+
TRITONSERVER_InferenceTraceActivity activity, uint64_t trace_id)
463463
{
464464
std::string span_name;
465465
switch (activity) {
466466
case TRITONSERVER_TRACE_REQUEST_START:
467467
case TRITONSERVER_TRACE_QUEUE_START:
468468
case TRITONSERVER_TRACE_REQUEST_END: {
469-
span_name = kRequestSpan;
469+
span_name = kRequestSpan + std::to_string(trace_id);
470470
break;
471471
}
472472

473473
case TRITONSERVER_TRACE_COMPUTE_START:
474474
case TRITONSERVER_TRACE_COMPUTE_INPUT_END:
475475
case TRITONSERVER_TRACE_COMPUTE_OUTPUT_START:
476476
case TRITONSERVER_TRACE_COMPUTE_END: {
477-
span_name = kComputeSpan;
477+
span_name = kComputeSpan + std::to_string(trace_id);
478478
break;
479479
}
480480
case TRITONSERVER_TRACE_TENSOR_QUEUE_INPUT:
@@ -507,7 +507,7 @@ void
507507
TraceManager::Trace::MaybeStartSpan(
508508
std::string span_key, TRITONSERVER_InferenceTrace* trace,
509509
TRITONSERVER_InferenceTraceActivity activity, uint64_t timestamp_ns,
510-
uint64_t id)
510+
uint64_t trace_id)
511511
{
512512
if (activity != TRITONSERVER_TRACE_REQUEST_START &&
513513
activity != TRITONSERVER_TRACE_COMPUTE_START) {
@@ -548,7 +548,7 @@ TraceManager::Trace::MaybeStartSpan(
548548
"getting model version");
549549
span->SetAttribute("triton.model_name", model_name);
550550
span->SetAttribute("triton.model_version", model_version);
551-
span->SetAttribute("triton.trace_id", id);
551+
span->SetAttribute("triton.trace_id", trace_id);
552552
span->SetAttribute("triton.trace_parent_id", parent_id);
553553
}
554554

src/tracer.h

+76-25
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ namespace triton { namespace server {
5555
using TraceConfig = std::vector<std::pair<std::string, std::string>>;
5656
using TraceConfigMap = std::unordered_map<std::string, TraceConfig>;
5757

58-
// Common OTel span types.
58+
// Common OTel span keys to store in OTel context
59+
// with the corresponding trace id.
5960
constexpr char kRootSpan[] = "root_span";
6061
constexpr char kRequestSpan[] = "request_span";
6162
constexpr char kComputeSpan[] = "compute_span";
@@ -171,18 +172,28 @@ class TraceManager {
171172
void CaptureTimestamp(const std::string& name, uint64_t timestamp_ns);
172173

173174
#if !defined(_WIN32) && defined(TRITON_ENABLE_TRACING)
174-
// Initializes Opentelemetry exporter, processor, provider and context
175+
/// Initializes Opentelemetry exporter, processor, provider and context.
176+
///
177+
/// \param config_map A config map, which stores all parameters, specified
178+
/// by user.
175179
void InitTracer(const TraceConfigMap& config_map);
176180

177-
// Reports TRITONSERVER_InferenceTraceActivity as event to
178-
// the currently active span. If activity is an instance of
179-
// `TRITONSERVER_TRACE_REQUEST_START` or
180-
// `TRITONSERVER_TRACE_COMPUTE_START`,
181-
// it starts a new request or compute span. For the request span it
182-
// adds some triton related attributes, and adds this span to
183-
// `otel_context_`. Alternatively, if activity is
184-
// `TRITONSERVER_TRACE_REQUEST_END` or `TRITONSERVER_TRACE_COMPUTE_END`,
185-
// it ends the corresponding span.
181+
/// Reports TRITONSERVER_InferenceTraceActivity as event to
182+
/// the currently active span. If activity is an instance of
183+
/// `TRITONSERVER_TRACE_REQUEST_START` or
184+
/// `TRITONSERVER_TRACE_COMPUTE_START`,
185+
/// it starts a new request or compute span. For the request span it
186+
/// adds some triton related attributes, and adds this span to
187+
/// `otel_context_`. Alternatively, if activity is
188+
/// `TRITONSERVER_TRACE_REQUEST_END` or `TRITONSERVER_TRACE_COMPUTE_END`,
189+
/// it ends the corresponding span.
190+
///
191+
/// \param trace TRITONSERVER_InferenceTrace instance.
192+
/// \param activity Trace activity.
193+
/// \param timestamp_ns Steady timestamp, which is used to calculate
194+
/// OpenTelemetry SystemTimestamp to display span on a timeline, and
195+
/// OpenTelemetry SteadyTimestamp to calculate the duration on the span
196+
/// with better precision.
186197
void ReportToOpenTelemetry(
187198
TRITONSERVER_InferenceTrace* trace,
188199
TRITONSERVER_InferenceTraceActivity activity, uint64_t timestamp_ns);
@@ -213,34 +224,74 @@ class TraceManager {
213224
// OTel context to store spans, created in the current trace
214225
opentelemetry::context::Context otel_context_;
215226

216-
// Starts a span with the provided timestamp
227+
/// Starts a span with the provided timestamp.
228+
///
229+
/// \param display_name Span's name, which will be shown in the trace.
230+
/// \param raw_timestamp_ns Steady timestamp, which is used to calculate
231+
/// OpenTelemetry SystemTimestamp to display span on a timeline, and
232+
/// OpenTelemetry SteadyTimestamp to calculate the duration on the span
233+
/// with better precision.
234+
/// \param is_root_span If true, a root span will be started,
235+
/// i.e. with no parent span specified. If false, a new child span will
236+
/// be started.
237+
/// \param parent_span_key A span key, to find a parent span in the
238+
/// OpenTelemetry context.
239+
/// \return A shared pointer to a newly created OpenTelemetry span.
217240
opentelemetry::nostd::shared_ptr<otel_trace_api::Span> StartSpan(
218241
std::string display_name, const uint64_t& raw_timestamp_ns,
219242
bool is_root_span, std::string parent_span_key = "");
220243

221-
// Ends the provided span
244+
/// Ends the provided span.
245+
///
246+
/// \param span_key Span's key to retrieve the corresponding span from the
247+
/// OpenTelemetry context.
222248
void EndSpan(std::string span_key);
223249

224-
// Ends the provided span at specified steady timestamp
250+
/// Ends the provided span at specified steady timestamp.
251+
///
252+
/// \param span_key Span's key to retrieve the corresponding span from the
253+
/// OpenTelemetry context.
254+
/// \param raw_timestamp_ns Steady timestamp to use as
255+
/// `EndSpanOptions::end_steady_time`.
225256
void EndSpan(std::string span_key, const uint64_t& raw_timestamp_ns);
226257

227-
// Returns the span key, for which the activity belongs
228-
std::string GetSpanNameForActivity(
229-
TRITONSERVER_InferenceTraceActivity activity);
230-
231-
// Adds event to a span
258+
/// Returns the span key, for which the activity belongs.
259+
///
260+
/// \param activity reported activity.
261+
/// \param trace_id Trace id.
262+
/// \return A key to identify span, stored in the OpenTelemetry context.
263+
std::string GetSpanKeyForActivity(
264+
TRITONSERVER_InferenceTraceActivity activity, uint64_t trace_id);
265+
266+
/// Adds event to the span.
267+
///
268+
/// \param span_key Span's key to retrieve the corresponding span from the
269+
/// OpenTelemetry context.
270+
/// \param event An event to add to the span.
271+
/// \param timestamp_ns Timestamp of the provided event.
232272
void AddEvent(
233273
std::string span_key, std::string event, uint64_t timestamp_ns);
234274

235-
// If activity is TRITONSERVER_TRACE_REQUEST_START, or
236-
// TRITONSERVER_TRACE_COMPUTE_START, starts a new span and adds it to
237-
// `otel_context_`. If the newly started span is a request span, then
238-
// it will set `model_name`, `model_version`, `trace_id`, `trace_parent_id`,
239-
// as span's attributes.
275+
/// If activity is TRITONSERVER_TRACE_REQUEST_START, or
276+
/// TRITONSERVER_TRACE_COMPUTE_START, starts a new span and adds it to
277+
/// `otel_context_`. If the newly started span is a request span, then
278+
/// it will set `model_name`, `model_version`, `trace_id`,
279+
/// `trace_parent_id`, as span's attributes.
280+
///
281+
/// \param span_key Span's key to retrieve the corresponding span from the
282+
/// OpenTelemetry context.
283+
/// \param trace TRITONSERVER_InferenceTrace, used to request model's name,
284+
/// version, trace parent_id from the backend.
285+
/// \param activity Trace activity.
286+
/// \param timestamp_ns Steady timestamp, which is used to calculate
287+
/// OpenTelemetry SystemTimestamp to display span on a timeline, and
288+
/// OpenTelemetry SteadyTimestamp to calculate the duration on the span
289+
/// with better precision.
290+
/// \param trace_id Trace id.
240291
void MaybeStartSpan(
241292
std::string span_key, TRITONSERVER_InferenceTrace* trace,
242293
TRITONSERVER_InferenceTraceActivity activity, uint64_t timestamp_ns,
243-
uint64_t id);
294+
uint64_t trace_id);
244295
#endif
245296
};
246297

0 commit comments

Comments
 (0)