├── CHANGELOG.md
├── LICENSE
├── README.md
├── pom.xml
└── src
├── main
└── java
│ └── cn
│ └── thinkingdata
│ └── analytics
│ ├── BatchConsumer.java
│ ├── DebugConsumer.java
│ ├── LoggerConsumer.java
│ ├── TDAnalytics.java
│ ├── TDBatchConsumer.java
│ ├── TDConstData.java
│ ├── TDDebugConsumer.java
│ ├── TDExample.java
│ ├── TDLoggerConsumer.java
│ ├── ThinkingDataAnalytics.java
│ ├── exception
│ ├── IllegalDataException.java
│ ├── InvalidArgumentException.java
│ └── NeedRetryException.java
│ ├── inter
│ ├── DynamicSuperPropertiesTracker.java
│ ├── ITALogger.java
│ ├── ITDAnalytics.java
│ ├── ITDConsumer.java
│ ├── ITDLogger.java
│ └── IThinkingDataAnalytics.java
│ ├── request
│ ├── TDBaseRequest.java
│ ├── TDBatchRequest.java
│ ├── TDDebugRequest.java
│ └── TDHttpRequestClient.java
│ └── util
│ ├── TDCommonUtil.java
│ ├── TDLogger.java
│ └── TDPropertyUtil.java
└── test
└── java
└── cn
└── thinkingdata
└── analytics
└── test
├── LoggerConsumerTest.java
├── functionTest
├── AssembleEventTest.java
├── TemporaryConsumer.java
├── TemporaryEvent.java
└── UtilTest.java
└── performanceTest
└── PerformanceTest.java
/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | ### v3.0.2-beta.2
2 | **Date:** 2024/08/08
3 |
4 | **Notes:**
5 |
6 | * Improve SDK stability
7 |
8 | ### v3.0.2-beta.1
9 | **Date:** 2024/06/06
10 |
11 | **Notes:**
12 |
13 | * The minimum supported JDK is 8
14 | * Upgrade to fastjson2
15 |
16 | ### v3.0.1
17 | **Date:** 2024/05/21
18 |
19 | **Notes:**
20 |
21 | * The debug mode supports data format verification
22 |
23 | ### v3.0.0
24 | **Date:** 2023/11/29
25 |
26 | **Notes:**
27 |
28 | * Release the official version
29 |
30 | ### v3.0.0-beta.1
31 | **Date:** 2023/09/20
32 |
33 | **Notes:**
34 |
35 | * Using the new API
36 |
37 | ### v2.1.2
38 | **Date:** 2023/09/19
39 |
40 | **Notes:**
41 |
42 | * Set SDK to official version
43 |
44 | ### v2.1.2-beta.3
45 | **Date:** 2023/07/17
46 |
47 | **Notes:**
48 |
49 | * Improve SDK stability
50 |
51 | ### v2.1.2-beta.2
52 | **Date:** 2023/04/20
53 |
54 | **Notes:**
55 |
56 | * Tidy test case
57 |
58 | ### v2.1.1
59 | **Date:** 2022/11/15
60 |
61 | **Notes:**
62 |
63 | * Add debug model
64 |
65 | ### v2.1.0
66 | **Date:** 2022/08/08
67 |
68 | **Notes:**
69 |
70 | * In LoggerConsumer and BatchConsumer mode, the correctness of attributes is not checked by default
71 | * Added log switch
72 | * Optimize some codes
73 |
74 | ### v2.0.4
75 | **Date:** 2022/05/27
76 |
77 | **Notes:**
78 |
79 | * Update to concurrentHasMap
80 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "{}"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright 2019 thinking-analytics / data-collector / client-sdk
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # ThinkingData SDK for Java
2 | 
3 |
4 | This is the [ThinkingData](https://www.thinkingdata.cn)™ SDK for Java. Documentation is available on our help center in the following languages:
5 |
6 | - [English](https://docs.thinkingdata.cn/ta-manual/latest/en/installation/installation_menu/server_sdk/java_sdk_installation/java_sdk_installation.html)
7 | - [中文](https://docs.thinkingdata.cn/ta-manual/latest/installation/installation_menu/server_sdk/java_sdk_installation/java_sdk_installation.html)
8 | - [日本語](https://docs.thinkingdata.cn/ta-manual/latest/ja/installation/installation_menu/server_sdk/java_sdk_installation/java_sdk_installation.html)
9 |
10 | ---
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
3 | 4.0.0
4 |
5 | cn.thinkingdata
6 | thinkingdatasdk
7 |
8 | 3.0.2
9 |
10 |
11 |
12 | thinkingdatasdk
13 | http://www.thinkinggame.cn
14 |
15 | org.sonatype.oss
16 | oss-parent
17 | 7
18 |
19 |
20 |
21 | The Apache Software License, Version 2.0
22 | http://www.apache.org/licenses/LICENSE-2.0.txt
23 |
24 |
25 |
26 |
27 | https://github.com/ThinkingDataAnalytics/java-sdk
28 | https://github.com/ThinkingDataAnalytics/java-sdk
29 | https://github.com/ThinkingDataAnalytics/java-sdk
30 |
31 |
32 |
33 | thinkingdata
34 | sdk@thinkingdata.cn
35 | https://www.thinkingdata.cn
36 |
37 |
38 |
39 |
40 | UTF-8
41 |
42 |
43 |
44 |
45 | com.alibaba
46 | fastjson
47 | 2.0.32
48 |
49 |
50 | org.anarres.lzo
51 | lzo-core
52 | 1.0.5
53 |
54 |
55 | org.apache.httpcomponents
56 | httpclient
57 | 4.5.13
58 |
59 |
60 | net.jpountz.lz4
61 | lz4
62 | 1.3.0
63 |
64 |
65 |
66 | junit
67 | junit
68 | 4.12
69 | test
70 |
71 |
72 |
73 |
74 |
75 | deploy
76 |
77 |
78 | org.apache.maven.plugins
79 | maven-compiler-plugin
80 | 3.5.1
81 |
82 | 8
83 | 8
84 | UTF-8
85 |
86 |
87 |
88 | org.apache.maven.plugins
89 | maven-javadoc-plugin
90 | 2.10.4
91 |
92 | true
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 | ossrh
101 |
102 |
103 | releases
104 | https://oss.sonatype.org/service/local/staging/deploy/maven2/
105 |
106 |
107 | snapshots
108 | https://oss.sonatype.org/content/repositories/snapshots/
109 |
110 |
111 |
112 |
113 | compile
114 |
115 |
116 |
117 | org.apache.maven.plugins
118 | maven-source-plugin
119 | 3.0.1
120 |
121 |
122 | package
123 |
124 | jar-no-fork
125 |
126 |
127 |
128 |
129 |
130 |
131 | org.apache.maven.plugins
132 | maven-javadoc-plugin
133 | 2.10.4
134 |
135 |
136 | package
137 |
138 | jar
139 |
140 |
141 |
142 |
143 |
144 |
145 | org.apache.maven.plugins
146 | maven-gpg-plugin
147 | 1.6
148 |
149 |
150 | sign-artifacts
151 | verify
152 |
153 | sign
154 |
155 |
156 |
157 |
158 |
159 |
160 |
161 |
162 |
163 |
--------------------------------------------------------------------------------
/src/main/java/cn/thinkingdata/analytics/BatchConsumer.java:
--------------------------------------------------------------------------------
1 | package cn.thinkingdata.analytics;
2 |
3 | import java.net.URISyntaxException;
4 |
5 | /**
6 | * @deprecated please use TDBatchConsumer instead
7 | */
8 | @Deprecated
9 | public class BatchConsumer extends TDBatchConsumer {
10 |
11 | public BatchConsumer(String serverUrl, String appId) throws URISyntaxException {
12 | super(serverUrl, appId, 20, 0, false, 0, "gzip", 0, true);
13 | }
14 |
15 | /**
16 | * init BatchConsumer with serverUrl and appId
17 | *
18 | * @param serverUrl serverUrl
19 | * @param appId APP ID
20 | * @param isThrowException throw exception or not
21 | * @throws URISyntaxException exception
22 | */
23 | public BatchConsumer(String serverUrl, String appId, boolean isThrowException) throws URISyntaxException {
24 | super(serverUrl, appId, 20, 0, false, 0, "gzip", 0, isThrowException);
25 | }
26 |
27 | /**
28 | * @param serverUrl serverUrl
29 | * @param appId APP ID
30 | * @param config BatchConsumer config
31 | * @throws URISyntaxException exception
32 | */
33 | public BatchConsumer(String serverUrl, String appId, Config config) throws URISyntaxException {
34 | super(serverUrl, appId, config.batchSize, config.timeout, config.autoFlush, config.interval, config.compress, config.maxCacheSize, config.isThrowException);
35 | }
36 |
37 | /**
38 | * init BatchConsumer
39 | *
40 | * @param serverUrl serverUrl
41 | * @param appId APP ID
42 | * @param batchSize flush event count each time
43 | * @param timeout http timeout (Unit: mill second)
44 | * @param autoFlush is auto flush or not
45 | * @param interval auto flush spacing (Unit: second)
46 | * @throws URISyntaxException exception
47 | */
48 | public BatchConsumer(String serverUrl, String appId, int batchSize, int timeout, boolean autoFlush, int interval) throws URISyntaxException {
49 | super(serverUrl, appId, batchSize, timeout, autoFlush, interval, "gzip", 0, true);
50 | }
51 |
52 | /**
53 | * init BatchConsumer
54 | *
55 | * @param serverUrl serverUrl
56 | * @param appId APP ID
57 | * @param batchSize flush event count each time
58 | * @param timeout http timeout (Unit: mill second)
59 | * @param autoFlush is auto flush or not
60 | * @param interval auto flush spacing (Unit: second)
61 | * @param compress compress type
62 | * @throws URISyntaxException exception
63 | */
64 | public BatchConsumer(String serverUrl, String appId, int batchSize, int timeout, boolean autoFlush, int interval, String compress) throws URISyntaxException {
65 | super(serverUrl, appId, batchSize, timeout, autoFlush, interval, compress, 0, true);
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/src/main/java/cn/thinkingdata/analytics/DebugConsumer.java:
--------------------------------------------------------------------------------
1 | package cn.thinkingdata.analytics;
2 |
3 | import java.net.URISyntaxException;
4 |
5 | /**
6 | * @deprecated please use TDDebugConsumer instead
7 | */
8 | @Deprecated
9 | public class DebugConsumer extends TDDebugConsumer {
10 | public DebugConsumer(String serverUrl, String appId) throws URISyntaxException {
11 | super(serverUrl, appId, true);
12 | }
13 |
14 | public DebugConsumer(String serverUrl, String appId, String deviceId) throws URISyntaxException {
15 | super(serverUrl, appId, true, deviceId);
16 | }
17 |
18 | public DebugConsumer(String serverUrl, String appId, boolean writeData) throws URISyntaxException {
19 | super(serverUrl, appId, writeData, null);
20 | }
21 |
22 | public DebugConsumer(String serverUrl, String appId, boolean writeData, String deviceId) throws URISyntaxException {
23 | super(serverUrl, appId, writeData, deviceId);
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/src/main/java/cn/thinkingdata/analytics/LoggerConsumer.java:
--------------------------------------------------------------------------------
1 | package cn.thinkingdata.analytics;
2 |
3 | /**
4 | * @deprecated please use TDLoggerConsumer instead
5 | */
6 | @Deprecated
7 | public class LoggerConsumer extends TDLoggerConsumer {
8 |
9 | /**
10 | * init LoggerConsumer with config
11 | *
12 | * @param config LoggerConsumer.Config instance.
13 | */
14 | public LoggerConsumer(final Config config) {
15 | super(config);
16 | }
17 |
18 | /**
19 | * init LoggerConsumer
20 | *
21 | * @param logDirectory directory of file
22 | */
23 | public LoggerConsumer(final String logDirectory) {
24 | this(new Config(logDirectory));
25 | }
26 |
27 | /**
28 | * init LoggerConsumer
29 | *
30 | * @param logDirectory directory of file
31 | * @param fileSize max size of single log file (MByte), default infinite
32 | */
33 | public LoggerConsumer(final String logDirectory, int fileSize) {
34 | this(new Config(logDirectory, fileSize));
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/src/main/java/cn/thinkingdata/analytics/TDAnalytics.java:
--------------------------------------------------------------------------------
1 | package cn.thinkingdata.analytics;
2 |
3 | import cn.thinkingdata.analytics.exception.InvalidArgumentException;
4 | import cn.thinkingdata.analytics.inter.DynamicSuperPropertiesTracker;
5 | import cn.thinkingdata.analytics.inter.ITDAnalytics;
6 | import cn.thinkingdata.analytics.inter.ITDConsumer;
7 | import cn.thinkingdata.analytics.inter.ITDLogger;
8 | import cn.thinkingdata.analytics.util.TDCommonUtil;
9 | import cn.thinkingdata.analytics.util.TDLogger;
10 | import cn.thinkingdata.analytics.util.TDPropertyUtil;
11 | import org.apache.http.util.TextUtils;
12 |
13 | import java.util.Date;
14 | import java.util.HashMap;
15 | import java.util.Map;
16 | import java.util.UUID;
17 | import java.util.concurrent.ConcurrentHashMap;
18 |
19 | import static cn.thinkingdata.analytics.TDConstData.LIB_NAME;
20 | import static cn.thinkingdata.analytics.TDConstData.LIB_VERSION;
21 |
22 | /**
23 | * Entry of SDK
24 | */
25 | public class TDAnalytics implements ITDAnalytics {
26 | /**
27 | * consumer
28 | */
29 | private final ITDConsumer consumer;
30 | /**
31 | * common properties
32 | */
33 | private final Map superProperties;
34 | private final boolean enableUUID;
35 | /**
36 | * Incorrect data will be reported to the TE when it is false; true would be not. default false
37 | */
38 | boolean isStrictModel;
39 | /**
40 | * dynamic common properties
41 | */
42 | private DynamicSuperPropertiesTracker dynamicSuperProperties = null;
43 | /**
44 | * Construct function
45 | *
46 | * @param consumer BatchConsumer or LoggerConsumer or DebugConsumer
47 | */
48 | public TDAnalytics(final ITDConsumer consumer) {
49 | this(consumer,false);
50 | }
51 |
52 | /**
53 | * Construct function
54 | *
55 | * @param consumer BatchConsumer or LoggerConsumer or DebugConsumer
56 | * @param enableUUID Whether to allow uuid creation for events automatically
57 | */
58 | public TDAnalytics(final ITDConsumer consumer, final boolean enableUUID) {
59 | this(consumer, enableUUID, consumer instanceof TDDebugConsumer);
60 | }
61 |
62 | /**
63 | * Construct function
64 | *
65 | * @param consumer BatchConsumer or LoggerConsumer or DebugConsumer
66 | * @param enableUUID Whether to allow uuid creation for events automatically
67 | * @param isStrictModel Whether to enable property verification
68 | */
69 | public TDAnalytics(final ITDConsumer consumer, final boolean enableUUID, final boolean isStrictModel) {
70 | this.consumer = consumer;
71 | this.enableUUID = enableUUID;
72 | this.superProperties = new ConcurrentHashMap<>();
73 | this.isStrictModel = isStrictModel;
74 | }
75 |
76 | /**
77 | * Is enabled log or not. it is enabled in DebugConsumer
78 | *
79 | * @param isPrintLog enabled
80 | */
81 | public static void enableLog(boolean isPrintLog)
82 | {
83 | TDLogger.enableLog(isPrintLog);
84 | }
85 |
86 | /**
87 | * Developers can set up a custom logging system based on requirements
88 | *
89 | * @param logger custom logger
90 | */
91 | public static void setLogger(ITDLogger logger)
92 | {
93 | TDLogger.setLogger(logger);
94 | }
95 |
96 | /**
97 | * Report normal event
98 | *
99 | * @param accountId account ID
100 | * @param distinctId distinct ID
101 | * @param eventName name
102 | * @param properties properties
103 | * @throws InvalidArgumentException exception
104 | */
105 | @Override
106 | public void track(String accountId, String distinctId, String eventName, Map properties) throws InvalidArgumentException {
107 | innerTrack(accountId, distinctId, TDConstData.DataType.TRACK, eventName, null, properties);
108 | }
109 |
110 | /**
111 | * Report first event
112 | *
113 | * @param accountId account ID
114 | * @param distinctId distinct ID
115 | * @param eventName event name
116 | * @param properties properties (must add "#first_check_id" in properties, because it is flag of the first event)
117 | * @throws InvalidArgumentException exception
118 | */
119 | @Override
120 | public void trackFirst(String accountId, String distinctId, String eventName, String firstCheckId, Map properties) throws InvalidArgumentException {
121 | if (firstCheckId != null) {
122 | properties.put("#first_check_id", firstCheckId);
123 | }
124 | if (properties.containsKey("#first_check_id")) {
125 | innerTrack(accountId, distinctId, TDConstData.DataType.TRACK, eventName, null, properties);
126 | } else {
127 | throw new InvalidArgumentException("#first_check_id key must set");
128 | }
129 | }
130 |
131 | /**
132 | * Report update event
133 | *
134 | * @param accountId account ID
135 | * @param distinctId distinct ID
136 | * @param eventName event name
137 | * @param eventId event id
138 | * @param properties properties
139 | * @throws InvalidArgumentException exception
140 | */
141 | @Override
142 | public void trackUpdate(String accountId, String distinctId, String eventName, String eventId, Map properties) throws InvalidArgumentException {
143 | innerTrack(accountId, distinctId, TDConstData.DataType.TRACK_UPDATE, eventName, eventId, properties);
144 | }
145 |
146 | /**
147 | * Report overwrite event
148 | *
149 | * @param accountId account ID
150 | * @param distinctId distinct ID
151 | * @param eventName event name
152 | * @param eventId event id
153 | * @param properties properties
154 | * @throws InvalidArgumentException exception
155 | */
156 | @Override
157 | public void trackOverwrite(String accountId, String distinctId, String eventName, String eventId, Map properties) throws InvalidArgumentException {
158 | innerTrack(accountId, distinctId, TDConstData.DataType.TRACK_OVERWRITE, eventName, eventId, properties);
159 | }
160 |
161 | /**
162 | * Sets the properties that each event carries
163 | *
164 | * @param properties common properties
165 | */
166 | @Override
167 | public void setSuperProperties(Map properties) {
168 | this.superProperties.putAll(properties);
169 | }
170 |
171 | @Override
172 | public Map getSuperProperties() {
173 | return this.superProperties;
174 | }
175 |
176 | @Override
177 | public void unsetSuperProperties(String key) {
178 | if (!key.isEmpty()) {
179 | this.superProperties.remove(key);
180 | }
181 | }
182 |
183 | @Override
184 | public void clearSuperProperties() {
185 | this.superProperties.clear();
186 | }
187 |
188 | @Override
189 | public void setDynamicSuperPropertiesTracker(DynamicSuperPropertiesTracker dynamicSuperPropertiesTracker) {
190 | this.dynamicSuperProperties = dynamicSuperPropertiesTracker;
191 | }
192 |
193 | /**
194 | * Delete a user, This operation is not reversible
195 | *
196 | * @param accountId account ID
197 | * @param distinctId distinct ID
198 | * @throws InvalidArgumentException exception
199 | */
200 | @Override
201 | public void userDelete(String accountId, String distinctId) throws InvalidArgumentException {
202 | innerUserProfile(accountId, distinctId, TDConstData.DataType.USER_DEL, null);
203 | }
204 |
205 | /**
206 | * To accumulate operations against the property
207 | *
208 | * @param accountId account ID
209 | * @param distinctId distinct ID
210 | * @param properties properties
211 | * @throws InvalidArgumentException exception
212 | */
213 | @Override
214 | public void userAdd(String accountId, String distinctId, Map properties) throws InvalidArgumentException {
215 | innerUserProfile(accountId, distinctId, TDConstData.DataType.USER_ADD, properties);
216 | }
217 |
218 | /**
219 | * Set user properties, this message would be neglected If such property had been set before
220 | *
221 | * @param accountId account ID
222 | * @param distinctId distinct ID
223 | * @param properties properties
224 | * @throws InvalidArgumentException exception
225 | */
226 | @Override
227 | public void userSetOnce(String accountId, String distinctId, Map properties) throws InvalidArgumentException {
228 | innerUserProfile(accountId, distinctId, TDConstData.DataType.USER_SET_ONCE, properties);
229 | }
230 |
231 | /**
232 | * Set user properties. would overwrite existing names
233 | *
234 | * @param accountId account ID
235 | * @param distinctId distinct ID
236 | * @param properties properties
237 | * @throws InvalidArgumentException exception
238 | */
239 | @Override
240 | public void userSet(String accountId, String distinctId, Map properties) throws InvalidArgumentException {
241 | innerUserProfile(accountId, distinctId, TDConstData.DataType.USER_SET, properties);
242 | }
243 |
244 | /**
245 | * Clear the user properties of users
246 | *
247 | * @param accountId account ID
248 | * @param distinctId distinct ID
249 | * @param properties properties
250 | * @throws InvalidArgumentException exception
251 | */
252 | @Override
253 | public void userUnset(String accountId, String distinctId, String... properties) throws InvalidArgumentException {
254 | if (properties == null) {
255 | return;
256 | }
257 | Map prop = new HashMap<>();
258 | for (String s : properties) {
259 | prop.put(s, 0);
260 | }
261 | innerUserProfile(accountId, distinctId, TDConstData.DataType.USER_UNSET, prop);
262 | }
263 |
264 | /**
265 | * To add user properties of array type
266 | *
267 | * @param accountId account ID
268 | * @param distinctId distinct ID
269 | * @param properties properties
270 | * @throws InvalidArgumentException exception
271 | */
272 | @Override
273 | public void userAppend(String accountId, String distinctId, Map properties) throws InvalidArgumentException {
274 | innerUserProfile(accountId, distinctId, TDConstData.DataType.USER_APPEND, properties);
275 | }
276 |
277 | /**
278 | * Append user properties to array type by unique.
279 | *
280 | * @param accountId account ID
281 | * @param distinctId distinct ID
282 | * @param properties properties
283 | * @throws InvalidArgumentException exception
284 | */
285 | @Override
286 | public void userUniqAppend(String accountId, String distinctId, Map properties) throws InvalidArgumentException {
287 | innerUserProfile(accountId, distinctId, TDConstData.DataType.USER_UNIQ_APPEND, properties);
288 | }
289 |
290 | @Override
291 | public void flush() {
292 | this.consumer.flush();
293 | TDLogger.println("Manually flush");
294 | }
295 |
296 | @Override
297 | public void close() {
298 | this.consumer.close();
299 | TDLogger.println("SDK close");
300 | }
301 |
302 | private void innerTrack(String accountId, String distinctId, TDConstData.DataType type, String eventName, String eventId, Map properties)
303 | throws InvalidArgumentException {
304 | add(distinctId, accountId, type, eventName, eventId, properties);
305 | }
306 |
307 | private void innerUserProfile(String accountId, String distinctId, TDConstData.DataType type, Map properties)
308 | throws InvalidArgumentException {
309 | add(distinctId, accountId, type, null, null, properties);
310 | }
311 |
312 | private void add(String distinctId, String accountId, TDConstData.DataType type, String eventName, String eventId, Map properties)
313 | throws InvalidArgumentException {
314 | if (isStrictModel && TextUtils.isEmpty(accountId) && TextUtils.isEmpty(distinctId)) {
315 | throw new InvalidArgumentException("accountId or distinctId must be provided.");
316 | }
317 | Map copyProperties = properties!=null ? new ConcurrentHashMap<>(properties) : new ConcurrentHashMap();
318 | Map desProperties = new HashMap<>();
319 | Map event = new HashMap<>();
320 | TDCommonUtil.buildData(event,"#distinct_id", distinctId);
321 | TDCommonUtil.buildData(event,"#account_id", accountId);
322 | event.put("#time", new Date());
323 | event.put("#type", type.getType());
324 | // #uuid v4 is only supported
325 | if(!event.containsKey("#uuid") && enableUUID)
326 | {
327 | event.put("#uuid", UUID.randomUUID().toString());
328 | }
329 | // Move special properties
330 | TDPropertyUtil.moveProperty(event,copyProperties,"#uuid","#time","#ip","#app_id","#first_check_id","#transaction_property","#import_tool_id");
331 | if (type.getType().contains("track")) {
332 | if (isStrictModel) {
333 | TDCommonUtil.throwEmptyException(eventName,"The event name must be provided.");
334 | }
335 | if (type.getType().contains("track_")) {
336 | if (isStrictModel) {
337 | TDCommonUtil.throwEmptyException(eventId,"The event id must be provided.");
338 | }
339 | TDCommonUtil.buildData(event,"#event_id", eventId);
340 | }
341 | TDPropertyUtil.mergeProperties(desProperties,superProperties,dynamicSuperProperties!=null ? dynamicSuperProperties.getDynamicSuperProperties():null,copyProperties);
342 | event.put("#event_name", eventName);
343 | desProperties.put("#lib", LIB_NAME);
344 | desProperties.put("#lib_version", LIB_VERSION);
345 | } else {
346 | TDPropertyUtil.mergeProperties(desProperties,copyProperties);
347 | }
348 | if (isStrictModel) {
349 | TDPropertyUtil.assertProperties(desProperties,type);
350 | }
351 | event.put("properties",desProperties);
352 | try {
353 | this.consumer.add(event);
354 | } catch (Exception e) {
355 | throw new InvalidArgumentException(e);
356 | }
357 | }
358 | }
359 |
--------------------------------------------------------------------------------
/src/main/java/cn/thinkingdata/analytics/TDBatchConsumer.java:
--------------------------------------------------------------------------------
1 | package cn.thinkingdata.analytics;
2 |
3 | import cn.thinkingdata.analytics.exception.IllegalDataException;
4 | import cn.thinkingdata.analytics.exception.NeedRetryException;
5 | import cn.thinkingdata.analytics.inter.ITDConsumer;
6 | import cn.thinkingdata.analytics.request.TDBatchRequest;
7 | import cn.thinkingdata.analytics.util.TDCommonUtil;
8 | import cn.thinkingdata.analytics.util.TDLogger;
9 | import com.alibaba.fastjson2.JSON;
10 |
11 | import java.net.URI;
12 | import java.net.URISyntaxException;
13 | import java.util.*;
14 |
15 | import static cn.thinkingdata.analytics.TDConstData.DEFAULT_DATE_FORMAT;
16 |
17 | /**
18 | * Data is reported using http
19 | */
20 | public class TDBatchConsumer implements ITDConsumer {
21 |
22 | private final int batchSize; // flush event count each time
23 | private final int maxCacheSize;
24 | private final boolean isThrowException;
25 | private Timer autoFlushTimer;
26 |
27 | private final static int MAX_BATCH_SIZE = 1000; // max limit of flush event count
28 | private final Object messageLock = new Object();
29 | private final Object cacheLock = new Object();
30 | private final Object sdkCloseLock = new Object();
31 | private List