diff --git a/lib/modules/perf/HttpMetric.js b/lib/modules/perf/HttpMetric.js new file mode 100644 index 00000000..cbeeab94 --- /dev/null +++ b/lib/modules/perf/HttpMetric.js @@ -0,0 +1,96 @@ +/** + * @flow + * Trace representation wrapper + */ +import { getNativeModule } from '../../utils/native'; +import type PerformanceMonitoring from './'; + +export default class HttpMetric { + url: string; + httpMethod: string; + _perf: PerformanceMonitoring; + + constructor(perf: PerformanceMonitoring, url: string, httpMethod: string) { + this._perf = perf; + this.url = url; + this.httpMethod = httpMethod; + } + + getAttribute(attribute: string): Promise { + return getNativeModule(this._perf).getHttpMetricAttribute( + this.url, + this.httpMethod, + attribute + ); + } + + getAttributes(): Promise { + return getNativeModule(this._perf).getHttpMetricAttributes( + this.url, + this.httpMethod + ); + } + + putAttribute(attribute: string, value: string): Promise { + return getNativeModule(this._perf).putHttpMetricAttribute( + this.url, + this.httpMethod, + attribute, + value + ); + } + + removeAttribute(attribute: string): Promise { + return getNativeModule(this._perf).removeHttpMetricAttribute( + this.url, + this.httpMethod, + attribute + ); + } + + setHttpResponseCode(code: number): Promise { + return getNativeModule(this._perf).setHttpMetricResponseCode( + this.url, + this.httpMethod, + code + ); + } + + setRequestPayloadSize(bytes: number): Promise { + return getNativeModule(this._perf).setHttpMetricRequestPayloadSize( + this.url, + this.httpMethod, + bytes + ); + } + + setResponseContentType(type: string): Promise { + return getNativeModule(this._perf).setHttpMetricResponseContentType( + this.url, + this.httpMethod, + type + ); + } + + setResponsePayloadSize(bytes: number): Promise { + return getNativeModule(this._perf).setHttpMetricResponsePayloadSize( + this.url, + this.httpMethod, + bytes + ); + } + + start(): Promise { + return getNativeModule(this._perf).startHttpMetric( + this.url, + this.httpMethod + ); + } + + stop(): Promise { + return getNativeModule(this._perf).stopHttpMetric( + this.url, + this.httpMethod + ); + } +} diff --git a/lib/modules/perf/Trace.js b/lib/modules/perf/Trace.js index 5fd54608..bf784c61 100644 --- a/lib/modules/perf/Trace.js +++ b/lib/modules/perf/Trace.js @@ -14,15 +14,60 @@ export default class Trace { this.identifier = identifier; } - start(): void { - getNativeModule(this._perf).start(this.identifier); + getAttribute(attribute: string): Promise { + return getNativeModule(this._perf).getTraceAttribute( + this.identifier, + attribute + ); } - stop(): void { - getNativeModule(this._perf).stop(this.identifier); + getAttributes(): Promise { + return getNativeModule(this._perf).getTraceAttributes(this.identifier); } - incrementCounter(event: string): void { - getNativeModule(this._perf).incrementCounter(this.identifier, event); + getMetric(metricName: string): Promise { + return getNativeModule(this._perf).getTraceLongMetric( + this.identifier, + metricName + ); + } + + incrementMetric(metricName: string, incrementBy: number): Promise { + return getNativeModule(this._perf).incrementTraceMetric( + this.identifier, + metricName, + incrementBy + ); + } + + putAttribute(attribute: string, value: string): Promise { + return getNativeModule(this._perf).putTraceAttribute( + this.identifier, + attribute, + value + ); + } + + putMetric(metricName: string, value: number): Promise { + return getNativeModule(this._perf).putTraceMetric( + this.identifier, + metricName, + value + ); + } + + removeAttribute(attribute: string): Promise { + return getNativeModule(this._perf).removeTraceAttribute( + this.identifier, + attribute + ); + } + + start(): Promise { + return getNativeModule(this._perf).startTrace(this.identifier); + } + + stop(): Promise { + return getNativeModule(this._perf).stopTrace(this.identifier); } } diff --git a/lib/modules/perf/index.js b/lib/modules/perf/index.js index c22ed35e..92282167 100644 --- a/lib/modules/perf/index.js +++ b/lib/modules/perf/index.js @@ -3,6 +3,7 @@ * Performance monitoring representation wrapper */ import Trace from './Trace'; +import HttpMetric from './HttpMetric'; import ModuleBase from '../../utils/ModuleBase'; import { getNativeModule } from '../../utils/native'; @@ -27,7 +28,13 @@ export default class PerformanceMonitoring extends ModuleBase { * @returns {*} */ setPerformanceCollectionEnabled(enabled: boolean): void { - getNativeModule(this).setPerformanceCollectionEnabled(enabled); + if (typeof enabled !== 'boolean') { + throw new Error( + 'firebase.perf().setPerformanceCollectionEnabled() requires a boolean value' + ); + } + + return getNativeModule(this).setPerformanceCollectionEnabled(enabled); } /** @@ -35,8 +42,22 @@ export default class PerformanceMonitoring extends ModuleBase { * @param trace */ newTrace(trace: string): Trace { + if (typeof trace !== 'string') { + throw new Error('firebase.perf().newTrace() requires a string value'); + } + return new Trace(this, trace); } + + newHttpMetric(url: string, httpMethod: string) { + if (typeof url !== 'string' || typeof httpMethod !== 'string') { + throw new Error( + 'firebase.perf().newHttpMetric() requires url and httpMethod string values' + ); + } + + return new HttpMetric(this, url, httpMethod); + } } export const statics = {}; diff --git a/lib/utils/index.js b/lib/utils/index.js index 5680419e..6d51b6b9 100644 --- a/lib/utils/index.js +++ b/lib/utils/index.js @@ -196,6 +196,7 @@ export function noop(): void {} * @returns {*} */ export function stripTrailingSlash(str: string): string { + if (!str || !isString(str)) return str; return str.endsWith('/') ? str.slice(0, -1) : str; }