Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(performance): add missing methods (e. g. setAttribute(...), getAttribute(...)) #770

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 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
5 changes: 5 additions & 0 deletions .changeset/chilly-seahorses-design.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@capacitor-firebase/performance': minor
---

feat: implement `putAttribute`, `getAttribute`, `getAttributes`, `putMetric`, `getMetric` and `record`
ebarooni marked this conversation as resolved.
Show resolved Hide resolved
213 changes: 213 additions & 0 deletions packages/performance/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,15 @@ const isEnabled = async () => {
* [`incrementMetric(...)`](#incrementmetric)
* [`setEnabled(...)`](#setenabled)
* [`isEnabled()`](#isenabled)
* [`putAttribute(...)`](#putattribute)
* [`getAttribute(...)`](#getattribute)
* [`getAttributes(...)`](#getattributes)
* [`removeAttribute(...)`](#removeattribute)
* [`putMetric(...)`](#putmetric)
* [`getMetric(...)`](#getmetric)
* [`record(...)`](#record)
* [Interfaces](#interfaces)
* [Type Aliases](#type-aliases)

</docgen-index>

Expand Down Expand Up @@ -160,6 +168,131 @@ Determines whether performance monitoring is enabled or disabled.
--------------------


### putAttribute(...)

```typescript
putAttribute(options: PutAttributeOptions) => Promise<void>
```

Sets a custom attribute of a trace to a given value.

| Param | Type |
| ------------- | ------------------------------------------------------------------- |
| **`options`** | <code><a href="#putattributeoptions">PutAttributeOptions</a></code> |

**Since:** 6.3.0

--------------------


### getAttribute(...)

```typescript
getAttribute(options: GetAttributeOptions) => Promise<GetAttributeResult>
```

Returns the value of a custom attribute of a trace.

| Param | Type |
| ------------- | ------------------------------------------------------------------- |
| **`options`** | <code><a href="#getattributeoptions">GetAttributeOptions</a></code> |

**Returns:** <code>Promise&lt;<a href="#getattributeresult">GetAttributeResult</a>&gt;</code>

**Since:** 6.3.0

--------------------


### getAttributes(...)

```typescript
getAttributes(options: GetAttributesOptions) => Promise<GetAttributesResult>
```

Gets the all the custom attributes of a trace with their values.

| Param | Type |
| ------------- | --------------------------------------------------------------------- |
| **`options`** | <code><a href="#getattributesoptions">GetAttributesOptions</a></code> |

**Returns:** <code>Promise&lt;<a href="#getattributesresult">GetAttributesResult</a>&gt;</code>

**Since:** 6.3.0

--------------------


### removeAttribute(...)

```typescript
removeAttribute(options: RemoveAttributeOptions) => Promise<void>
```

Removes a custom attribute from a trace given its name.

| Param | Type |
| ------------- | ------------------------------------------------------------------- |
| **`options`** | <code><a href="#getattributeoptions">GetAttributeOptions</a></code> |

**Since:** 6.3.0

--------------------


### putMetric(...)

```typescript
putMetric(options: PutMetricOptions) => Promise<void>
```

Sets the value of a custom metric.

| Param | Type |
| ------------- | ------------------------------------------------------------- |
| **`options`** | <code><a href="#putmetricoptions">PutMetricOptions</a></code> |

**Since:** 6.3.0

--------------------


### getMetric(...)

```typescript
getMetric(options: GetMetricOptions) => Promise<GetMetricResult>
```

Get the value of a custom metric by name.

| Param | Type |
| ------------- | ------------------------------------------------------------- |
| **`options`** | <code><a href="#getmetricoptions">GetMetricOptions</a></code> |

**Returns:** <code>Promise&lt;<a href="#getmetricresult">GetMetricResult</a>&gt;</code>

**Since:** 6.3.0

--------------------


### record(...)

```typescript
record(options: RecordOptions) => Promise<void>
```

Records a trace given its name and options.

| Param | Type |
| ------------- | ------------------------------------------------------- |
| **`options`** | <code><a href="#recordoptions">RecordOptions</a></code> |

**Since:** 6.3.0

--------------------


### Interfaces


Expand Down Expand Up @@ -199,6 +332,86 @@ Determines whether performance monitoring is enabled or disabled.
| ------------- | -------------------- | --------------------------------------------------------------- | ----- |
| **`enabled`** | <code>boolean</code> | `true` if performance monitoring is enabled, otherwise `false`. | 0.1.0 |


#### PutAttributeOptions

| Prop | Type | Description | Since |
| --------------- | ------------------- | --------------------------------------- | ----- |
| **`traceName`** | <code>string</code> | Name of the trace to set its attribute. | 6.3.0 |
| **`attribute`** | <code>string</code> | Name of the attribute to set its value. | 6.3.0 |
| **`value`** | <code>string</code> | The value to set to the attribute. | 6.3.0 |


#### GetAttributeResult

| Prop | Type | Description | Since |
| ----------- | --------------------------- | ---------------------------------- | ----- |
| **`value`** | <code>string \| null</code> | The value of the custom attribute. | 6.3.0 |


#### GetAttributeOptions

| Prop | Type | Description | Since |
| --------------- | ------------------- | -------------------------------------------- | ----- |
| **`traceName`** | <code>string</code> | Name of the trace to set its attribute. | 6.3.0 |
| **`attribute`** | <code>string</code> | Name of the attribute to retrieve its value. | 6.3.0 |


#### GetAttributesResult

| Prop | Type | Description | Since |
| ------------ | --------------------------------------- | ------------------------------------------------------------ | ----- |
| **`result`** | <code>{ [key: string]: string; }</code> | A map of all custom attributes of a trace with their values. | 6.3.0 |


#### GetAttributesOptions

| Prop | Type | Description | Since |
| --------------- | ------------------- | ---------------------------------------- | ----- |
| **`traceName`** | <code>string</code> | Name of the trace to get its attributes. | 6.3.0 |


#### PutMetricOptions

| Prop | Type | Description | Since |
| ---------------- | ------------------- | ---------------------------------------------------------------------------------------- | ----- |
| **`traceName`** | <code>string</code> | Name of the trace to set its metric. | 6.3.0 |
| **`metricName`** | <code>string</code> | The metric name. | 6.3.0 |
| **`num`** | <code>number</code> | The value to set for the metric. The given value is floored down to the nearest integer. | 6.3.0 |


#### GetMetricResult

| Prop | Type | Description | Since |
| ----------- | --------------------------- | ---------------------------------- | ----- |
| **`value`** | <code>number \| null</code> | The value of the metric if exists. | 6.3.0 |


#### GetMetricOptions

| Prop | Type | Description | Since |
| ---------------- | ------------------- | ------------------------------------ | ----- |
| **`traceName`** | <code>string</code> | Name of the trace to get its metric. | 6.3.0 |
| **`metricName`** | <code>string</code> | The metric name. | 6.3.0 |


#### RecordOptions

| Prop | Type | Description | Since |
| --------------- | ----------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------- | ----- |
| **`traceName`** | <code>string</code> | Name of the trace to record. | 6.3.0 |
| **`startTime`** | <code>number</code> | Start time of the trace since epoch in milliseconds. | 6.3.0 |
| **`duration`** | <code>number</code> | The duration of the trace in milliseconds. | 6.3.0 |
| **`options`** | <code>{ metrics?: { [key: string]: number; }; attributes?: { [key: string]: string; }; }</code> | An optional object that hold optional maps of custom metrics and attributes. | 6.3.0 |


### Type Aliases


#### RemoveAttributeOptions

<code><a href="#getattributeoptions">GetAttributeOptions</a></code>

</docgen-api>

## Changelog
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
package io.capawesome.capacitorjs.plugins.firebase.performance;

import androidx.annotation.Nullable;
import com.google.firebase.perf.metrics.Trace;
import io.capawesome.capacitorjs.plugins.firebase.performance.classes.results.GetAttributeResult;
import io.capawesome.capacitorjs.plugins.firebase.performance.classes.results.GetAttributesResult;
import io.capawesome.capacitorjs.plugins.firebase.performance.classes.results.GetMetricResult;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class FirebasePerformance {

private HashMap<String, Trace> traces = new HashMap<String, Trace>();
private ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);

public void startTrace(String traceName) {
Trace trace = this.getFirebasePerformanceInstance().newTrace(traceName);
Expand Down Expand Up @@ -39,6 +48,61 @@ public Boolean isEnabled() {
return this.getFirebasePerformanceInstance().isPerformanceCollectionEnabled();
}

public static void putAttribute(Trace trace, String attribute, String value) {
trace.putAttribute(attribute, value);
}

public static GetAttributeResult getAttribute(Trace trace, String attribute) {
return new GetAttributeResult(trace.getAttribute(attribute));
}

public static GetAttributesResult getAttributes(Trace trace) {
return new GetAttributesResult(trace.getAttributes());
}

public static void removeAttribute(Trace trace, String attribute) {
trace.removeAttribute(attribute);
return;
}

public static void putMetric(Trace trace, String metricName, long num) {
trace.putMetric(metricName, num);
}

public static GetMetricResult getMetric(Trace trace, String metricName) {
return new GetMetricResult(trace.getLongMetric(metricName));
}

public void record(
Trace trace,
String traceName,
long startTime,
long duration,
@Nullable Map<String, String> attributes,
@Nullable Map<String, Long> metrics
) {
long currentTime = System.currentTimeMillis();
long startDelay = Math.max(0, (startTime - currentTime));
if (attributes != null) {
for (String key : attributes.keySet()) {
FirebasePerformance.putAttribute(trace, key, attributes.get(key));
}
}
if (metrics != null) {
for (String key : metrics.keySet()) {
FirebasePerformance.putMetric(trace, key, metrics.get(key));
}
}
this.scheduler.schedule(
() -> {
this.startTrace(traceName);
scheduler.schedule(() -> this.stopTrace(traceName), duration, TimeUnit.MILLISECONDS);
},
startDelay,
TimeUnit.MILLISECONDS
);
}

private com.google.firebase.perf.FirebasePerformance getFirebasePerformanceInstance() {
return com.google.firebase.perf.FirebasePerformance.getInstance();
}
Expand Down
Loading
Loading