From 3a233ee32fb8a6f41ed0cfa44a900f219e536625 Mon Sep 17 00:00:00 2001 From: Yuqian Li Date: Tue, 26 Jan 2021 16:43:55 -0800 Subject: [PATCH 1/2] Allow LegacyFlutterDestination to specify project So we may use the service account from one project to write into the datastore of another project. Eventually, we'll remove all datastore related code and LegacyFlutterDestination so this will only be a temporary solution during our transition. --- packages/metrics_center/CHANGELOG.md | 3 +++ packages/metrics_center/lib/src/flutter.dart | 5 ++++- packages/metrics_center/lib/src/legacy_datastore.dart | 10 +++++++--- packages/metrics_center/lib/src/legacy_flutter.dart | 7 +++++-- packages/metrics_center/pubspec.yaml | 2 +- packages/metrics_center/test/legacy_flutter_test.dart | 9 +++++++++ 6 files changed, 29 insertions(+), 7 deletions(-) diff --git a/packages/metrics_center/CHANGELOG.md b/packages/metrics_center/CHANGELOG.md index 64fdfcc196b5..27a5b7134e59 100644 --- a/packages/metrics_center/CHANGELOG.md +++ b/packages/metrics_center/CHANGELOG.md @@ -1,3 +1,6 @@ +# 0.0.6 +- Allow `datastoreFromCredentialsJson` to specify project id. + # 0.0.5 - `FlutterDestination` writes into both Skia perf GCS and the legacy datastore. diff --git a/packages/metrics_center/lib/src/flutter.dart b/packages/metrics_center/lib/src/flutter.dart index 7fbfc6c8429a..7a4f0eab8ad3 100644 --- a/packages/metrics_center/lib/src/flutter.dart +++ b/packages/metrics_center/lib/src/flutter.dart @@ -40,8 +40,11 @@ class FlutterDestination extends MetricDestination { static Future makeFromCredentialsJson( Map json, {bool isTesting = false}) async { + // Specify the project id for LegacyFlutterDestination as we may get a + // service account json from another GCP project. final LegacyFlutterDestination legacyDestination = - LegacyFlutterDestination(await datastoreFromCredentialsJson(json)); + await LegacyFlutterDestination.makeFromCredentialsJson(json, + projectId: 'flutter-cirrus'); final SkiaPerfDestination skiaPerfDestination = await SkiaPerfDestination.makeFromGcpCredentials(json, isTesting: isTesting); diff --git a/packages/metrics_center/lib/src/legacy_datastore.dart b/packages/metrics_center/lib/src/legacy_datastore.dart index 1d12d7ab8b29..1095ef125f7b 100644 --- a/packages/metrics_center/lib/src/legacy_datastore.dart +++ b/packages/metrics_center/lib/src/legacy_datastore.dart @@ -18,11 +18,15 @@ import 'common.dart'; import 'constants.dart'; /// Creates a [DatastoreDB] connection from JSON service account credentials. -Future datastoreFromCredentialsJson( - Map json) async { +/// +/// We allow specifying a project id as we may use the service account from one +/// project to write into the datastore of another project. +Future datastoreFromCredentialsJson(Map json, + {String projectId}) async { final AutoRefreshingAuthClient client = await clientViaServiceAccount( ServiceAccountCredentials.fromJson(json), DatastoreImpl.SCOPES); - return DatastoreDB(DatastoreImpl(client, json[kProjectId] as String)); + return DatastoreDB( + DatastoreImpl(client, projectId ?? json[kProjectId] as String)); } /// Creates a [DatastoreDB] from an auth token. diff --git a/packages/metrics_center/lib/src/legacy_flutter.dart b/packages/metrics_center/lib/src/legacy_flutter.dart index 8bc7f1fc9ce3..a153fbe68ff2 100644 --- a/packages/metrics_center/lib/src/legacy_flutter.dart +++ b/packages/metrics_center/lib/src/legacy_flutter.dart @@ -61,8 +61,11 @@ class LegacyFlutterDestination extends MetricDestination { /// Creates this destination from a service account credentials JSON file. static Future makeFromCredentialsJson( - Map json) async { - return LegacyFlutterDestination(await datastoreFromCredentialsJson(json)); + Map json, { + String projectId, + }) async { + return LegacyFlutterDestination( + await datastoreFromCredentialsJson(json, projectId: projectId)); } /// Creates this destination to authorize with an OAuth access token. diff --git a/packages/metrics_center/pubspec.yaml b/packages/metrics_center/pubspec.yaml index f4905a3ef1dd..353acc55d172 100644 --- a/packages/metrics_center/pubspec.yaml +++ b/packages/metrics_center/pubspec.yaml @@ -1,5 +1,5 @@ name: metrics_center -version: 0.0.5 +version: 0.0.6 description: Support multiple performance metrics sources/formats and destinations. homepage: diff --git a/packages/metrics_center/test/legacy_flutter_test.dart b/packages/metrics_center/test/legacy_flutter_test.dart index a67220a5dd39..b5e5d0445020 100644 --- a/packages/metrics_center/test/legacy_flutter_test.dart +++ b/packages/metrics_center/test/legacy_flutter_test.dart @@ -23,6 +23,15 @@ void main() { await dst.update([MetricPoint(1.0, const {})]); }, skip: credentialsJson == null); + test( + 'LegacyFlutterDestination integration test: ' + 'can specify datastore project id.', () async { + final LegacyFlutterDestination dst = + await LegacyFlutterDestination.makeFromCredentialsJson(credentialsJson, + projectId: 'flutter-test-262600'); + await dst.update([MetricPoint(1.0, const {})]); + }, skip: credentialsJson == null); + test( 'LegacyFlutterDestination integration test: ' 'can update with an access token.', () async { From ca4263c7920af132917109ba5c536545857fc616 Mon Sep 17 00:00:00 2001 From: Yuqian Li Date: Tue, 26 Jan 2021 17:01:18 -0800 Subject: [PATCH 2/2] Fix constants --- packages/metrics_center/CHANGELOG.md | 3 +++ packages/metrics_center/lib/metrics_center.dart | 1 + packages/metrics_center/pubspec.yaml | 2 +- 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/metrics_center/CHANGELOG.md b/packages/metrics_center/CHANGELOG.md index 27a5b7134e59..919bd3c9a472 100644 --- a/packages/metrics_center/CHANGELOG.md +++ b/packages/metrics_center/CHANGELOG.md @@ -1,3 +1,6 @@ +# 0.0.7 +- Expose constants that were missing since 0.0.4+1. + # 0.0.6 - Allow `datastoreFromCredentialsJson` to specify project id. diff --git a/packages/metrics_center/lib/metrics_center.dart b/packages/metrics_center/lib/metrics_center.dart index beebe8ecb9c6..379011634610 100644 --- a/packages/metrics_center/lib/metrics_center.dart +++ b/packages/metrics_center/lib/metrics_center.dart @@ -3,6 +3,7 @@ // found in the LICENSE file. export 'src/common.dart'; +export 'src/constants.dart'; export 'src/flutter.dart'; export 'src/google_benchmark.dart'; export 'src/skiaperf.dart'; diff --git a/packages/metrics_center/pubspec.yaml b/packages/metrics_center/pubspec.yaml index 353acc55d172..72e360d1b8ee 100644 --- a/packages/metrics_center/pubspec.yaml +++ b/packages/metrics_center/pubspec.yaml @@ -1,5 +1,5 @@ name: metrics_center -version: 0.0.6 +version: 0.0.7 description: Support multiple performance metrics sources/formats and destinations. homepage: