[perf] Break out Trace/HttpMetric classes

This commit is contained in:
Elliot Hesp 2018-07-10 20:12:01 +01:00
parent 90bbb972ea
commit 8f6299f07f
4 changed files with 170 additions and 7 deletions

View File

@ -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<string> {
return getNativeModule(this._perf).getHttpMetricAttribute(
this.url,
this.httpMethod,
attribute
);
}
getAttributes(): Promise<Object> {
return getNativeModule(this._perf).getHttpMetricAttributes(
this.url,
this.httpMethod
);
}
putAttribute(attribute: string, value: string): Promise<null> {
return getNativeModule(this._perf).putHttpMetricAttribute(
this.url,
this.httpMethod,
attribute,
value
);
}
removeAttribute(attribute: string): Promise<null> {
return getNativeModule(this._perf).removeHttpMetricAttribute(
this.url,
this.httpMethod,
attribute
);
}
setHttpResponseCode(code: number): Promise<null> {
return getNativeModule(this._perf).setHttpMetricResponseCode(
this.url,
this.httpMethod,
code
);
}
setRequestPayloadSize(bytes: number): Promise<null> {
return getNativeModule(this._perf).setHttpMetricRequestPayloadSize(
this.url,
this.httpMethod,
bytes
);
}
setResponseContentType(type: string): Promise<null> {
return getNativeModule(this._perf).setHttpMetricResponseContentType(
this.url,
this.httpMethod,
type
);
}
setResponsePayloadSize(bytes: number): Promise<null> {
return getNativeModule(this._perf).setHttpMetricResponsePayloadSize(
this.url,
this.httpMethod,
bytes
);
}
start(): Promise<null> {
return getNativeModule(this._perf).startHttpMetric(
this.url,
this.httpMethod
);
}
stop(): Promise<null> {
return getNativeModule(this._perf).stopHttpMetric(
this.url,
this.httpMethod
);
}
}

View File

@ -14,15 +14,60 @@ export default class Trace {
this.identifier = identifier;
}
start(): void {
getNativeModule(this._perf).start(this.identifier);
getAttribute(attribute: string): Promise<string> {
return getNativeModule(this._perf).getTraceAttribute(
this.identifier,
attribute
);
}
stop(): void {
getNativeModule(this._perf).stop(this.identifier);
getAttributes(): Promise<Object> {
return getNativeModule(this._perf).getTraceAttributes(this.identifier);
}
incrementCounter(event: string): void {
getNativeModule(this._perf).incrementCounter(this.identifier, event);
getMetric(metricName: string): Promise<number> {
return getNativeModule(this._perf).getTraceLongMetric(
this.identifier,
metricName
);
}
incrementMetric(metricName: string, incrementBy: number): Promise<null> {
return getNativeModule(this._perf).incrementTraceMetric(
this.identifier,
metricName,
incrementBy
);
}
putAttribute(attribute: string, value: string): Promise<null> {
return getNativeModule(this._perf).putTraceAttribute(
this.identifier,
attribute,
value
);
}
putMetric(metricName: string, value: number): Promise<null> {
return getNativeModule(this._perf).putTraceMetric(
this.identifier,
metricName,
value
);
}
removeAttribute(attribute: string): Promise<null> {
return getNativeModule(this._perf).removeTraceAttribute(
this.identifier,
attribute
);
}
start(): Promise<null> {
return getNativeModule(this._perf).startTrace(this.identifier);
}
stop(): Promise<null> {
return getNativeModule(this._perf).stopTrace(this.identifier);
}
}

View File

@ -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 = {};

View File

@ -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;
}