Skip to content

Commit 648b832

Browse files
jeremyjiang-devpull[bot]
authored andcommitted
Add console URL logging. (#2632)
* Create a conosle URL logging prototype. * Add console logging for trace metrics * Add ConsoleUrlGenerator for logging console links. * Clean unused variables. * Fix compile error. * Address comments. * Add missing utm_source. * Add tests. * Add tests. * Revert AppStateUpdateHandler.java. * Add new line at the end of the file. * Update logging messages. * Address comments * Update tests. * Address comments.
1 parent 6fbb93f commit 648b832

File tree

4 files changed

+153
-3
lines changed

4 files changed

+153
-3
lines changed

firebase-perf/src/main/java/com/google/firebase/perf/FirebasePerformance.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import com.google.firebase.perf.config.ConfigResolver;
3232
import com.google.firebase.perf.config.RemoteConfigManager;
3333
import com.google.firebase.perf.logging.AndroidLogger;
34+
import com.google.firebase.perf.logging.ConsoleUrlGenerator;
3435
import com.google.firebase.perf.metrics.HttpMetric;
3536
import com.google.firebase.perf.metrics.Trace;
3637
import com.google.firebase.perf.metrics.validator.PerfMetricValidator;
@@ -191,6 +192,13 @@ public static FirebasePerformance getInstance() {
191192
gaugeManager.setApplicationContext(appContext);
192193

193194
mPerformanceCollectionForceEnabledState = configResolver.getIsPerformanceCollectionEnabled();
195+
if (isPerformanceCollectionEnabled()) {
196+
logger.debug(
197+
String.format(
198+
"Firebase Performance Monitoring is successfully initialized! In a minute, visit the Firebase console to view your data: %s",
199+
ConsoleUrlGenerator.generateDashboardUrl(
200+
firebaseApp.getOptions().getProjectId(), appContext.getPackageName())));
201+
}
194202
}
195203

196204
/**
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Copyright 2021 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.google.firebase.perf.logging;
16+
17+
public final class ConsoleUrlGenerator {
18+
private static final String URL_BASE_PATH = "https://console.firebase.google.com";
19+
private static final String UTM_MEDIUM = "android-ide";
20+
private static final String UTM_SOURCE = "perf-android-sdk";
21+
22+
/**
23+
* Generate the console URL for Firebase Performance dashboard page.
24+
*
25+
* @param projectId the Firebase project ID
26+
* @param packageName the name of this application's package
27+
*/
28+
public static String generateDashboardUrl(String projectId, String packageName) {
29+
String rootUrl = getRootUrl(projectId, packageName);
30+
return String.format("%s/trends?utm_source=%s&utm_medium=%s", rootUrl, UTM_SOURCE, UTM_MEDIUM);
31+
}
32+
33+
/**
34+
* Generate the console URL for the custom trace.
35+
*
36+
* @param projectId the Firebase project ID
37+
* @param packageName the name of this application's package
38+
* @param name the trace name
39+
*/
40+
public static String generateCustomTraceUrl(String projectId, String packageName, String name) {
41+
String rootUrl = getRootUrl(projectId, packageName);
42+
return String.format(
43+
"%s/metrics/trace/DURATION_TRACE/%s?utm_source=%s&utm_medium=%s",
44+
rootUrl, name, UTM_SOURCE, UTM_MEDIUM);
45+
}
46+
47+
/**
48+
* Generate the console URL for the screen trace.
49+
*
50+
* @param projectId the Firebase project ID
51+
* @param packageName the name of this application's package
52+
* @param name the trace name
53+
*/
54+
public static String generateScreenTraceUrl(String projectId, String packageName, String name) {
55+
String rootUrl = getRootUrl(projectId, packageName);
56+
return String.format(
57+
"%s/metrics/trace/SCREEN_TRACE/%s?utm_source=%s&utm_medium=%s",
58+
rootUrl, name, UTM_SOURCE, UTM_MEDIUM);
59+
}
60+
61+
/**
62+
* Get the root URL for the project and package.
63+
*
64+
* @param projectId the Firebase project ID
65+
* @param packageName the name of this application's package
66+
*/
67+
private static String getRootUrl(String projectId, String packageName) {
68+
return String.format(
69+
"%s/project/%s/performance/app/android:%s", URL_BASE_PATH, projectId, packageName);
70+
}
71+
}

