message  Send Feedback

Trace

Trace allows you to time the beginning to end of a certain action in your app with additional metric values and attributes.

Methods


Methods

getAttribute

getAttribute(
  attribute: string
): string | null;

Returns the value of an attribute. Returns null if it does not exist.

Example

const attribute = trace.getAttribute('userId');

Parameters

  • attribute: string
    Name of the attribute to fetch the value of.

getMetric

getMetric(
  metricName: string
): number;

Gets the value of the metric with the given name in the current trace. If the metric doesn't exist, it will not be created and a 0 is returned.

Example

const metric = trace.getMetric('hits');

Parameters

  • metricName: string
    Name of the metric to get.

getMetrics

getMetrics(): { [key: string]: number };

Returns an object of all the currently added metrics and their number values.

Example

const metrics = trace.getMetrics();
metrics.forEach(($) => {
console.log($);
});

incrementMetric

incrementMetric(
  metricName: string,
  incrementBy: number
): any;

Increments the named metric by the incrementBy value.

If a metric with the given name doesn't exist, a new one will be created starting with the value of incrementBy.

trace.incrementMetric('hits', 1);

Parameters

  • metricName: string
    Name of the metric to increment. Must not have a leading or trailing whitespace, no leading underscore '_' character and have a max length of 32 characters.
  • incrementBy: number
    The value the metric should be incremented by.

putAttribute

putAttribute(
  attribute: string,
  value: string
): any;

Sets a String value for the specified attribute. Updates the value of the attribute if it already exists. The maximum number of attributes that can be added is 5.

Example

trace.putAttribute('userId', '123456789');

Parameters

  • attribute: string
    Name of the attribute. Max length is 40 chars.
  • value: string
    Value of the attribute. Max length is 100 chars.

putMetric

putMetric(
  metricName: string,
  value: number
): any;

Sets the value of the named metric with the provided number.

If a metric with the given name exists it will be overwritten. If a metric with the given name doesn't exist, a new one will be created.

Example

trace.putMetric('hits', 1);

Parameters

  • metricName: string
    Name of the metric to set. Must not have a leading or trailing whitespace, no leading underscore '_' character and have a max length of 32 characters.
  • value: number
    The value the metric should be set to.

removeMetric

removeMetric(
  metricName: string
): any;

Removes a metric by name if it exists.

Example

trace.removeMetric('hits');

Parameters

  • metricName: string
    Name of the metric to remove.

start

start(): Promise<null>;

Marks the start time of the trace. Does nothing if already started.

Example

const trace = firebase.perf().newTrace('example');
await trace.start();

stop

stop(): Promise<null>;

Marks the end time of the trace and queues the metric on the device for transmission. Does nothing if already stopped.

  • Example

const trace = firebase.perf().newTrace('example');
await trace.start();
trace.putMetric('hits', 1);
await trace.stop();

React Native Market

Looking for pre-built React Native apps to kick start your next project? Check out the React Native Market by Invertase.

Your purchases help support our open-source projects such as React Native Firebase. All items are currently 50% off to celebrate the release of React Native Firebase version 6.
shopping_cartVisit React Native Market