Skip to content

Commit 84156c6

Browse files
authored
Merge pull request #49 from DeployGate/feat/send_logs_with_buffering
Buffer custom logs until a service connection is established
2 parents 3776369 + 21eca16 commit 84156c6

21 files changed

+1155
-17
lines changed

.circleci/config.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ jobs:
3636
steps:
3737
- checkout
3838
- restore_gradle_cache
39-
- run: ./gradlew sdk:build sdkMock:build --continue
39+
- run: ./gradlew sdk:build sdkMock:build -x sdk:test -x sdkMock:test --continue
4040
- save_gradle_cache
4141
test:
4242
executor: android

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,5 @@ build/
2121
#idea
2222
*.iml
2323
.idea/
24+
25+
.DS_Store

sample/proguard-project.txt

+5
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,8 @@
1919
# public *;
2020
#}
2121
-keep class * extends android.os.IInterface
22+
23+
# to check the R8 results except obfuscation
24+
-keepnames class com.deploygate.sdk.** {
25+
*;
26+
}

sdk/build.gradle

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
apply from: rootProject.file('sdk.build.gradle')
22

3-
android.defaultConfig.consumerProguardFiles 'deploygate-sdk-proguard-rules.txt'
3+
android {
4+
defaultConfig {
5+
consumerProguardFiles 'deploygate-sdk-proguard-rules.txt'
6+
}
7+
}
8+
9+
dependencies {
10+
testImplementation 'org.mockito:mockito-core:4.4.0'
11+
}
412

513
ext {
614
displayName = "DeployGate SDK"

sdk/deploygate-sdk-proguard-rules.txt

+9
Original file line numberDiff line numberDiff line change
@@ -1 +1,10 @@
11
-keep class * extends android.os.IInterface
2+
3+
# To allow removing our logger
4+
-assumenosideeffects class com.deploygate.sdk.internal.Logger {
5+
public static void v(...);
6+
public static void i(...);
7+
public static void w(...);
8+
public static void d(...);
9+
public static void e(...);
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.deploygate.sdk.internal;
2+
3+
public class Config {
4+
public static final boolean DEBUG = true;
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.deploygate.sdk;
2+
3+
import android.os.Bundle;
4+
5+
import com.deploygate.service.DeployGateEvent;
6+
7+
class CustomLog {
8+
public final String type;
9+
public final String body;
10+
private int retryCount;
11+
12+
CustomLog(
13+
String type,
14+
String body
15+
) {
16+
this.type = type;
17+
this.body = body;
18+
this.retryCount = 0;
19+
}
20+
21+
/**
22+
* @return the number of current attempts
23+
*/
24+
int getAndIncrementRetryCount() {
25+
return retryCount++;
26+
}
27+
28+
/**
29+
* @return a bundle to send to the client service
30+
*/
31+
Bundle toExtras() {
32+
Bundle extras = new Bundle();
33+
extras.putSerializable(DeployGateEvent.EXTRA_LOG, body);
34+
extras.putSerializable(DeployGateEvent.EXTRA_LOG_TYPE, type);
35+
return extras;
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package com.deploygate.sdk;
2+
3+
import com.deploygate.sdk.internal.Logger;
4+
5+
public class CustomLogConfiguration {
6+
public enum Backpressure {
7+
/**
8+
* SDK rejects new logs if buffer size is exceeded
9+
*/
10+
PRESERVE_BUFFER,
11+
12+
/**
13+
* SDK drops logs from the oldest if buffer size is exceeded
14+
*/
15+
DROP_BUFFER_BY_OLDEST
16+
}
17+
18+
/**
19+
* the log buffer is required until DeployGate client app receives BOOT_COMPLETED broadcast.
20+
* <p>
21+
* This is an experimental value.
22+
* <p>
23+
* - 10 seconds until boot-completed
24+
* - 10 logs per 1 seconds
25+
* - plus some buffer
26+
*/
27+
private static final int DEFAULT_BUFFER_SIZE = 150;
28+
private static final int MAX_BUFFER_SIZE = DEFAULT_BUFFER_SIZE;
29+
30+
public final Backpressure backpressure;
31+
public final int bufferSize;
32+
33+
/**
34+
* Do not bypass {@link Builder} to instantiate this class.
35+
*
36+
* @see Builder
37+
*/
38+
private CustomLogConfiguration(
39+
Backpressure backpressure,
40+
int bufferSize
41+
) {
42+
this.backpressure = backpressure;
43+
this.bufferSize = bufferSize;
44+
}
45+
46+
public static class Builder {
47+
private Backpressure backpressure = Backpressure.DROP_BUFFER_BY_OLDEST;
48+
private int bufferSize = DEFAULT_BUFFER_SIZE;
49+
50+
/**
51+
* @param backpressure
52+
* the strategy of the backpressure in the log buffer
53+
*
54+
* @return self
55+
*
56+
* @see Backpressure
57+
*/
58+
public Builder setBackpressure(Backpressure backpressure) {
59+
if (backpressure == null) {
60+
throw new IllegalArgumentException("backpressure must be non-null");
61+
}
62+
63+
this.backpressure = backpressure;
64+
return this;
65+
}
66+
67+
/**
68+
* @param bufferSize
69+
* the max size of the log buffer
70+
*
71+
* @return self
72+
*/
73+
public Builder setBufferSize(int bufferSize) {
74+
if (bufferSize <= 0) {
75+
throw new IllegalArgumentException("buffer size must be greater than 0");
76+
}
77+
78+
if (bufferSize > MAX_BUFFER_SIZE) {
79+
Logger.w("buffer size is exceeded %d so it's rounded to %d", bufferSize, MAX_BUFFER_SIZE);
80+
bufferSize = MAX_BUFFER_SIZE;
81+
}
82+
83+
this.bufferSize = bufferSize;
84+
return this;
85+
}
86+
87+
public CustomLogConfiguration build() {
88+
return new CustomLogConfiguration(backpressure, bufferSize);
89+
}
90+
}
91+
}

0 commit comments

Comments
 (0)