Skip to content

Commit 2f2d4c8

Browse files
authored
Migrate Clearcut client-side usage metrics (#3600)
* Migrate from Clearcut to Firelog * Add a safeguard * Fix test * Update copyright header
1 parent 3aee0b2 commit 2f2d4c8

File tree

7 files changed

+78
-28
lines changed

7 files changed

+78
-28
lines changed

kokoro/ubuntu/release.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@ gcloud components install app-engine-java --quiet
1717

1818
echo "OAUTH_CLIENT_ID: ${OAUTH_CLIENT_ID}"
1919
echo "OAUTH_CLIENT_SECRET: ${OAUTH_CLIENT_SECRET}"
20+
echo "FIRELOG_API_KEY: ${FIRELOG_API_KEY}"
2021
echo "PRODUCT_VERSION_SUFFIX: "${PRODUCT_VERSION_SUFFIX}
2122

2223
# Exit if undefined (zero-length).
2324
test -n "${OAUTH_CLIENT_ID}"
2425
test -n "${OAUTH_CLIENT_SECRET}"
26+
test -n "${FIRELOG_API_KEY}"
2527

2628
cd git/google-cloud-eclipse
2729

@@ -34,5 +36,6 @@ TMPDIR= xvfb-run \
3436
mvn -V -B \
3537
-Doauth.client.id="${OAUTH_CLIENT_ID}" \
3638
-Doauth.client.secret="${OAUTH_CLIENT_SECRET}" \
39+
-Dfirelog.api.key="${FIRELOG_API_KEY}" \
3740
${PRODUCT_VERSION_SUFFIX:+-Dproduct.version.qualifier.suffix="'${PRODUCT_VERSION_SUFFIX}'"} \
3841
clean verify

plugins/com.google.cloud.tools.eclipse.usagetracker.test/src/com/google/cloud/tools/eclipse/usagetracker/AnalyticsPingManagerPluginTest.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,25 +60,24 @@ public void testBuildJson() {
6060
PingEvent event = new PingEvent("SomeEvent", metadata, null);
6161
String json = pingManager.jsonEncode(event);
6262

63-
Type singletonRootType = new TypeToken<List<Map<String, ?>>>(){}.getType();
64-
List<Map<String, ?>> singletonRoot = gson.fromJson(json, singletonRootType);
65-
Map<String, ?> root = singletonRoot.get(0);
63+
Type mapType = new TypeToken<Map<String, ?>>(){}.getType();
64+
Map<String, ?> root = gson.fromJson(json, mapType);
6665

67-
Map<String, ?> clientInfo = ((List<Map<String, ?>>) root.get("client_info")).get(0);
66+
Map<String, ?> clientInfo = (Map<String, ?>) root.get("client_info");
6867
Assert.assertEquals("DESKTOP", clientInfo.get("client_type"));
69-
Assert.assertEquals("CONCORD", root.get("log_source_name"));
68+
Assert.assertEquals("CONCORD", root.get("log_source"));
7069

7170
long requestTimeMs = ((Double) root.get("request_time_ms")).longValue();
7271
Assert.assertTrue(requestTimeMs >= 1000000);
7372

7473
Map<String, String> desktopClientInfo =
75-
((List<Map<String, String>>) clientInfo.get("desktop_client_info")).get(0);
74+
(Map<String, String>) clientInfo.get("desktop_client_info");
7675
Assert.assertTrue(desktopClientInfo.get("os").length() > 1);
7776

78-
List<Object> logEvents = ((List<List<Object>>) root.get("log_event")).get(0);
77+
List<Map<String, Object>> logEvents = (List<Map<String, Object>>) root.get("log_event");
7978
Assert.assertEquals(1, logEvents.size());
8079

81-
Map<String, Object> logEvent = (Map<String, Object>) logEvents.get(0);
80+
Map<String, Object> logEvent = logEvents.get(0);
8281
long eventTimeMs = ((Double) logEvent.get("event_time_ms")).longValue();
8382
Assert.assertTrue(eventTimeMs >= 1000000);
8483

@@ -88,7 +87,7 @@ public void testBuildJson() {
8887
Type sourceExtensionJsonType = new TypeToken<Map<String, ?>>(){}.getType();
8988
Map<String, ?> source = gson.fromJson(sourceExtensionJson, sourceExtensionJsonType);
9089
Assert.assertEquals("CLOUD_TOOLS_FOR_ECLIPSE", source.get("console_type"));
91-
Assert.assertEquals("clientId", source.get("client_machine_id"));
90+
Assert.assertEquals("clientId", source.get("client_install_id"));
9291
Assert.assertEquals("SomeEvent", source.get("event_name"));
9392

9493
List<Map<String, String>> eventMetadata =

plugins/com.google.cloud.tools.eclipse.usagetracker/.classpath

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
44
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
55
<classpathentry kind="src" path="src"/>
6+
<classpathentry kind="src" path="generated-sources"/>
67
<classpathentry kind="output" path="target/classes"/>
78
</classpath>

plugins/com.google.cloud.tools.eclipse.usagetracker/build.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ bin.includes = META-INF/,\
33
plugin.xml,\
44
plugin.properties,\
55
.
6-
source.. = src/
6+
source.. = src/,\
7+
generated-sources/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.tools.eclipse.usagetracker;
18+
19+
/**
20+
* Placeholder constants initialized at compile-time.
21+
*/
22+
public class Constants {
23+
24+
public static final String FIRELOG_API_KEY = "@firelog.api.key@";
25+
}

plugins/com.google.cloud.tools.eclipse.usagetracker/pom.xml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,26 @@
1010
<artifactId>com.google.cloud.tools.eclipse.usagetracker</artifactId>
1111
<version>0.1.0-SNAPSHOT</version>
1212
<packaging>eclipse-plugin</packaging>
13+
14+
<build>
15+
<plugins>
16+
<plugin>
17+
<groupId>org.codehaus.mojo</groupId>
18+
<artifactId>templating-maven-plugin</artifactId>
19+
<version>1.0.0</version>
20+
<configuration>
21+
<sourceDirectory>${basedir}/java-templates</sourceDirectory>
22+
<outputDirectory>${basedir}/generated-sources</outputDirectory>
23+
</configuration>
24+
<executions>
25+
<execution>
26+
<id>filter-src</id>
27+
<goals>
28+
<goal>filter-sources</goal>
29+
</goals>
30+
</execution>
31+
</executions>
32+
</plugin>
33+
</plugins>
34+
</build>
1335
</project>

plugins/com.google.cloud.tools.eclipse.usagetracker/src/com/google/cloud/tools/eclipse/usagetracker/AnalyticsPingManager.java

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ public class AnalyticsPingManager {
5353

5454
private static final Logger logger = Logger.getLogger(AnalyticsPingManager.class.getName());
5555

56-
private static final String CLEAR_CUT_COLLECTION_URL = "https://play.google.com/log";
56+
private static final String FIRELOG_COLLECTION_URL =
57+
"https://firebaselogging-pa.googleapis.com/v1/firelog/legacy/log";
5758

5859
private static AnalyticsPingManager instance;
5960

60-
// analytics services
61-
private final String clearCutUrl;
61+
private final String collectionUrl;
6262

6363
// Preference store (should be configuration scoped) from which we get UUID, opt-in status, etc.
6464
private final IEclipsePreferences preferences;
@@ -81,21 +81,20 @@ protected IStatus run(IProgressMonitor monitor) {
8181
private int sequencePosition = 0;
8282

8383
@VisibleForTesting
84-
AnalyticsPingManager(String clearCutUrl, IEclipsePreferences preferences,
84+
AnalyticsPingManager(String collectionUrl, IEclipsePreferences preferences,
8585
ConcurrentLinkedQueue<PingEvent> concurrentLinkedQueue) {
86-
this.clearCutUrl = clearCutUrl;
86+
this.collectionUrl = collectionUrl;
8787
this.preferences = Preconditions.checkNotNull(preferences);
8888
pingEventQueue = concurrentLinkedQueue;
8989
}
9090

9191
public static synchronized AnalyticsPingManager getInstance() {
9292
if (instance == null) {
93-
String clearCutUrl = null;
94-
if (!Platform.inDevelopmentMode()) {
95-
// Enable only in production environment.
96-
clearCutUrl = CLEAR_CUT_COLLECTION_URL;
93+
String collectionUrl = null;
94+
if (!Platform.inDevelopmentMode() && !Constants.FIRELOG_API_KEY.startsWith("@")) {
95+
collectionUrl = FIRELOG_COLLECTION_URL + "?key=" + Constants.FIRELOG_API_KEY;
9796
}
98-
instance = new AnalyticsPingManager(clearCutUrl, AnalyticsPreferences.getPreferenceNode(),
97+
instance = new AnalyticsPingManager(collectionUrl, AnalyticsPreferences.getPreferenceNode(),
9998
new ConcurrentLinkedQueue<PingEvent>());
10099
}
101100
return instance;
@@ -180,7 +179,7 @@ private void sendPingOnShell(Shell parentShell, String eventName, Map<String, St
180179
Preconditions.checkArgument(!Strings.isNullOrEmpty(eventName), "eventName null or empty");
181180
Preconditions.checkNotNull(metadata);
182181

183-
if (clearCutUrl != null) {
182+
if (collectionUrl != null) {
184183
// Note: always enqueue if a user has not seen the opt-in dialog yet; enqueuing itself
185184
// doesn't mean that the event ping will be posted.
186185
if (userHasOptedIn() || !userHasRegisteredOptInStatus()) {
@@ -199,7 +198,7 @@ private void sendPing(PingEvent pingEvent) {
199198
if (userHasOptedIn()) {
200199
try {
201200
String json = jsonEncode(pingEvent);
202-
int resultCode = HttpUtil.sendPost(clearCutUrl, json, "application/json");
201+
int resultCode = HttpUtil.sendPost(collectionUrl, json, "application/json");
203202
if (resultCode >= 300) {
204203
logger.log(Level.FINE, "Failed to POST to Concord with HTTP result " + resultCode);
205204
}
@@ -293,11 +292,11 @@ String jsonEncode(PingEvent event) {
293292

294293
Map<String, Object> clientInfo = new HashMap<>();
295294
clientInfo.put("client_type", "DESKTOP");
296-
clientInfo.put("desktop_client_info", Collections.singletonList(desktopClientInfo));
295+
clientInfo.put("desktop_client_info", desktopClientInfo);
297296

298297
// logs/proto/cloud/concord/concord_event.proto
299298
Map<String, Object> sourceExtension = new HashMap<>();
300-
sourceExtension.put("client_machine_id", getAnonymizedClientId());
299+
sourceExtension.put("client_install_id", getAnonymizedClientId());
301300
sourceExtension.put("console_type", CloudToolsInfo.CONSOLE_TYPE);
302301
sourceExtension.put("event_name", event.eventName);
303302

@@ -321,11 +320,11 @@ String jsonEncode(PingEvent event) {
321320
logEvent.put("source_extension_json", sourceExtensionJsonString);
322321

323322
Map<String, Object> root = new HashMap<>();
324-
root.put("log_source_name", "CONCORD");
323+
root.put("log_source", "CONCORD");
325324
root.put("request_time_ms", System.currentTimeMillis());
326-
root.put("client_info", Collections.singletonList(clientInfo));
327-
root.put("log_event", Collections.singletonList(Collections.singletonList(logEvent)));
325+
root.put("client_info", clientInfo);
326+
root.put("log_event", Collections.singletonList(logEvent));
328327

329-
return gson.toJson(Collections.singletonList(root));
328+
return gson.toJson(root);
330329
}
331330
}

0 commit comments

Comments
 (0)