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 30, 2022
1 parent b033df9 commit 6913fc9
Show file tree
Hide file tree
Showing 12 changed files with 564 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::content.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 the 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
4 changes: 4 additions & 0 deletions glean-core/ios/Glean.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
BFFE18382350A5F50068D97B /* TimingDistributionMetric.swift in Sources */ = {isa = PBXBuildFile; fileRef = BFFE18372350A5F50068D97B /* TimingDistributionMetric.swift */; };
BFFE183A2350A61F0068D97B /* TimingDistributionMetricTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = BFFE18392350A61F0068D97B /* TimingDistributionMetricTests.swift */; };
BFFE33AB232927C3005348FE /* Utils.swift in Sources */ = {isa = PBXBuildFile; fileRef = BFFE33AA232927C3005348FE /* Utils.swift */; };
CD062129284110970006370D /* TextMetric.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD062128284110970006370D /* TextMetric.swift */; };
CD0CADA427E216810015A997 /* glean.swift in Sources */ = {isa = PBXBuildFile; fileRef = CDD08C8527E21104007C8400 /* glean.swift */; };
CD0F7CC026F0F27900EDA6A4 /* UrlMetric.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD0F7CBF26F0F27900EDA6A4 /* UrlMetric.swift */; };
CD0F7CC226F0F28900EDA6A4 /* UrlMetricTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD0F7CC126F0F28900EDA6A4 /* UrlMetricTests.swift */; };
Expand Down Expand Up @@ -132,6 +133,7 @@
BFFE18372350A5F50068D97B /* TimingDistributionMetric.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TimingDistributionMetric.swift; sourceTree = "<group>"; };
BFFE18392350A61F0068D97B /* TimingDistributionMetricTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TimingDistributionMetricTests.swift; sourceTree = "<group>"; };
BFFE33AA232927C3005348FE /* Utils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Utils.swift; sourceTree = "<group>"; };
CD062128284110970006370D /* TextMetric.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TextMetric.swift; sourceTree = "<group>"; };
CD0F7CBF26F0F27900EDA6A4 /* UrlMetric.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UrlMetric.swift; sourceTree = "<group>"; };
CD0F7CC126F0F28900EDA6A4 /* UrlMetricTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UrlMetricTests.swift; sourceTree = "<group>"; };
CD387868271D9CD100C097D8 /* glean.udl */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = glean.udl; path = ../../src/glean.udl; sourceTree = "<group>"; };
Expand Down Expand Up @@ -314,6 +316,7 @@
BF30FDC3233260B500840607 /* TimespanMetric.swift */,
BFAED5072369751100DF293D /* StringListMetric.swift */,
BF89055E232BC213003CA2BA /* StringMetric.swift */,
CD062128284110970006370D /* TextMetric.swift */,
1FD4527423395B4500F4C7E8 /* UuidMetric.swift */,
CD0F7CBF26F0F27900EDA6A4 /* UrlMetric.swift */,
BF2E57042334B77D00364D92 /* EventMetric.swift */,
Expand Down Expand Up @@ -576,6 +579,7 @@
1F58920D23C7D615007D2D80 /* MetricsPingScheduler.swift in Sources */,
BF7CC0A62473F61C003B166D /* Metrics.swift in Sources */,
1FB70AEF23301C1D00C7CF09 /* Logger.swift in Sources */,
CD062129284110970006370D /* TextMetric.swift in Sources */,
1F6A8FF0233C049D007837D5 /* BooleanMetric.swift in Sources */,
1F6A8FF4233C0A91007837D5 /* DatetimeMetric.swift in Sources */,
AC06529C26E032E300D92D5E /* QuantityMetric.swift in Sources */,
Expand Down
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
3 changes: 3 additions & 0 deletions glean-core/src/lib_unit_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ fn correct_order() {
}

// One of every metric type. The values are arbitrary and don't matter.
let long_string = "0123456789".repeat(200);
let all_metrics = vec![
Boolean(false),
Counter(0),
Expand All @@ -418,6 +419,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(long_string),
];

for metric in all_metrics {
Expand Down Expand Up @@ -445,6 +447,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 6913fc9

Please sign in to comment.