Skip to content

Performance Monitoring reference

At a glance

  • Performance.performance(): trace(name), start_trace(name), data_collection_enabled, instrumentation_enabled
  • Trace: name, start(), stop(), running, active, increment_metric(name, by=1), set_metric(name, value), get_metric(name), put_attribute(name, value), get_attribute(name), remove_attribute(name), attributes; a context manager
  • HTTPMetric(url, method): start(), stop(), running, active, response_code, request_payload_size, response_payload_size, response_content_type, the attribute methods; a context manager; HTTPMethod

pyios_firebase.performance

Firebase Performance Monitoring: custom code traces and network requests.

from pyios_firebase.performance import HTTPMethod, HTTPMetric, Performance

with Performance.performance().trace("load_feed") as trace:
    items = load_feed()
    trace.increment_metric("items", len(items))
    trace.put_attribute("source", "cache")

metric = HTTPMetric("https://api.example.com/feed", HTTPMethod.GET)
metric.start()
...                                   # make the request
metric.response_code = 200
metric.response_payload_size = len(body)
metric.stop()

The SDK measures app start, screen rendering and the requests made with NSURLSession by itself. Requests made from Python (urllib, requests, httpx...) do not use NSURLSession: measure them with HTTPMetric.

Bad names and values raise ValueError (the SDK would drop the trace, metric or attribute quietly). While data collection is off, traces and HTTP metrics are inactive (active is False) and do nothing.

Needs the FirebasePerformance framework.

Classes:

Name Description
HTTPMethod

The method of an HTTPMetric's request (FIRHTTPMethod).

Trace

A custom code trace: the time between start and stop, with your

HTTPMetric

A network request you measure yourself (for requests made from Python,

Performance

Performance Monitoring. Get it with Performance.performance().

HTTPMethod

The method of an HTTPMetric's request (FIRHTTPMethod).

Trace

Trace(native: ObjCObject | None, name: str)

Bases: _Timed

A custom code trace: the time between start and stop, with your own metrics (counters) and attributes. Get it from Performance.

Use it with with to start and stop it::

with Performance.performance().trace("checkout") as trace:
    trace.increment_metric("items", 3)

Parameters:

Name Type Description Default
native ObjCObject | None

the native FIRTrace, or None (inactive).

required
name str

the trace's name.

required

Methods:

Name Description
increment_metric

Add to a metric (a metric starts at 0).

set_metric

Set a metric.

get_metric

A metric's value (0 if it was never set).

Attributes:

Name Type Description
name str

The trace's name.

name property

name: str

The trace's name.

increment_metric

increment_metric(name: str, by: int = 1) -> None

Add to a metric (a metric starts at 0).

Parameters:

Name Type Description Default
name str

the metric's name (as for trace names).

required
by int

the amount to add (negative to subtract).

1

Raises:

Type Description
ValueError

an invalid name.

RuntimeError

the trace is not running.

set_metric

set_metric(name: str, value: int) -> None

Set a metric.

Parameters:

Name Type Description Default
name str

the metric's name.

required
value int

the value.

required

Raises:

Type Description
ValueError

an invalid name.

RuntimeError

the trace is not running.

get_metric

get_metric(name: str) -> int

A metric's value (0 if it was never set).

Parameters:

Name Type Description Default
name str

the metric's name.

required

HTTPMetric

HTTPMetric(url: str, method: int = GET)

Bases: _Timed

A network request you measure yourself (for requests made from Python, which the SDK does not see). Start it before sending the request, set the response fields, then stop it.

Parameters:

Name Type Description Default
url str

the request's http or https URL.

required
method int

an HTTPMethod.

GET

Raises:

Type Description
ValueError

not an http(s) URL, or an unknown method.

Attributes:

Name Type Description
url str

The request's URL.

response_code int

The HTTP status code (set it before stop).

request_payload_size int

The request body's size in bytes.

response_payload_size int

The response body's size in bytes.

response_content_type str | None

The response's Content-Type, for example "application/json".

url property

url: str

The request's URL.

response_code property writable

response_code: int

The HTTP status code (set it before stop).

request_payload_size property writable

request_payload_size: int

The request body's size in bytes.

response_payload_size property writable

response_payload_size: int

The response body's size in bytes.

response_content_type property writable

response_content_type: str | None

The response's Content-Type, for example "application/json".

Performance

Performance(native: ObjCObject)

Performance Monitoring. Get it with Performance.performance().

Parameters:

Name Type Description Default
native ObjCObject

the native FIRPerformance.

required

Methods:

Name Description
performance

The shared Performance.

trace

A new trace, not started (use with, or start() / stop()).

start_trace

A new trace, already started.

Attributes:

Name Type Description
native ObjCObject

The FIRPerformance.

data_collection_enabled bool

Collect and send data (custom traces too). Setting it is kept across

instrumentation_enabled bool

Measure app start, screens and NSURLSession requests automatically.

native property

native: ObjCObject

The FIRPerformance.

data_collection_enabled property writable

data_collection_enabled: bool

Collect and send data (custom traces too). Setting it is kept across launches; to start with it off, set firebase_performance_collection_enabled to NO in Info.plist.

instrumentation_enabled property writable

instrumentation_enabled: bool

Measure app start, screens and NSURLSession requests automatically. Takes effect at the next launch; firebase_performance_instrumentation_enabled in Info.plist sets it at build time.

performance classmethod

performance() -> Performance

The shared Performance.

trace

trace(name: str) -> Trace

A new trace, not started (use with, or start() / stop()).

Parameters:

Name Type Description Default
name str

1 to 100 characters, not starting with _.

required

Returns:

Type Description
Trace

A Trace (inactive when data collection is off).

Raises:

Type Description
ValueError

an invalid name.

RuntimeError

Firebase is not configured yet (the SDK would abort the app).

start_trace

start_trace(name: str) -> Trace

A new trace, already started.

Parameters:

Name Type Description Default
name str

as for trace.

required

Returns:

Type Description
Trace

The running Trace.

Raises:

Type Description
ValueError

an invalid name.

RuntimeError

Firebase is not configured yet.