Skip to content

Commit

Permalink
Add support for productId in transport (#5485)
Browse files Browse the repository at this point in the history
Allow logging the productId together with the event in transport.

For googlers b/303136538
  • Loading branch information
rlazo authored Oct 27, 2023
1 parent 4d283b3 commit ccc7339
Show file tree
Hide file tree
Showing 24 changed files with 415 additions and 70 deletions.
3 changes: 1 addition & 2 deletions transport/transport-api/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
# Unreleased


* [feature] Added new APIs to support passing a product id when logging an event.
2 changes: 1 addition & 1 deletion transport/transport-api/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
# See the License for the specific language governing permissions and
# limitations under the License.

version=3.0.1
version=3.1.0
latestReleasedVersion=3.0.0
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,54 @@ public abstract class Event<T> {

public abstract Priority getPriority();

@Nullable
public abstract ProductData getProductData();

public static <T> Event<T> ofData(int code, T payload, @Nullable ProductData productData) {
return new AutoValue_Event<>(code, payload, Priority.DEFAULT, productData);
}

public static <T> Event<T> ofData(int code, T payload) {
return new AutoValue_Event<>(code, payload, Priority.DEFAULT);
return new AutoValue_Event<>(code, payload, Priority.DEFAULT, null);
}

public static <T> Event<T> ofData(T payload, @Nullable ProductData productData) {
return new AutoValue_Event<>(null, payload, Priority.DEFAULT, productData);
}

public static <T> Event<T> ofData(T payload) {
return new AutoValue_Event<>(null, payload, Priority.DEFAULT);
return new AutoValue_Event<>(null, payload, Priority.DEFAULT, null);
}

public static <T> Event<T> ofTelemetry(int code, T value, @Nullable ProductData productData) {
return new AutoValue_Event<>(code, value, Priority.VERY_LOW, productData);
}

public static <T> Event<T> ofTelemetry(int code, T value) {
return new AutoValue_Event<>(code, value, Priority.VERY_LOW);
return new AutoValue_Event<>(code, value, Priority.VERY_LOW, null);
}

public static <T> Event<T> ofTelemetry(T value, @Nullable ProductData productData) {
return new AutoValue_Event<>(null, value, Priority.VERY_LOW, productData);
}

public static <T> Event<T> ofTelemetry(T value) {
return new AutoValue_Event<>(null, value, Priority.VERY_LOW);
return new AutoValue_Event<>(null, value, Priority.VERY_LOW, null);
}

public static <T> Event<T> ofUrgent(int code, T value, @Nullable ProductData productData) {
return new AutoValue_Event<>(code, value, Priority.HIGHEST, productData);
}

public static <T> Event<T> ofUrgent(int code, T value) {
return new AutoValue_Event<>(code, value, Priority.HIGHEST);
return new AutoValue_Event<>(code, value, Priority.HIGHEST, null);
}

public static <T> Event<T> ofUrgent(T value, @Nullable ProductData productData) {
return new AutoValue_Event<>(null, value, Priority.HIGHEST, productData);
}

public static <T> Event<T> ofUrgent(T value) {
return new AutoValue_Event<>(null, value, Priority.HIGHEST);
return new AutoValue_Event<>(null, value, Priority.HIGHEST, null);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.android.datatransport;

import androidx.annotation.Nullable;
import com.google.auto.value.AutoValue;

@AutoValue
public abstract class ProductData {
@Nullable
public abstract Integer getProductId();

public static ProductData withProductId(@Nullable Integer productId) {
return new AutoValue_ProductData(productId);
}
}
3 changes: 1 addition & 2 deletions transport/transport-backend-cct/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
# Unreleased


* [feature] Added support for product id.
2 changes: 1 addition & 1 deletion transport/transport-backend-cct/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
# See the License for the specific language governing permissions and
# limitations under the License.

version=3.1.10
version=3.2.0
latestReleasedVersion=3.1.9
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
import com.google.android.datatransport.cct.internal.AndroidClientInfo;
import com.google.android.datatransport.cct.internal.BatchedLogRequest;
import com.google.android.datatransport.cct.internal.ClientInfo;
import com.google.android.datatransport.cct.internal.ComplianceData;
import com.google.android.datatransport.cct.internal.ExternalPRequestContext;
import com.google.android.datatransport.cct.internal.ExternalPrivacyContext;
import com.google.android.datatransport.cct.internal.LogEvent;
import com.google.android.datatransport.cct.internal.LogRequest;
import com.google.android.datatransport.cct.internal.LogResponse;
Expand Down Expand Up @@ -280,6 +283,19 @@ private BatchedLogRequest getRequestBody(BackendRequest backendRequest) {
if (eventInternal.getCode() != null) {
event.setEventCode(eventInternal.getCode());
}
if (eventInternal.getProductId() != null) {
event.setComplianceData(
ComplianceData.builder()
.setPrivacyContext(
ExternalPrivacyContext.builder()
.setPrequest(
ExternalPRequestContext.builder()
.setOriginAssociatedProductId(eventInternal.getProductId())
.build())
.build())
.setProductIdOrigin(ComplianceData.ProductIdOrigin.EVENT_OVERRIDE)
.build());
}
logEvents.add(event.build());
}
requestBuilder.setLogEvents(logEvents);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.android.datatransport.cct.internal;

import android.util.SparseArray;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.auto.value.AutoValue;

@AutoValue
public abstract class ComplianceData {
public enum ProductIdOrigin {
NOT_SET(0),
EVENT_OVERRIDE(5);

private static final SparseArray<ProductIdOrigin> valueMap = new SparseArray<>();

private final int value;

static {
valueMap.put(0, NOT_SET);
valueMap.put(5, EVENT_OVERRIDE);
}

ProductIdOrigin(int value) {
this.value = value;
}

public int getValue() {
return value;
}

@Nullable
public static ProductIdOrigin forNumber(int value) {
return valueMap.get(value);
}
}

@Nullable
public abstract ExternalPrivacyContext getPrivacyContext();

@Nullable
public abstract ProductIdOrigin getProductIdOrigin();

@NonNull
public static Builder builder() {
return new AutoValue_ComplianceData.Builder();
}

@AutoValue.Builder
public abstract static class Builder {
@NonNull
public abstract Builder setPrivacyContext(@Nullable ExternalPrivacyContext value);

@NonNull
public abstract Builder setProductIdOrigin(@Nullable ProductIdOrigin value);

@NonNull
public abstract ComplianceData build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.android.datatransport.cct.internal;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.auto.value.AutoValue;

@AutoValue
public abstract class ExternalPRequestContext {
@Nullable
public abstract Integer getOriginAssociatedProductId();

@NonNull
public static Builder builder() {
return new AutoValue_ExternalPRequestContext.Builder();
}

@AutoValue.Builder
public abstract static class Builder {
@NonNull
public abstract Builder setOriginAssociatedProductId(@Nullable Integer value);

@NonNull
public abstract ExternalPRequestContext build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.android.datatransport.cct.internal;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.auto.value.AutoValue;

@AutoValue
public abstract class ExternalPrivacyContext {
@Nullable
public abstract ExternalPRequestContext getPrequest();

@NonNull
public static Builder builder() {
return new AutoValue_ExternalPrivacyContext.Builder();
}

@AutoValue.Builder
public abstract static class Builder {
@NonNull
public abstract Builder setPrequest(@Nullable ExternalPRequestContext value);

@NonNull
public abstract ExternalPrivacyContext build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ public abstract class LogEvent {
@Nullable
public abstract Integer getEventCode();

@Nullable
public abstract ComplianceData getComplianceData();

public abstract long getEventUptimeMs();

@SuppressWarnings("mutable")
Expand Down Expand Up @@ -63,6 +66,9 @@ public abstract static class Builder {
@NonNull
public abstract Builder setEventCode(@Nullable Integer value);

@NonNull
public abstract Builder setComplianceData(@Nullable ComplianceData value);

@NonNull
public abstract Builder setEventUptimeMs(long value);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ public class CctTransportBackendTest {
private static final String JSON_PAYLOAD = "{\"hello\": false}";
private static final String JSON_PAYLOAD_ESCAPED = "{\\\"hello\\\": false}";
private static final int CODE = 5;

private static final int PRODUCT_ID = 98765;
private static final String TEST_NAME = "hello";
private static final Encoding PROTOBUF_ENCODING = Encoding.of("proto");
private static final Encoding JSON_ENCODING = Encoding.of("json");
Expand Down Expand Up @@ -110,6 +112,7 @@ private BackendRequest getCCTBackendRequest(String transportName, CCTDestination
new EncodedPayload(
JSON_ENCODING, JSON_PAYLOAD.getBytes(Charset.defaultCharset())))
.setCode(CODE)
.setProductId(PRODUCT_ID)
.build())))
.setExtras(destination.getExtras())
.build();
Expand Down Expand Up @@ -185,6 +188,13 @@ public void testCCTSuccessLoggingRequest() {
activeNetworkInfo.getSubtype()))))
.withRequestBody(notMatching("$[?(@.logRequest[0].logEvent[0].eventCode)]"))
.withRequestBody(matchingJsonPath("$[?(@.logRequest[0].logEvent[1].eventCode == 5)]"))
.withRequestBody(
matchingJsonPath("$[?(@.logRequest[0].logEvent[0].complianceData)]", absent()))
.withRequestBody(
matchingJsonPath(
String.format(
"$[?(@.logRequest[0].logEvent[1].complianceData.privacyContext.prequest.originAssociatedProductId == %s)]",
PRODUCT_ID)))
.withRequestBody(
matchingJsonPath(
String.format(
Expand Down
8 changes: 3 additions & 5 deletions transport/transport-backend-cct/transport-backend-cct.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ android {
}

dependencies {
implementation 'com.google.android.datatransport:transport-api:3.0.0'
implementation 'com.google.android.datatransport:transport-runtime:3.1.8'
implementation project(':transport:transport-api')
implementation project(':transport:transport-runtime')
implementation 'com.google.firebase:firebase-encoders:17.0.0'
implementation 'com.google.firebase:firebase-encoders-json:18.0.0'
implementation 'androidx.annotation:annotation:1.1.0'
Expand All @@ -68,9 +68,7 @@ dependencies {
testImplementation "androidx.test:core:$androidxTestCoreVersion"
testImplementation "com.google.truth:truth:$googleTruthVersion"
testImplementation 'com.google.truth.extensions:truth-proto-extension:1.0'
testImplementation 'com.github.tomakehurst:wiremock:2.26.3'
//Android compatible version of Apache httpclient.
testImplementation 'org.apache.httpcomponents:httpclient-android:4.3.5.1'
testImplementation 'com.github.tomakehurst:wiremock:3.0.1'
// Keep Robolectric to 4.3.1 for httpclient and TelephonyManager compatibility.
testImplementation "org.robolectric:robolectric:4.3.1"
testImplementation 'junit:junit:4.13.1'
Expand Down
1 change: 1 addition & 0 deletions transport/transport-runtime/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# Unreleased
* [unchanged] Updated internal Dagger dependency.
* [feature] Added support for product id.
2 changes: 1 addition & 1 deletion transport/transport-runtime/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

version=3.1.10
version=3.2.0
latestReleasedVersion=3.1.9
android.enableUnitTestBinaryResources=true
Loading

0 comments on commit ccc7339

Please sign in to comment.