Skip to content

Commit

Permalink
Implement the text metric for Kotlin and Swift
Browse files Browse the repository at this point in the history
  • Loading branch information
badboy committed May 27, 2022
1 parent cc73d14 commit d4e02f7
Show file tree
Hide file tree
Showing 11 changed files with 563 additions and 21 deletions.
177 changes: 158 additions & 19 deletions docs/user/reference/metrics/text.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,54 @@ Sets a text metric to a specific value.

{{#include ../../../shared/tab_header.md}}

<div data-lang="Kotlin" class="tab"></div>
<div data-lang="Kotlin" class="tab">

<div data-lang="Java" class="tab"></div>
```Kotlin
import org.mozilla.yourApplication.GleanMetrics.Article

<div data-lang="Swift" class="tab"></div>
Article.content.set(extractedText)
```

</div>

<div data-lang="Java" class="tab">

```Java
import org.mozilla.yourApplication.GleanMetrics.Article;

Article.INSTANCE.content().set(extractedText);
```

</div>

<div data-lang="Swift" class="tab">

```Swift
Article.content.set(extractedText)
```

</div>

<div data-lang="Python" class="tab">

```Python
from glean import load_metrics
metrics = load_metrics("metrics.yaml")

metrics.article.content.set(extracted_text)
```

</div>

<div data-lang="Rust" class="tab">

<div data-lang="Python" class="tab"></div>
```Rust
use glean_metrics::article;

article::conent.set(extracted_text);
```

</div>

<div data-lang="JavaScript" class="tab">

Expand All @@ -35,8 +76,6 @@ article.content.set(extractedText);
```
</div>

<div data-lang="Rust" class="tab"></div>

<div data-lang="Firefox Desktop" class="tab"></div>

{{#include ../../../shared/tab_footer.md}}
Expand All @@ -58,17 +97,60 @@ article.content.set(extractedText);

### `testGetValue`

Gets the recorded value for a given text metric.
Gets the recorded value for a given text metric.
Returns the string if data is stored.
Returns `null` if no data is stored.

{{#include ../../../shared/tab_header.md}}

<div data-lang="Kotlin" class="tab"></div>
<div data-lang="Kotlin" class="tab">

```Kotlin
import org.mozilla.yourApplication.GleanMetrics.Article

assertEquals("some content", Article.content.testGetValue())
```

</div>

<div data-lang="Java" class="tab">

```Java
import org.mozilla.yourApplication.GleanMetrics.Article;

assertEquals("some content", Article.INSTANCE.content().testGetValue());
```

</div>

<div data-lang="Swift" class="tab">

<div data-lang="Java" class="tab"></div>
```Swift
XCTAssertEqual("some content", Article.content.testGetValue())
```

</div>

<div data-lang="Swift" class="tab"></div>
<div data-lang="Python" class="tab">

<div data-lang="Python" class="tab"></div>
```Python
from glean import load_metrics
metrics = load_metrics("metrics.yaml")

assert "some content" == metrics.article.content.test_get_value()
```

</div>

<div data-lang="Rust" class="tab">

```Rust
use glean_metrics::article;

assert_eq!("some content", article::content.test_get_value(None).unwrap());
```

</div>

<div data-lang="JavaScript" class="tab">

Expand All @@ -80,8 +162,6 @@ assert.strictEqual("some content", await article.content.testGetValue());

</div>

<div data-lang="Rust" class="tab"></div>

<div data-lang="Firefox Desktop" class="tab"></div>

{{#include ../../../shared/tab_footer.md}}
Expand All @@ -92,13 +172,74 @@ Gets number of errors recorded for a given text metric.

{{#include ../../../shared/tab_header.md}}

<div data-lang="Kotlin" class="tab"></div>
<div data-lang="Kotlin" class="tab">

```Kotlin
import org.mozilla.yourApplication.GleanMetrics.Article

// Was the string truncated, and an error reported?
assertEquals(
0,
Article.content.testGetNumRecordedErrors(ErrorType.INVALID_OVERFLOW)
)
```

</div>

<div data-lang="Java" class="tab">

```Kotlin
import org.mozilla.yourApplication.GleanMetrics.Article;

// Was the string truncated, and an error reported?
assertEquals(
0,
Article.content.testGetNumRecordedErrors(ErrorType.INVALID_OVERFLOW)
);
```

</div>

<div data-lang="Swift" class="tab">

```Swift
// Was the string truncated, and an error reported?
XCTAssertEqual(0, Article.content.testGetNumRecordedErrors(.invalidOverflow))
```

</div>

<div data-lang="Python" class="tab">

```Python
from glean import load_metrics
metrics = load_metrics("metrics.yaml")

<div data-lang="Java" class="tab"></div>
# Was the string truncated, and an error reported?
assert 0 == metrics.article.content.test_get_num_recorded_errors(
ErrorType.INVALID_OVERFLOW
)
```

</div>

<div data-lang="Rust" class="tab">

```Rust
use glean::ErrorType;
use glean_metrics::article;

<div data-lang="Swift" class="tab"></div>
// Was the string truncated, and an error reported?
assert_eq!(
0,
article::content.test_get_num_recorded_errors(
ErrorType::InvalidOverflow,
None
)
);
```

<div data-lang="Python" class="tab"></div>
</div>

<div data-lang="JavaScript" class="tab">

Expand All @@ -114,8 +255,6 @@ assert.strictEqual(

</div>

<div data-lang="Rust" class="tab"></div>

<div data-lang="Firefox Desktop" class="tab"></div>

{{#include ../../../shared/tab_footer.md}}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package mozilla.telemetry.glean.private

/**
* This implements the developer facing API for recording text metrics.
*
* Instances of this class type are automatically generated by the parsers at build time,
* allowing developers to record values that were previously registered in the metrics.yaml file.
*
* The text API only exposes the [set] method, which takes care of validating the input
* data and making sure that limits are enforced.
*/
typealias TextMetricType = mozilla.telemetry.glean.internal.TextMetric
12 changes: 12 additions & 0 deletions glean-core/ios/Glean/Metrics/TextMetric.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/// This implements the developer facing API for recording text metrics.
///
/// Instances of this class type are automatically generated by the parsers at build time,
/// allowing developers to record values that were previously registered in the metrics.yaml file.
///
/// The text API only exposes the `TextMetricType.set(_:)` method, which takes care of validating the input
/// data and making sure that limits are enforced.
public typealias TextMetricType = TextMetric
10 changes: 10 additions & 0 deletions glean-core/src/glean.udl
Original file line number Diff line number Diff line change
Expand Up @@ -537,3 +537,13 @@ interface NumeratorMetric {

i32 test_get_num_recorded_errors(ErrorType error, optional string? ping_name = null);
};

interface TextMetric {
constructor(CommonMetricData meta);

void set(string value);

string? test_get_value(optional string? ping_name = null);

i32 test_get_num_recorded_errors(ErrorType error, optional string? ping_name = null);
};
4 changes: 2 additions & 2 deletions glean-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ pub use crate::metrics::{
BooleanMetric, CounterMetric, CustomDistributionMetric, Datetime, DatetimeMetric,
DenominatorMetric, DistributionData, EventMetric, MemoryDistributionMetric, MemoryUnit,
NumeratorMetric, PingType, QuantityMetric, Rate, RateMetric, RecordedEvent, RecordedExperiment,
StringListMetric, StringMetric, TimeUnit, TimerId, TimespanMetric, TimingDistributionMetric,
UrlMetric, UuidMetric,
StringListMetric, StringMetric, TextMetric, TimeUnit, TimerId, TimespanMetric,
TimingDistributionMetric, UrlMetric, UuidMetric,
};
pub use crate::upload::{PingRequest, PingUploadTask, UploadResult};

Expand Down
2 changes: 2 additions & 0 deletions glean-core/src/lib_unit_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,7 @@ fn correct_order() {
MemoryDistribution(Histogram::functional(2.0, 8.0)),
Jwe("eyJhbGciOiJSU0EtT0FFUCIsImVuYyI6IkEyNTZHQ00ifQ.OKOawDo13gRp2ojaHV7LFpZcgV7T6DVZKTyKOMTYUmKoTCVJRgckCL9kiMT03JGeipsEdY3mx_etLbbWSrFr05kLzcSr4qKAq7YN7e9jwQRb23nfa6c9d-StnImGyFDbSv04uVuxIp5Zms1gNxKKK2Da14B8S4rzVRltdYwam_lDp5XnZAYpQdb76FdIKLaVmqgfwX7XWRxv2322i-vDxRfqNzo_tETKzpVLzfiwQyeyPGLBIO56YJ7eObdv0je81860ppamavo35UgoRdbYaBcoh9QcfylQr66oc6vFWXRcZ_ZT2LawVCWTIy3brGPi6UklfCpIMfIjf7iGdXKHzg.48V1_ALb6US04U3b.5eym8TW_c8SuK0ltJ3rpYIzOeDQz7TALvtu6UG9oMo4vpzs9tX_EFShS8iB7j6jiSdiwkIr3ajwQzaBtQD_A.XFBoMYUZodetZdvTiFvSkQ".into()),
Rate(0, 0),
Text("loooooooong text".into()),
];

for metric in all_metrics {
Expand Down Expand Up @@ -445,6 +446,7 @@ fn correct_order() {
Jwe(..) => assert_eq!(13, disc),
Rate(..) => assert_eq!(14, disc),
Url(..) => assert_eq!(15, disc),
Text(..) => assert_eq!(16, disc),
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions glean-core/src/metrics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ mod rate;
mod recorded_experiment;
mod string;
mod string_list;
mod text;
mod time_unit;
mod timespan;
mod timing_distribution;
Expand Down Expand Up @@ -56,6 +57,7 @@ pub use self::quantity::QuantityMetric;
pub use self::rate::{Rate, RateMetric};
pub use self::string::StringMetric;
pub use self::string_list::StringListMetric;
pub use self::text::TextMetric;
pub use self::time_unit::TimeUnit;
pub use self::timespan::TimespanMetric;
pub use self::timing_distribution::TimerId;
Expand Down Expand Up @@ -125,6 +127,8 @@ pub enum Metric {
Rate(i32, i32),
/// A URL metric. See [`UrlMetric`] for more information.
Url(String),
/// A Text metric. See [`TextMetric`] for more information.
Text(String),
}

/// A [`MetricType`] describes common behavior across all metrics.
Expand Down Expand Up @@ -181,6 +185,7 @@ impl Metric {
Metric::Uuid(_) => "uuid",
Metric::MemoryDistribution(_) => "memory_distribution",
Metric::Jwe(_) => "jwe",
Metric::Text(_) => "text",
}
}

Expand Down Expand Up @@ -209,6 +214,7 @@ impl Metric {
Metric::Uuid(s) => json!(s),
Metric::MemoryDistribution(hist) => json!(memory_distribution::snapshot(hist)),
Metric::Jwe(s) => json!(s),
Metric::Text(s) => json!(s),
}
}
}
Loading

0 comments on commit d4e02f7

Please sign in to comment.