Python Manual Instrumentation
Datadog の調査レポート: サーバーレスの状態 レポート: サーバーレスの状態

Python Manual Instrumentation

このページは英語では対応しておりません。随時翻訳に取り組んでいます。翻訳に関してご質問やご意見ございましたら、お気軽にご連絡ください。

If you aren’t using supported library instrumentation (see library compatibility), you may want to manually instrument your code.

You may also want to extend the functionality of the ddtrace library or gain finer control over instrumenting your application. Several techniques are provided by the library to accomplish this.

The following examples use the global tracer object which can be imported via:

from ddtrace import tracer

ddtrace provides a decorator that can be used to trace a particular method in your application:

  @tracer.wrap()
  def business_logic():
    """A method that would be of interest to trace."""
    # ...
    # ...

API details for the decorator can be found at ddtrace.Tracer.wrap()

To trace an arbitrary block of code, you can use the ddtrace.Span context manager:

  # trace some interesting operation
  with tracer.trace('interesting.operations'):
    # do some interesting operation(s)
    # ...
    # ...

Further API details can be found at ddtrace.Tracer()

If the decorator and context manager methods are still not enough to satisfy your tracing needs, a manual API is provided which allows you to start and finish spans however you may require:

  span = tracer.trace('operations.of.interest')

  # do some operation(s) of interest in between

  # NOTE: make sure to call span.finish() or the entire trace is not sent
  # to Datadog
  span.finish()

API details of the decorator can be found here:

Further Reading