firebase-perf/src/main/java/com/google/firebase/perf/transport/TransportManager.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import com.google.firebase.perf.application.AppStateMonitor.AppStateCallback;
3636
import com.google.firebase.perf.config.ConfigResolver;
3737
import com.google.firebase.perf.logging.AndroidLogger;
38+
import com.google.firebase.perf.logging.ConsoleUrlGenerator;
3839
import com.google.firebase.perf.metrics.validator.PerfMetricValidator;
3940
import com.google.firebase.perf.session.SessionManager;
4041
import com.google.firebase.perf.util.Constants;
@@ -116,6 +117,8 @@ public class TransportManager implements AppStateCallback {
116117
private RateLimiter rateLimiter;
117118
private AppStateMonitor appStateMonitor;
118119
private ApplicationInfo.Builder applicationInfoBuilder;
120+
private String packageName;
121+
private String projectId;
119122

120123
private boolean isForegroundState = false;
121124

@@ -156,6 +159,7 @@ void initializeForTest(
156159
ExecutorService executorService) {
157160

158161
this.firebaseApp = firebaseApp;
162+
this.projectId = firebaseApp.getOptions().getProjectId();
159163
this.appContext = firebaseApp.getApplicationContext();
160164
this.firebasePerformance = firebasePerformance;
161165
this.firebaseInstallationsApi = firebaseInstallationsApi;
@@ -190,6 +194,7 @@ public void initialize(
190194
@NonNull Provider<TransportFactory> flgTransportFactoryProvider) {
191195

192196
this.firebaseApp = firebaseApp;
197+
projectId = firebaseApp.getOptions().getProjectId();
193198
this.firebaseInstallationsApi = firebaseInstallationsApi;
194199
this.flgTransportFactoryProvider = flgTransportFactoryProvider;
195200

@@ -201,6 +206,7 @@ public void initialize(
201206
@WorkerThread
202207
private void syncInit() {
203208
appContext = firebaseApp.getApplicationContext();
209+
packageName = appContext.getPackageName();
204210
configResolver = ConfigResolver.getInstance();
205211
rateLimiter = new RateLimiter(appContext, Constants.RATE_PER_MINUTE, Constants.BURST_CAPACITY);
206212
appStateMonitor = AppStateMonitor.getInstance();
@@ -218,7 +224,7 @@ private void finishInitialization() {
218224
.setGoogleAppId(firebaseApp.getOptions().getApplicationId())
219225
.setAndroidAppInfo(
220226
AndroidApplicationInfo.newBuilder()
221-
.setPackageName(appContext.getPackageName())
227+
.setPackageName(packageName)
222228
.setSdkVersion(BuildConfig.FIREPERF_VERSION_NAME)
223229
.setVersionName(getVersionName(appContext)));
224230

@@ -448,7 +454,15 @@ private boolean isAllowedToDispatch(PerfMetric perfMetric) {
448454

449455
@WorkerThread
450456
private void dispatchLog(PerfMetric perfMetric) {
451-
logger.info("Logging %s", getLogcatMsg(perfMetric));
457+
458+
// Logs the metrics to logcat plus console URL for every trace metric.
459+
if (perfMetric.hasTraceMetric()) {
460+
logger.info(
461+
"Logging %s. In a minute, visit the Firebase console to view your data: %s",
462+
getLogcatMsg(perfMetric), getConsoleUrl(perfMetric.getTraceMetric()));
463+
} else {
464+
logger.info("Logging %s", getLogcatMsg(perfMetric));
465+
}
452466

453467
flgTransport.log(perfMetric);
454468
}
@@ -597,7 +611,6 @@ private static String getLogcatMsg(PerfMetricOrBuilder perfMetric) {
597611

598612
private static String getLogcatMsg(TraceMetric traceMetric) {
599613
long durationInUs = traceMetric.getDurationUs();
600-
601614
return String.format(
602615
Locale.ENGLISH,
603616
"trace metric: %s (duration: %.4fms)",
@@ -633,6 +646,15 @@ private static String getLogcatMsg(GaugeMetric gaugeMetric) {
633646
gaugeMetric.getAndroidMemoryReadingsCount());
634647
}
635648

649+
private String getConsoleUrl(TraceMetric traceMetric) {
650+
String traceName = traceMetric.getName();
651+
if (traceName.startsWith(Constants.SCREEN_TRACE_PREFIX)) {
652+
return ConsoleUrlGenerator.generateScreenTraceUrl(projectId, packageName, traceName);
653+
} else {
654+
return ConsoleUrlGenerator.generateCustomTraceUrl(projectId, packageName, traceName);
655+
}
656+
}
657+
636658
// endregion
637659

638660
// region Visible for Testing
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright 2021 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.google.firebase.perf.logging;
16+
17+
import static com.google.common.truth.Truth.assertThat;
18+
19+
import org.junit.Test;
20+
21+
/** Unit tests for {@link ConsoleUrlGenerator}. */
22+
public class ConsoleUrlGeneratorTest {
23+
24+
@Test
25+
public void testDashboardUrl() {
26+
String url = ConsoleUrlGenerator.generateDashboardUrl("test-project", "test-package");
27+
assertThat(url)
28+
.isEqualTo(
29+
"https://console.firebase.google.com/project/test-project/performance/app/android:test-package/trends?utm_source=perf-android-sdk&utm_medium=android-ide");
30+
}
31+
32+
@Test
33+
public void testCustomTraceUrl() {
34+
String url =
35+
ConsoleUrlGenerator.generateCustomTraceUrl("test-project", "test-package", "test-trace");
36+
assertThat(url)
37+
.isEqualTo(
38+
"https://console.firebase.google.com/project/test-project/performance/app/android:test-package/metrics/trace/DURATION_TRACE/test-trace?utm_source=perf-android-sdk&utm_medium=android-ide");
39+
}
40+
41+
@Test
42+
public void testScreenTraceUrl() {
43+
String url =
44+
ConsoleUrlGenerator.generateScreenTraceUrl("test-project", "test-package", "test-trace");
45+
assertThat(url)
46+
.isEqualTo(
47+
"https://console.firebase.google.com/project/test-project/performance/app/android:test-package/metrics/trace/SCREEN_TRACE/test-trace?utm_source=perf-android-sdk&utm_medium=android-ide");
48+
}
49+
}

0 commit comments

Comments
 (0)