Skip to content

Commit cb40f3b

Browse files
authored
Merge pull request #448 from YangSen-qn/V8.1.1
优化日志统计
2 parents 5f8f6f4 + 4a19c5b commit cb40f3b

25 files changed

+360
-125
lines changed

.github/workflows/ci-test.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
on: [push]
1+
on:
2+
push:
3+
paths-ignore:
4+
- '**.md'
5+
pull_request:
6+
paths-ignore:
7+
- '**.md'
28
name: Run Test Cases
39
jobs:
410
test-on-mac:

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
#Changelog
2+
## 8.1.1 (2021-01-06)
3+
* 优化日志统计
4+
25
## 8.1.0 (2020-12-29)
36
* 支持分片上传V2
47
* 支持自定义meta

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ https://github.com/qiniudemo/qiniu-lab-android
2828
| 7.0.7 | Android 2.2+ | android-async-http 1.4.8 |
2929

3030
### 注意
31-
* 推荐使用最新版:8.1.0
31+
* 推荐使用最新版:8.1.1
3232
* AndroidNetwork.getMobileDbm()可以获取手机信号强度,需要如下权限(API>=18时生效)
3333
```
3434
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

library/src/androidTest/java/com/qiniu/android/BaseTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.qiniu.android;
22

3+
import com.qiniu.android.utils.LogUtil;
4+
35
import junit.framework.TestCase;
46

57
import java.util.Date;
@@ -11,6 +13,12 @@
1113
*/
1214
public class BaseTest extends TestCase {
1315

16+
@Override
17+
protected void setUp() throws Exception {
18+
super.setUp();
19+
LogUtil.enableLog(true);
20+
}
21+
1422
private long maxWaitTimestamp = 0;
1523
/**
1624
* waitCondition: 等待条件

library/src/androidTest/java/com/qiniu/android/CancelTest.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package com.qiniu.android;
22

3-
import android.test.InstrumentationTestCase;
4-
53
import com.qiniu.android.common.FixedZone;
6-
import com.qiniu.android.common.Zone;
74
import com.qiniu.android.common.ZoneInfo;
85
import com.qiniu.android.common.ZonesInfo;
96
import com.qiniu.android.http.ResponseInfo;
@@ -50,7 +47,7 @@ public void testFile() throws Throwable {
5047
checkTemp(ts, "testFile");
5148
}
5249

53-
public void testMultiFileA() throws Throwable {
50+
public void testMultiFile() throws Throwable {
5451
Temp[] ts = new Temp[]{templateFile(400, 0.2), templateFile(700, 0.2), templateFile(1024, 0.51), templateFile(4 * 1024, 0.5), templateFile(8 * 1024 + 1, 0.6)};
5552
checkTemp(ts, "testFile");
5653
}

library/src/androidTest/java/com/qiniu/android/LogUtilsTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ public class LogUtilsTest extends BaseTest {
66

77
public void testLog(){
88

9+
LogUtil.enableLog(true);
10+
911
Throwable throwable = new Throwable();
1012

1113
assertTrue(LogUtil.v("log") > 0);

library/src/main/java/com/qiniu/android/collect/ReportItem.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ public String toJson(){
7373
public static final String RequestKeySDKName = "sdk_name";
7474
public static final String RequestKeySDKVersion = "sdk_version";
7575
public static final String RequestKeyClientTime = "client_time";
76+
public static final String RequestKeyHttpClient = "http_client";
77+
public static final String RequestKeyHttpClientVersion = "http_client_version";
7678
public static final String RequestKeyNetworkType = "network_type";
7779
public static final String RequestKeySignalStrength = "signal_strength";
7880
public static final String RequestKeyPrefetchedDnsSource = "prefetched_dns_source";
@@ -82,6 +84,8 @@ public String toJson(){
8284
// 分块上传统计⽇志
8385
public static final String BlockKeyLogType = "log_type";
8486
public static final String BlockKeyUpTime = "up_time";
87+
public static final String BlockKeyTargetBucket = "target_bucket";
88+
public static final String BlockKeyTargetKey = "target_key";
8589
public static final String BlockKeyTargetRegionId = "target_region_id";
8690
public static final String BlockKeyCurrentRegionId = "current_region_id";
8791
public static final String BlockKeyTotalElapsedTime = "total_elapsed_time";
@@ -92,6 +96,10 @@ public String toJson(){
9296
public static final String BlockKeyTid = "tid";
9397
public static final String BlockKeyUpApiVersion = "up_api_version";
9498
public static final String BlockKeyClientTime = "client_time";
99+
public static final String BlockKeyOsName = "os_name";
100+
public static final String BlockKeyOsVersion = "os_version";
101+
public static final String BlockKeySDKName = "sdk_name";
102+
public static final String BlockKeySDKVersion = "sdk_version";
95103

96104

97105
// 上传质量统计
@@ -107,6 +115,10 @@ public String toJson(){
107115
public static final String QualityKeyCloudType = "cloud_type";
108116
public static final String QualityKeyErrorType = "error_type";
109117
public static final String QualityKeyErrorDescription = "error_description";
118+
public static final String QualityKeyOsName = "os_name";
119+
public static final String QualityKeyOsVersion = "os_version";
120+
public static final String QualityKeySDKName = "sdk_name";
121+
public static final String QualityKeySDKVersion = "sdk_version";
110122

111123
public static String requestReportStatusCode(ResponseInfo responseInfo){
112124
if (responseInfo == null){

library/src/main/java/com/qiniu/android/collect/UploadInfoReporter.java

Lines changed: 48 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.qiniu.android.storage.UpToken;
99
import com.qiniu.android.utils.AsyncRun;
1010
import com.qiniu.android.utils.LogUtil;
11+
import com.qiniu.android.utils.StringUtils;
1112

1213
import org.json.JSONObject;
1314

@@ -35,86 +36,88 @@ public class UploadInfoReporter {
3536

3637
private static UploadInfoReporter instance = new UploadInfoReporter();
3738

38-
private UploadInfoReporter(){}
39+
private UploadInfoReporter() {
40+
}
3941

40-
public static UploadInfoReporter getInstance(){
42+
public static UploadInfoReporter getInstance() {
4143
return instance;
4244
}
4345

44-
public synchronized void report(ReportItem reportItem,
45-
final String tokenString){
46-
if (reportItem == null){
46+
public synchronized void report(final ReportItem reportItem,
47+
final String tokenString) {
48+
if (reportItem == null) {
4749
return;
4850
}
4951

5052
final String jsonString = reportItem.toJson();
51-
if (!checkReportAvailable() || jsonString == null){
53+
if (!checkReportAvailable() || jsonString == null) {
5254
return;
5355
}
5456

5557
AsyncRun.runInBack(new Runnable() {
5658
@Override
5759
public void run() {
58-
synchronized (this){
60+
LogUtil.i("up log:" + StringUtils.toNonnullString(jsonString));
61+
synchronized (this) {
5962
saveReportJsonString(jsonString);
6063
reportToServerIfNeeded(tokenString);
6164
}
6265
}
6366
});
6467
}
6568

66-
public void clean(){
69+
public void clean() {
6770
cleanRecorderFile();
6871
cleanTempLogFile();
6972
}
7073

71-
private void cleanRecorderFile(){
72-
if (recorderFile.exists()){
74+
private void cleanRecorderFile() {
75+
if (recorderFile.exists()) {
7376
recorderFile.delete();
7477
}
7578
}
7679

77-
private void cleanTempLogFile(){
78-
if (recorderTempFile.exists()){
80+
private void cleanTempLogFile() {
81+
if (recorderTempFile.exists()) {
7982
recorderTempFile.delete();
8083
}
8184
}
8285

83-
private boolean checkReportAvailable(){
84-
if (!config.isReportEnable){
86+
private boolean checkReportAvailable() {
87+
if (!config.isReportEnable) {
8588
return false;
8689
}
87-
if (config.maxRecordFileSize <= config.uploadThreshold){
90+
if (config.maxRecordFileSize <= config.uploadThreshold) {
8891
LogUtil.e("maxRecordFileSize must be larger than uploadThreshold");
8992
return false;
9093
}
9194
return true;
9295
}
9396

94-
private void saveReportJsonString(final String jsonString){
97+
private void saveReportJsonString(final String jsonString) {
9598

96-
if (!recordDirectory.exists() && !recordDirectory.mkdirs()){
99+
if (!recordDirectory.exists() && !recordDirectory.mkdirs()) {
97100
return;
98101
}
99102

100-
if (!recordDirectory.isDirectory()){
103+
if (!recordDirectory.isDirectory()) {
101104
LogUtil.e("recordDirectory is not a directory");
102105
return;
103106
}
104107

105-
if (!recorderFile.exists()){
108+
if (!recorderFile.exists()) {
106109
try {
107110
boolean isSuccess = recorderFile.createNewFile();
108-
if (!isSuccess){
111+
if (!isSuccess) {
109112
return;
110113
}
111114
} catch (IOException e) {
112115
e.printStackTrace();
113116
return;
114117
}
115118
}
116-
117-
if (recorderFile.length() > config.maxRecordFileSize){
119+
120+
if (recorderFile.length() > config.maxRecordFileSize) {
118121
return;
119122
}
120123

@@ -129,54 +132,55 @@ private void saveReportJsonString(final String jsonString){
129132
if (fos != null) {
130133
try {
131134
fos.close();
132-
} catch (IOException ignored) {}
135+
} catch (IOException ignored) {
136+
}
133137
}
134138
}
135139
}
136140

137-
private void reportToServerIfNeeded(String tokenString){
138-
if (isReporting){
141+
private void reportToServerIfNeeded(String tokenString) {
142+
if (isReporting) {
139143
return;
140144
}
141145
boolean needToReport = false;
142146
long currentTime = new Date().getTime();
143147

144-
if (recorderTempFile.exists()){
148+
if (recorderTempFile.exists()) {
145149
needToReport = true;
146150
} else if ((recorderFile.length() > config.uploadThreshold)
147-
|| (lastReportTime == 0 || (currentTime - lastReportTime) > config.interval * 60)){
151+
|| (lastReportTime == 0 || (currentTime - lastReportTime) > config.interval * 60)) {
148152
boolean isSuccess = recorderFile.renameTo(recorderTempFile);
149153
if (isSuccess) {
150154
needToReport = true;
151155
}
152156
}
153-
if (needToReport && !this.isReporting){
157+
if (needToReport && !this.isReporting) {
154158
reportToServer(tokenString);
155159
}
156160
}
157161

158-
private void reportToServer(String tokenString){
162+
private void reportToServer(String tokenString) {
159163

160164
isReporting = true;
161165

162166
RequestTransaction transaction = createUploadRequestTransaction(tokenString);
163-
if (transaction == null){
167+
if (transaction == null) {
164168
return;
165169
}
166170

167171
byte[] logData = getLogData();
168-
if (logData == null || logData.length == 0){
172+
if (logData == null || logData.length == 0) {
169173
return;
170174
}
171175

172176
transaction.reportLog(logData, X_Log_Client_Id, true, new RequestTransaction.RequestCompleteHandler() {
173177
@Override
174178
public void complete(ResponseInfo responseInfo, UploadRegionRequestMetrics requestMetrics, JSONObject response) {
175-
if (responseInfo.isOK()){
179+
if (responseInfo.isOK()) {
176180
lastReportTime = new Date().getTime();
177181
if (X_Log_Client_Id == null
178182
&& responseInfo.responseHeader != null
179-
&& responseInfo.responseHeader.get("x-log-client-id") != null){
183+
&& responseInfo.responseHeader.get("x-log-client-id") != null) {
180184
X_Log_Client_Id = responseInfo.responseHeader.get("x-log-client-id");
181185
}
182186
cleanTempLogFile();
@@ -189,43 +193,44 @@ public void complete(ResponseInfo responseInfo, UploadRegionRequestMetrics reque
189193

190194
}
191195

192-
private byte[] getLogData(){
193-
if (recorderTempFile == null || recorderTempFile.length() == 0){
196+
private byte[] getLogData() {
197+
if (recorderTempFile == null || recorderTempFile.length() == 0) {
194198
return null;
195199
}
196200

197-
int fileSize = (int)recorderTempFile.length();
201+
int fileSize = (int) recorderTempFile.length();
198202
RandomAccessFile randomAccessFile = null;
199203
byte[] data = null;
200204
try {
201205
randomAccessFile = new RandomAccessFile(recorderTempFile, "r");
202206
ByteArrayOutputStream out = new ByteArrayOutputStream(fileSize);
203207
int len = 0;
204208
byte[] buff = new byte[fileSize];
205-
while ((len = randomAccessFile.read(buff)) >= 0){
209+
while ((len = randomAccessFile.read(buff)) >= 0) {
206210
out.write(buff, 0, len);
207211
}
208212
data = out.toByteArray();
209213
} catch (FileNotFoundException ignored) {
210214
} catch (IOException e) {
211215
data = null;
212216
} finally {
213-
if (randomAccessFile != null){
217+
if (randomAccessFile != null) {
214218
try {
215219
randomAccessFile.close();
216-
} catch (IOException e){}
220+
} catch (IOException e) {
221+
}
217222
}
218223
}
219224

220225
return data;
221226
}
222227

223-
private RequestTransaction createUploadRequestTransaction(String tokenString){
224-
if (config == null){
228+
private RequestTransaction createUploadRequestTransaction(String tokenString) {
229+
if (config == null) {
225230
return null;
226231
}
227232
UpToken token = UpToken.parse(tokenString);
228-
if (token == null){
233+
if (token == null) {
229234
return null;
230235
}
231236
ArrayList<String> hosts = new ArrayList<>();
@@ -235,7 +240,7 @@ private RequestTransaction createUploadRequestTransaction(String tokenString){
235240
return transaction;
236241
}
237242

238-
private void destroyTransactionResource(){
243+
private void destroyTransactionResource() {
239244
transaction = null;
240245
}
241246
}

library/src/main/java/com/qiniu/android/common/Constants.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33

44
public final class Constants {
5-
public static final String VERSION = "8.1.0";
5+
public static final String VERSION = "8.1.1";
66

77
public static final String UTF_8 = "utf-8";
88
}

library/src/main/java/com/qiniu/android/http/UserAgent.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import com.qiniu.android.common.Constants;
77
import com.qiniu.android.utils.StringUtils;
8+
import com.qiniu.android.utils.Utils;
89

910
import java.nio.charset.Charset;
1011
import java.util.Locale;
@@ -35,7 +36,8 @@ private static String genId() {
3536
}
3637

3738
static String getUserAgent(String id) {
38-
return format("QiniuAndroid/%s (%s; %s; %s", Constants.VERSION,
39+
String addition = Utils.isDebug() ? "_Debug" : "";
40+
return format("QiniuAndroid%s/%s (%s; %s; %s",addition, Constants.VERSION,
3941
osVersion(), device(), id);
4042
}
4143

0 commit comments

Comments
 (0)