Skip to content

Commit

Permalink
Bug 1650787 - Implement the JWE metric type on the core and FFI (#1062)
Browse files Browse the repository at this point in the history
  • Loading branch information
Beatriz Rizental authored Jul 20, 2020
1 parent fdf341f commit 0d7532f
Show file tree
Hide file tree
Showing 10 changed files with 731 additions and 0 deletions.
1 change: 1 addition & 0 deletions .dictionary
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ integrations
io
ios
janerik
JWE
ktlint
lang
latencies
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

* General
* BUGFIX: fix `int32` to `ErrorType` mapping. The `InvalidOverflow` had a value mismatch between glean-core and the bindings. This would only be a problem in unit tests. ([#1063](https://github.com/mozilla/glean/pull/1063))
* Implement a JWE metric type ([#1062](https://github.com/mozilla/glean/pull/1062)).

[Full changelog](https://github.com/mozilla/glean/compare/v31.4.0...main)

Expand Down
28 changes: 28 additions & 0 deletions glean-core/ffi/glean.h
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,8 @@ void glean_destroy_event_metric(uint64_t v);

void glean_destroy_glean(void);

void glean_destroy_jwe_metric(uint64_t v);

void glean_destroy_labeled_boolean_metric(uint64_t v);

void glean_destroy_labeled_counter_metric(uint64_t v);
Expand Down Expand Up @@ -440,6 +442,25 @@ uint8_t glean_is_first_run(void);

uint8_t glean_is_upload_enabled(void);

void glean_jwe_set(uint64_t metric_id,
FfiStr header,
FfiStr key,
FfiStr init_vector,
FfiStr cipher_text,
FfiStr auth_tag);

void glean_jwe_set_with_compact_repr(uint64_t metric_id, FfiStr value);

int32_t glean_jwe_test_get_num_recorded_errors(uint64_t metric_id,
int32_t error_type,
FfiStr storage_name);

char *glean_jwe_test_get_value(uint64_t metric_id, FfiStr storage_name);

char *glean_jwe_test_get_value_as_json_string(uint64_t metric_id, FfiStr storage_name);

uint8_t glean_jwe_test_has_value(uint64_t metric_id, FfiStr storage_name);

/**
* Create a new instance of the sub-metric of this labeled metric.
*/
Expand Down Expand Up @@ -524,6 +545,13 @@ uint64_t glean_new_event_metric(FfiStr category,
RawStringArray extra_keys,
int32_t extra_keys_len);

uint64_t glean_new_jwe_metric(FfiStr category,
FfiStr name,
RawStringArray send_in_pings,
int32_t send_in_pings_len,
Lifetime lifetime,
uint8_t disabled);

/**
* Create a new labeled metric.
*/
Expand Down
83 changes: 83 additions & 0 deletions glean-core/ffi/src/jwe.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// 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 https://mozilla.org/MPL/2.0/.

use std::os::raw::c_char;

use ffi_support::FfiStr;

use crate::ffi_string_ext::FallibleToString;
use crate::{define_metric, handlemap_ext::HandleMapExtension, with_glean_value, Lifetime};

define_metric!(JweMetric => JWE_METRICS {
new -> glean_new_jwe_metric(),
test_get_num_recorded_errors -> glean_jwe_test_get_num_recorded_errors,
destroy -> glean_destroy_jwe_metric,
});

#[no_mangle]
pub extern "C" fn glean_jwe_set_with_compact_repr(metric_id: u64, value: FfiStr) {
with_glean_value(|glean| {
JWE_METRICS.call_with_log(metric_id, |metric| {
let value = value.to_string_fallible()?;
metric.set_with_compact_repr(&glean, value);
Ok(())
})
})
}

#[no_mangle]
pub extern "C" fn glean_jwe_set(
metric_id: u64,
header: FfiStr,
key: FfiStr,
init_vector: FfiStr,
cipher_text: FfiStr,
auth_tag: FfiStr,
) {
with_glean_value(|glean| {
JWE_METRICS.call_with_log(metric_id, |metric| {
let header = header.to_string_fallible()?;
let key = key.to_string_fallible()?;
let init_vector = init_vector.to_string_fallible()?;
let cipher_text = cipher_text.to_string_fallible()?;
let auth_tag = auth_tag.to_string_fallible()?;
metric.set(&glean, header, key, init_vector, cipher_text, auth_tag);
Ok(())
})
})
}

#[no_mangle]
pub extern "C" fn glean_jwe_test_has_value(metric_id: u64, storage_name: FfiStr) -> u8 {
with_glean_value(|glean| {
JWE_METRICS.call_infallible(metric_id, |metric| {
metric
.test_get_value(glean, storage_name.as_str())
.is_some()
})
})
}

#[no_mangle]
pub extern "C" fn glean_jwe_test_get_value(metric_id: u64, storage_name: FfiStr) -> *mut c_char {
with_glean_value(|glean| {
JWE_METRICS.call_infallible(metric_id, |metric| {
metric.test_get_value(glean, storage_name.as_str()).unwrap()
})
})
}

#[no_mangle]
pub extern "C" fn glean_jwe_test_get_value_as_json_string(
metric_id: u64,
storage_name: FfiStr,
) -> *mut c_char {
with_glean_value(|glean| {
JWE_METRICS.call_infallible(metric_id, |metric| {
metric
.test_get_value_as_json_string(glean, storage_name.as_str())
.unwrap()
})
})
}
1 change: 1 addition & 0 deletions glean-core/ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ mod event;
mod ffi_string_ext;
mod from_raw;
mod handlemap_ext;
mod jwe;
mod labeled;
mod memory_distribution;
pub mod ping_type;
Expand Down
28 changes: 28 additions & 0 deletions glean-core/ios/Glean/GleanFfi.h
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,8 @@ void glean_destroy_event_metric(uint64_t v);

void glean_destroy_glean(void);

void glean_destroy_jwe_metric(uint64_t v);

void glean_destroy_labeled_boolean_metric(uint64_t v);

void glean_destroy_labeled_counter_metric(uint64_t v);
Expand Down Expand Up @@ -440,6 +442,25 @@ uint8_t glean_is_first_run(void);

uint8_t glean_is_upload_enabled(void);

void glean_jwe_set(uint64_t metric_id,
FfiStr header,
FfiStr key,
FfiStr init_vector,
FfiStr cipher_text,
FfiStr auth_tag);

void glean_jwe_set_with_compact_repr(uint64_t metric_id, FfiStr value);

int32_t glean_jwe_test_get_num_recorded_errors(uint64_t metric_id,
int32_t error_type,
FfiStr storage_name);

char *glean_jwe_test_get_value(uint64_t metric_id, FfiStr storage_name);

char *glean_jwe_test_get_value_as_json_string(uint64_t metric_id, FfiStr storage_name);

uint8_t glean_jwe_test_has_value(uint64_t metric_id, FfiStr storage_name);

/**
* Create a new instance of the sub-metric of this labeled metric.
*/
Expand Down Expand Up @@ -524,6 +545,13 @@ uint64_t glean_new_event_metric(FfiStr category,
RawStringArray extra_keys,
int32_t extra_keys_len);

uint64_t glean_new_jwe_metric(FfiStr category,
FfiStr name,
RawStringArray send_in_pings,
int32_t send_in_pings_len,
Lifetime lifetime,
uint8_t disabled);

/**
* Create a new labeled metric.
*/
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 @@ -372,6 +372,7 @@ fn correct_order() {
Timespan(Duration::new(5, 0), TimeUnit::Second),
TimingDistribution(Histogram::functional(2.0, 8.0)),
MemoryDistribution(Histogram::functional(2.0, 8.0)),
Jwe("eyJhbGciOiJSU0EtT0FFUCIsImVuYyI6IkEyNTZHQ00ifQ.OKOawDo13gRp2ojaHV7LFpZcgV7T6DVZKTyKOMTYUmKoTCVJRgckCL9kiMT03JGeipsEdY3mx_etLbbWSrFr05kLzcSr4qKAq7YN7e9jwQRb23nfa6c9d-StnImGyFDbSv04uVuxIp5Zms1gNxKKK2Da14B8S4rzVRltdYwam_lDp5XnZAYpQdb76FdIKLaVmqgfwX7XWRxv2322i-vDxRfqNzo_tETKzpVLzfiwQyeyPGLBIO56YJ7eObdv0je81860ppamavo35UgoRdbYaBcoh9QcfylQr66oc6vFWXRcZ_ZT2LawVCWTIy3brGPi6UklfCpIMfIjf7iGdXKHzg.48V1_ALb6US04U3b.5eym8TW_c8SuK0ltJ3rpYIzOeDQz7TALvtu6UG9oMo4vpzs9tX_EFShS8iB7j6jiSdiwkIr3ajwQzaBtQD_A.XFBoMYUZodetZdvTiFvSkQ".into()),
];

for metric in all_metrics {
Expand All @@ -396,6 +397,7 @@ fn correct_order() {
Timespan(..) => assert_eq!(10, disc),
TimingDistribution(..) => assert_eq!(11, disc),
MemoryDistribution(..) => assert_eq!(12, disc),
Jwe(..) => assert_eq!(13, disc),
}
}
}
Expand Down
Loading

0 comments on commit 0d7532f

Please sign in to comment.