Skip to content
This repository was archived by the owner on Apr 4, 2023. It is now read-only.

Performance http metrics #1582

Merged
merged 2 commits into from
Apr 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions docs/PERFORMANCE_MONITORING.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,41 @@ if (firebaseTrace) {
firebaseTrace = undefined;
}
```

### `startHttpMetric`
To be able to interact with a started HTTP metric, you can remember it in a property (in this case `firebaseHttpMetric`):

```typescript
import { performance as firebasePerformance } from "nativescript-plugin-firebase";
import { FirebaseHttpMetric } from "nativescript-plugin-firebase/performance/performance";

const firebaseHttpMetric: FirebaseHttpMetric = firebasePerformance.startHttpMetric("https://postman-echo.com/get", "GET");
```

Now you can call several functions on the remembered metric object, read on below. And don't forget to use `metric.stop` afterwards.

### `metric.setRequestPayloadSize`

```typescript
if (firebaseHttpMetric) {
firebaseHttpMetric.setRequestPayloadSize(42);
}
```

### `metric.setHttpResponseCode`

```typescript
if (firebaseHttpMetric) {
firebaseHttpMetric.setHttpResponseCode(200);
}
```

### `metric.stop`
To stop the metric, call `stop` on the remembered metric object:

```typescript
if (firebaseHttpMetric) {
firebaseHttpMetric.stop();
firebaseHttpMetric = undefined;
}
```
22 changes: 22 additions & 0 deletions src/performance/performance.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ export function startTrace(name: string): FirebaseTrace {
return new FirebaseTrace(trace);
}

export function startHttpMetric(url: string, method: string) {
const httpMetric = com.google.firebase.perf.FirebasePerformance.getInstance().newHttpMetric(url, method);
httpMetric.start();
return new FirebaseHttpMetric(httpMetric);
}

export class FirebaseTrace {
constructor(private nativeTrace: com.google.firebase.perf.metrics.Trace) {
}
Expand Down Expand Up @@ -39,3 +45,19 @@ export class FirebaseTrace {
this.nativeTrace.stop();
}
}

export class FirebaseHttpMetric {
constructor(private nativeHttpMetric: com.google.firebase.perf.metrics.HttpMetric) {}

setRequestPayloadSize(size: number) {
this.nativeHttpMetric.setRequestPayloadSize(size);
}

setHttpResponseCode(responseCode: number) {
this.nativeHttpMetric.setHttpResponseCode(responseCode);
}

stop(): void {
this.nativeHttpMetric.stop();
}
}
8 changes: 8 additions & 0 deletions src/performance/performance.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,11 @@ export declare class FirebaseTrace {
}

export declare function startTrace(name: string): FirebaseTrace;

export declare class FirebaseHttpMetric {
setRequestPayloadSize(size: number): void;
setHttpResponseCode(responseCode: number): void;
stop(): void;
}

export declare function startHttpMetric(url: string, method: string): FirebaseHttpMetric;
44 changes: 44 additions & 0 deletions src/performance/performance.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ export function startTrace(name: string): FirebaseTrace {
return new FirebaseTrace(FIRPerformance.startTraceWithName(name));
}

export function startHttpMetric(url: string, method: string) {
const httpMetric = FIRHTTPMetric.alloc().initWithURLHTTPMethod(NSURL.URLWithString(url), getHttpMethodFromString(method));
httpMetric.start();
return new FirebaseHttpMetric(httpMetric);
}

export class FirebaseTrace {
constructor(private nativeTrace: FIRTrace) {
}
Expand Down Expand Up @@ -32,3 +38,41 @@ export class FirebaseTrace {
this.nativeTrace.stop();
}
}

export class FirebaseHttpMetric {
constructor(private nativeHttpMetric: FIRHTTPMetric) {
}

setRequestPayloadSize(size: number) {
this.nativeHttpMetric.requestPayloadSize = size;
}

setHttpResponseCode(responseCode: number) {
this.nativeHttpMetric.responseCode = responseCode;
}

stop(): void {
this.nativeHttpMetric.stop();
}
}

function getHttpMethodFromString(method: string): FIRHTTPMethod {
switch (method) {
case 'GET':
return FIRHTTPMethod.GET;
case 'PUT':
return FIRHTTPMethod.PUT;
case 'POST':
return FIRHTTPMethod.POST;
case 'DELETE':
return FIRHTTPMethod.DELETE;
case 'HEAD':
return FIRHTTPMethod.HEAD;
case 'PATCH':
return FIRHTTPMethod.PATCH;
case 'OPTIONS':
return FIRHTTPMethod.OPTIONS;
default:
return null;
}
}