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 5 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
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>{ [k: 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
45 changes: 45 additions & 0 deletions packages/performance/ios/Plugin/FirebasePerformance.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,49 @@ import FirebasePerformance
@objc public func isEnabled() -> Bool {
return Performance.sharedInstance().isDataCollectionEnabled
}

@objc public static func putAttribute(_ trace: Trace, _ attribute: String, _ value: String) {
trace.setValue(value, forAttribute: attribute)
}

@objc public static func getAttribute(_ trace: Trace, _ attribute: String) -> String? {
return trace.value(forAttribute: attribute)
}

@objc public static func getAttributes(_ trace: Trace) -> [String: String] {
return trace.attributes
}

@objc public static func removeAttribute(_ trace: Trace, _ attribute: String) {
trace.removeAttribute(attribute)
}

@objc public static func putMetric(_ trace: Trace, _ metricName: String, _ num: Double) {
trace.setValue(Int64(floor(num)), forMetric: metricName)
}

@objc public static func getMetric(_ trace: Trace, _ metricName: String) -> Int64 {
return trace.valueForMetric(metricName)
}

@objc public func record(_ traceName: String, _ startTime: Double, _ duration: Double, _ attributes: [String: String], _ metrics: [String: Double]) {
let trace = getTraceByName(traceName)
let currentTime = Date().timeIntervalSince1970 * 1000
let startDelay = max(0, (startTime - currentTime) / 1000)

DispatchQueue.global().asyncAfter(deadline: .now() + startDelay) {
for (key, value) in attributes {
FirebasePerformance.putAttribute(trace!, key, value)
}
for (key, value) in metrics {
FirebasePerformance.putMetric(trace!, key, value)
}
self.startTrace(traceName)

DispatchQueue.global().asyncAfter(deadline: .now() + duration / 1000) {
self.stopTrace(traceName)
}
}
return
ebarooni marked this conversation as resolved.
Show resolved Hide resolved
}
}
7 changes: 7 additions & 0 deletions packages/performance/ios/Plugin/FirebasePerformancePlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,11 @@
CAP_PLUGIN_METHOD(incrementMetric, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(setEnabled, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(isEnabled, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(putAttribute, CAPPluginReturnNone);
CAP_PLUGIN_METHOD(getAttribute, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(getAttributes, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(removeAttribute, CAPPluginReturnNone);
CAP_PLUGIN_METHOD(putMetric, CAPPluginReturnNone);
CAP_PLUGIN_METHOD(getMetric, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(record, CAPPluginReturnNone);
)
Loading
Loading