Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exposing trace context to python backend #6985

Merged
merged 8 commits into from
Mar 15, 2024

Conversation

oandreeva-nv
Copy link
Contributor

@oandreeva-nv oandreeva-nv commented Mar 14, 2024

The purpose of this PR:
This PR is a Stage 1 towards adding an ability for users to create custom traces in python backend.
This set of PRs is heavily focused on OpenTelemetry trace mode.

Functionality:

  • Prepares OpenTelemetry trace context via PrepareTraceContext and attaches it to TRITONSERVER_InferenceTrace instance through TRITONSERVER_InferenceTraceSetContex at the moment in time, when REQUEST_START callback happens. That one is happening from the core side, so when PBE model starts execution, it should have context attached to its trace.
  • Context is passed as a string and for OpenTelemetry is essentially 2 headers traceparent : "..." and tracestate: "...". Some of you may know about them from OpenTelemetry context propagation discussions. It's the same.
  • We do nothing with trace Context in Triton mode. On the core side TRITONSERVER_InferenceTrace is initiated with an empty string as a context, since it is never updated, pbe will get an empty string and return None with request.trace().get_context()

Below is a POC illustrated.

mode=triton tests are on the way.

Related PRs:
python_be: triton-inference-server/python_backend#346
core: triton-inference-server/core#334

POC:
image

created with model.py:

import time
import json

import numpy as np
import triton_python_backend_utils as pb_utils

from opentelemetry import trace
from opentelemetry.propagate import inject
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import (
    BatchSpanProcessor
)
from opentelemetry.trace.propagation.tracecontext import TraceContextTextMapPropagator
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter



class TritonPythonModel:

    def initialize(self, args):
    	trace.set_tracer_provider(TracerProvider())
    	self.tracer = trace.get_tracer_provider().get_tracer("pbe")
    	trace.get_tracer_provider().add_span_processor(
	    BatchSpanProcessor(OTLPSpanExporter(endpoint="http://localhost:4318/v1/traces"))
	)

    def execute(self, requests):
        logger = pb_utils.Logger
        logger.log_info(f"got {len(requests)} requests")

        responses = []
        for request in requests:
            inp = pb_utils.get_input_tensor_by_name(request, "INPUT0").as_numpy()
            context = request.trace().get_context()
            if context == None:
            	context = "{}"
            ctx = TraceContextTextMapPropagator().extract(carrier=json.loads(context))

            # emulate some work
            with self.tracer.start_as_current_span('child', context=ctx) as span:
            	time.sleep(3)
            
            time.sleep(3)

            output_tensors = [pb_utils.Tensor("OUTPUT0", inp.astype(np.float32))]
            inference_response = pb_utils.InferenceResponse(output_tensors=output_tensors)
            responses.append(inference_response)

        return responses

@oandreeva-nv oandreeva-nv marked this pull request as ready for review March 14, 2024 19:52
rmccorm4
rmccorm4 previously approved these changes Mar 15, 2024
Copy link
Contributor

@rmccorm4 rmccorm4 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work 🚀

@oandreeva-nv oandreeva-nv merged commit 06b73f3 into main Mar 15, 2024
3 checks passed
@oandreeva-nv oandreeva-nv deleted the oandreeva_expose_otel_context_pb branch March 15, 2024 21:52
oandreeva-nv added a commit that referenced this pull request Mar 15, 2024
* Added TRITONSERVER_InferenceTraceSetContext logic
GuanLuo pushed a commit that referenced this pull request Mar 15, 2024
* Added TRITONSERVER_InferenceTraceSetContext logic
@@ -0,0 +1,46 @@
import numpy as np
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@oandreeva-nv do we need a copyright statement for this file?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, will follow up shortly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants