Python Tracer API

graphsignal.configure

configure(
    api_key: Optional[str] = None, 
    api_url: Optional[str] = None, 
    deployment: Optional[str] = None, 
    tags: Optional[dict[str, str]] = None,
    auto_instrument: Optional[bool] = True,
    record_payloads: Optional[bool] = True,
    profiling_rate: Optional[float] = 0.1,
    upload_on_shutdown:  Optional[bool] = True,
    debug_mode: Optional[bool] = False) -> None

Configures and initializes the Graphsignal tracer.

All arguments can also be passed via environment variables: GRAPHSIGNAL_{ARG_NAME_UPPERCASE}. To pass tags as an environment variable, use the format k1=v1,k2=v2. Arguments passed directly to the function take precedence.

Arguments:

  • api_key: The access key for communication with Graphsignal cloud.
  • api_url: Graphsignal on-premise server URL. Defaults to Graphsignal cloud.
  • deployment: A unique name to group and identify operations for a particular deployment, version or environment.
  • tags: Process tags to group and filter recorded information.
  • auto_instrument: Enable/disable automatic instrumentation of libraries and frameworks.
  • record_payloads: Enable/disable automatic recording of data, such as prompts and completions.
  • profiling_rate: Fraction of operations to be profiled when specifying with_profile=True in trace() or automatically for auto-instrumented libraries, if applicable. To disable profiling, set to 0, to profile every operation, set to 1.
  • upload_on_shutdown: Enable/disable uploading of outstanding metrics on process or tracer shutdown.
  • debug_mode: Enable/disable debug logging.

Raises:

  • ValueError - When arguments are missing or invalid.

graphsignal.set_tag

set_tag(key: str, value: Any) -> None

Set process tags to group and filter recorded data.

To remove a tag, set its value to None or use remove_tag().

Arguments:

  • key: Tag key.
  • value: Tag value. Will be converted to string using str().
  • append_uuid When True, appends a UUID to the value, e.g. for provided value myval the resulting tag value will be myval-44955cc4fc1d.

graphsignal.set_context_tag

set_context_tag(key: str, value: Any, append_uuid: Optional[bool] = False) -> None

Set context tags to group and filter recorded data. Context tags are propagated to all operation in the same context, e.g. a web application request. Process tags with same keys will be overwritten.

To remove a tag, set its value to None or use remove_context_tag().

Arguments:

  • key: Tag key.
  • value: Tag value. Will be converted to string using str().
  • append_uuid When True, appends a UUID to the value, e.g. for provided value myval the resulting tag value will be myval-44955cc4fc1d.

graphsignal.remove_context_tag

remove_context_tag(key: str) -> None

Remove a context tag.

Arguments:

  • key: Tag key.
  • value: Tag value. Will be converted to string using str().

graphsignal.trace

trace(
    operation: str,
    tags: Optional[dict[str, str]] = None,
    with_profile: Optional[bool] = False) -> Span,

Measure and record an operation.

with context manager is supported. Otherwise, stop() method should be called on the returned Span object.

Arguments:

  • operation: Operation name.
  • tags: Operation tags. Process and context tags will be added automatically.
  • with_profile: Records a CPU/GPU profile for this operation, if a profiler is available, e.g. PyTorch Profiler. Profiling is subject to sampling according to profiling_rate.

Returns:

  • Span - span object representing currently active operation, e.g. inference.

graphsignal.trace_function

trace_function(
    operation: Optional[str],
    tags: Optional[dict[str, str]] = None,
    with_profile: Optional[bool] = False)

A decorator for tracing function operation. If operation is not set, function name will be set as operation name.

@graphsignal.trace_function
def predict(x):
    return model(x)

You can set operation name and also provide tags, which will be added to each recorded operation.

@graphsignal.trace_function(operation='predict', tags=dict(t1=1))
def predict(x):
    return model(x)

Arguments:

  • operation: Operation name.
  • tags: Operation tags. Process and context tags will be added automatically.

graphsignal.score

