Skip to content

Commit cdee9f8

Browse files
authored
Firebase Performance Monitoring (#10)
1 parent 7b6cc17 commit cdee9f8

File tree

8 files changed

+153
-950
lines changed

8 files changed

+153
-950
lines changed

packages/firebase_snippets_app/android/app/build.gradle

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ if (flutterVersionName == null) {
2222
}
2323

2424
apply plugin: 'com.android.application'
25+
apply plugin: 'com.google.gms.google-services'
2526
apply plugin: 'kotlin-android'
2627
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
2728

@@ -43,7 +44,7 @@ android {
4344

4445
defaultConfig {
4546
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
46-
applicationId "com.example.firebase_snippets_app"
47+
applicationId "com.example.firestore_snippets"
4748
minSdkVersion 21
4849
targetSdkVersion flutter.targetSdkVersion
4950
versionCode flutterVersionCode.toInteger()
@@ -65,4 +66,5 @@ flutter {
6566

6667
dependencies {
6768
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
69+
implementation platform('com.google.firebase:firebase-bom:29.3.0')
6870
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{
2+
"project_info": {
3+
"project_number": "220375889386",
4+
"project_id": "flutter-fire-snippets",
5+
"storage_bucket": "flutter-fire-snippets.appspot.com"
6+
},
7+
"client": [
8+
{
9+
"client_info": {
10+
"mobilesdk_app_id": "1:220375889386:android:1a16e85eeab0d5896bae93",
11+
"android_client_info": {
12+
"package_name": "com.example.firestore_snippets"
13+
}
14+
},
15+
"oauth_client": [
16+
{
17+
"client_id": "220375889386-2k28ra1b5iq3p2qbh61ag1o4rt24asb6.apps.googleusercontent.com",
18+
"client_type": 3
19+
}
20+
],
21+
"api_key": [
22+
{
23+
"current_key": "AIzaSyC2bbpFcMWgGpKKEmqNw57Al2vdV4QnNP0"
24+
}
25+
],
26+
"services": {
27+
"appinvite_service": {
28+
"other_platform_oauth_client": [
29+
{
30+
"client_id": "220375889386-2k28ra1b5iq3p2qbh61ag1o4rt24asb6.apps.googleusercontent.com",
31+
"client_type": 3
32+
},
33+
{
34+
"client_id": "220375889386-8v73sv2lia3e7c12h0btp2tfbj0um4it.apps.googleusercontent.com",
35+
"client_type": 2,
36+
"ios_info": {
37+
"bundle_id": "com.fluttersnippets.app"
38+
}
39+
}
40+
]
41+
}
42+
}
43+
}
44+
],
45+
"configuration_version": "1"
46+
}

packages/firebase_snippets_app/android/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ buildscript {
88
dependencies {
99
classpath 'com.android.tools.build:gradle:4.1.0'
1010
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
11+
classpath 'com.google.gms:google-services:4.3.10'
1112
}
1213
}
1314

packages/firebase_snippets_app/lib/firebase_options.dart

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,6 @@ import 'package:firebase_core/firebase_core.dart' show FirebaseOptions;
1818
import 'package:flutter/foundation.dart'
1919
show defaultTargetPlatform, kIsWeb, TargetPlatform;
2020

21-
/*
22-
NB: This contains errors!
23-
24-
TODO: ewindmill -- set up env variables and add options back in.
25-
26-
*/
27-
2821
/// Default [FirebaseOptions] for use with your Firebase apps.
2922
///
3023
/// Example:
@@ -58,29 +51,31 @@ class DefaultFirebaseOptions {
5851
}
5952

6053
static const FirebaseOptions web = FirebaseOptions(
61-
apiKey: 'TODO: use secrets or demo',
62-
appId: 'TODO: use secrets or demo',
63-
messagingSenderId: 'TODO: use secrets or demo',
64-
projectId: 'TODO: use secrets or demo',
65-
authDomain: 'TODO: use secrets or demo',
66-
storageBucket: 'TODO: use secrets or demo',
54+
apiKey: 'AIzaSyDu2FRBnv0pV01dHHOQuk26pR1oYcZpGTE',
55+
appId: '1:220375889386:web:bc468dd9e586597b6bae93',
56+
messagingSenderId: '220375889386',
57+
projectId: 'flutter-fire-snippets',
58+
authDomain: 'flutter-fire-snippets.firebaseapp.com',
59+
storageBucket: 'flutter-fire-snippets.appspot.com',
6760
);
6861

6962
static const FirebaseOptions android = FirebaseOptions(
70-
apiKey: 'TODO: use secrets or demo',
71-
appId: 'TODO: use secrets or demo',
72-
messagingSenderId: 'TODO: use secrets or demo',
73-
projectId: 'TODO: use secrets or demo',
74-
storageBucket: 'TODO: use secrets or demo',
63+
apiKey: 'AIzaSyC2bbpFcMWgGpKKEmqNw57Al2vdV4QnNP0',
64+
appId: '1:220375889386:android:1a16e85eeab0d5896bae93',
65+
messagingSenderId: '220375889386',
66+
projectId: 'flutter-fire-snippets',
67+
storageBucket: 'flutter-fire-snippets.appspot.com',
7568
);
7669

7770
static const FirebaseOptions ios = FirebaseOptions(
78-
apiKey: 'TODO: use secrets or demo',
79-
appId: 'TODO: use secrets or demo',
80-
messagingSenderId: 'TODO: use secrets or demo',
81-
projectId: 'TODO: use secrets or demo',
82-
storageBucket: 'TODO: use secrets or demo',
83-
iosClientId: 'TODO: use secrets or demo',
71+
apiKey: 'AIzaSyDNp5_kQjIgyS_UMtv9oRKcbCjOfYB1LNc',
72+
appId: '1:220375889386:ios:0fec9df2e96f0db36bae93',
73+
messagingSenderId: '220375889386',
74+
projectId: 'flutter-fire-snippets',
75+
storageBucket: 'flutter-fire-snippets.appspot.com',
76+
iosClientId:
77+
'220375889386-8v73sv2lia3e7c12h0btp2tfbj0um4it.apps.googleusercontent.com',
8478
iosBundleId: 'com.fluttersnippets.app',
8579
);
80+
8681
}

packages/firebase_snippets_app/lib/main.dart

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,6 @@ import 'package:flutter/material.dart';
2727
import 'firebase_options.dart';
2828

2929
void main() async {
30-
// [START get_firestore_instance]
31-
/// In the Firebase documentation, the code should look like the following example
32-
/// In this snippets app, we're using the FlutterFire CLI to generate FirebaseOptions
33-
///```dart
34-
/// WidgetsFlutterBinding.ensureInitialized();
35-
/// await Firebase.initializeApp(
36-
/// options: const FirebaseOptions(
37-
/// apiKey: '### FIREBASE API KEY ###',
38-
/// authDomain: '### FIREBASE AUTH DOMAIN ###',
39-
/// projectId: '### CLOUD FIRESTORE PROJECT ID ###'
40-
/// )
41-
/// );
42-
/// final firestore = FirebaseFirestore.instance;
43-
/// ```
44-
// [END get_firestore_instance]
45-
4630
WidgetsFlutterBinding.ensureInitialized();
4731
await Firebase.initializeApp(
4832
options: DefaultFirebaseOptions.currentPlatform,
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// ignore_for_file: non_constant_identifier_names
2+
3+
import 'package:firebase_performance/firebase_performance.dart';
4+
import 'package:firebase_snippets_app/snippets/snippet_base.dart';
5+
import 'package:http/http.dart' as http;
6+
7+
class FirebasePerformanceMonitoringSnippets implements DocSnippet {
8+
@override
9+
void runAll() {
10+
performanceMonitoring_usage();
11+
performanceMonitoring_httpTracking();
12+
performanceMonitoring_stopAutoCollecting();
13+
}
14+
15+
void performanceMonitoring_usage() async {
16+
// [START performance_monitoring_usage]
17+
FirebasePerformance performance = FirebasePerformance.instance;
18+
19+
Trace trace = performance.newTrace('custom-trace');
20+
// [END performance_monitoring_usage]
21+
22+
// [START performance_monitoring_start_trace]
23+
await trace.start();
24+
25+
// Set metrics you wish to track
26+
trace.setMetric('sum', 200);
27+
trace.setMetric('time', 342340435);
28+
// [END performance_monitoring_start_trace]
29+
30+
// [START performance_monitoring_increment_values]
31+
trace.setMetric('sum', 200);
32+
33+
// `sum` will be incremented to 201
34+
trace.incrementMetric('sum', 1);
35+
// [END performance_monitoring_increment_values]
36+
37+
// [START performance_monitoring_set_non_metric_data]
38+
trace.putAttribute('userId', '1234');
39+
// [END performance_monitoring_set_non_metric_data]
40+
41+
// [START performance_monitoring_stop_trace]
42+
await trace.stop();
43+
// [END performance_monitoring_stop_trace]
44+
}
45+
46+
void performanceMonitoring_httpTracking() async {
47+
// [START performance_monitoring_http_tracking]
48+
FirebasePerformance performance = FirebasePerformance.instance;
49+
50+
// Create a `HttpMetric` instance using the URL you're requesting as well as the type of request
51+
String url = 'https://firebase.flutter.dev';
52+
HttpMetric metric = performance.newHttpMetric(url, HttpMethod.Get);
53+
54+
// You may also assign up to 5 attributes for each trace
55+
metric.putAttribute('foo', 'bar');
56+
57+
// Start the trace
58+
await metric.start();
59+
60+
// Make the request
61+
Uri uri = Uri.parse(url);
62+
var response = await http.get(uri);
63+
64+
// Set specific headers to be collated
65+
metric.responseContentType = response.headers['Content-Type'];
66+
metric.httpResponseCode = response.statusCode;
67+
metric.responsePayloadSize = response.contentLength;
68+
69+
// Stops the trace. This is when the data is sent to the Firebase server and it will appear in your Firebase console
70+
await metric.stop();
71+
// [END performance_monitoring_http_tracking]
72+
}
73+
74+
void performanceMonitoring_stopAutoCollecting() async {
75+
// [START performance_monitoring_stop_auto_collecting]
76+
FirebasePerformance performance = FirebasePerformance.instance;
77+
// Custom data collection is, by default, enabled
78+
bool isEnabled = await performance.isPerformanceCollectionEnabled();
79+
// Set data collection to `false`
80+
await performance.setPerformanceCollectionEnabled(false);
81+
// [END performance_monitoring_stop_auto_collecting]
82+
}
83+
}

0 commit comments

Comments
 (0)