score(
    name: str, 
    tags: Optional[dict[str, str]], 
    score: Optional[Union[int, float]] = None, 
    severity: Optional[int] = None, 
    comment: Optional[str] = None) -> None

Create a score associated with the tags.

Arguments:

  • name: Score name.
  • tags: Score tags to associate the score with, e.g. run_id. Process and context tags will be added automatically.
  • score: Score value.
  • severity: Score severity from 1 to 5, corresponding to: 1 - critical, 2 - high, 3 - medium, 4 - low, 5 - informational.
  • comment: Any comment or description for the score.

graphsignal.upload

upload(block: Optional[bool] = False) -> None

Performance data is uploaded periodically. Call this method to initiate upload.

Arguments:

  • block: If set to true True, any outstanding data will be uploaded in the current thread instead of a new thread.

graphsignal.shutdown

shutdown() -> None

Clean up and shut down the tracer.

Normally, when python scripts exists, this method is automatically called. Use this method, if you want to explicitly clean up and shut down the tracer.

graphsignal.Span

Span object represent currently active operation. It is returned by graphsignal.trace() method and should not be created directly.

graphsignal.Span.is_sampling

is_sampling() -> bool

Returns True if current operation is being recorded.

graphsignal.Span.set_tag

set_tag(key: str, value: Any) -> None

Set operation tags. Process and context tags will be added automatically.

To remove a tag, set its value to None.

Arguments:

  • key: Tag key.
  • value: Tag value. Will be converted to string using str().

graphsignal.Span.add_exception

add_exception(exc: Optional[Exception] = None, exc_info: Optional[bool] = None) -> None

When with context manager is used with graphsignal.trace() method, exceptions are automatically reported. For other cases, use this method.

Arguments:

  • exc: Exception object.
  • exc_info: When True, the sys.exc_info() will be called internally.

graphsignal.Span.set_usage

set_usage(name: str, value: Union[int, float]) -> None

Set usage parameters, e.g. 'cost'

Arguments:

  • name: Usage counter name.
  • value: Usage counter value.

graphsignal.Span.inc_usage

inc_usage(name: str, value: Union[int, float]) -> None

Increment usage parameters, e.g. 'cost'

Arguments:

  • name: Usage counter name.
  • value: Usage counter value.

graphsignal.Span.set_payload

set_payload(
    name: str, 
    content: Any) -> None

Record any data related to current operation, e.g. input, output.

with graphsignal.trace('generate') as span:
    span.set_payload('input', input_data)

To append to data, use graphsignal.Span.append_payload with same parameters instead. content must support the + operator.

Arguments:

  • name: Payload name, e.g. 'input'
  • content: Set additional usage counts, e.g. token_count.

graphsignal.Span.set_profile

set_profile(
    name: str, 
    format: str,
    content: str) -> None

Record profiles related to current operation, e.g. CPU profile.

with graphsignal.trace('generate') as span:
    span.set_profile('chrome-trace', trace_data)

Arguments:

  • name: Payload name, e.g. 'input'
  • format: Profile format supported by Graphsignal, e.g. event-averages.
  • content: Profile contents.

graphsignal.Span.score

score(
    name: str, 
    score: Union[int, float], 
    severity: Optional[int] = None, 
    comment: Optional[str] = None) -> None

Create a score associated with the span.

Arguments:

  • name: Score name.
  • score: Score value.
  • severity: Score severity from 1 to 5, corresponding to: 1 - info, 2 - low, 3 - medium, 4 - high, 5 - critical.
  • comment: Any comment or description for the score.

graphsignal.Span.trace

trace(
    operation: str,
    tags: Optional[dict[str, str]] = None) -> Span

Measure and record sub-operations.

with context manager is supported. Otherwise, stop() method should be called on the returned Span object.

Arguments:

  • operation: Operation name.
  • tags: Operation tags. Process and context tags will be added automatically.

Returns:

  • Span - span object representing currently active operation, e.g. inference.

graphsignal.Span.stop

stop() -> None

Stops measuring and tracing current operation, if tracing is active. This method is automatically called if with context manager is used around the code.