|
| 1 | +/* |
| 2 | + * Copyright (c) 2010-2020 Tencent Cloud. All rights reserved. |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | + * of this software and associated documentation files (the "Software"), to deal |
| 6 | + * in the Software without restriction, including without limitation the rights |
| 7 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | + * copies of the Software, and to permit persons to whom the Software is |
| 9 | + * furnished to do so, subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included in all |
| 12 | + * copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 | + * SOFTWARE. |
| 21 | + */ |
| 22 | + |
| 23 | +package com.tencent.qcloud.track; |
| 24 | + |
| 25 | +import android.content.Context; |
| 26 | +import android.util.Log; |
| 27 | + |
| 28 | +import com.tencentcloudapi.cls.android.CLSAdapter; |
| 29 | +import com.tencentcloudapi.cls.android.CLSConfig; |
| 30 | +import com.tencentcloudapi.cls.android.producer.AsyncProducerClient; |
| 31 | +import com.tencentcloudapi.cls.android.producer.AsyncProducerConfig; |
| 32 | +import com.tencentcloudapi.cls.android.producer.common.LogItem; |
| 33 | +import com.tencentcloudapi.cls.android.producer.errors.ProducerException; |
| 34 | +import com.tencentcloudapi.cls.android.producer.util.NetworkUtils; |
| 35 | +import com.tencentcloudapi.cls.plugin.network_diagnosis.CLSNetDiagnosisPlugin; |
| 36 | + |
| 37 | +import java.util.ArrayList; |
| 38 | +import java.util.List; |
| 39 | +import java.util.Map; |
| 40 | + |
| 41 | +/** |
| 42 | + * 数据追踪服务 |
| 43 | + */ |
| 44 | +public class QCloudTrackService { |
| 45 | + private static final String TAG = "TrackService"; |
| 46 | + |
| 47 | + private static final String NET_DIAGNOSIS_TOPIC_ID = "net_diagnosis"; |
| 48 | + |
| 49 | + private static boolean debug = false; |
| 50 | + private static boolean isCloseTrack = false; |
| 51 | + private static AsyncProducerClient clsClient; |
| 52 | + |
| 53 | + /** |
| 54 | + * 主题前缀 |
| 55 | + */ |
| 56 | + private static String topicIdPrefix = "qcloud_track"; |
| 57 | + |
| 58 | + /** |
| 59 | + * 初始化 |
| 60 | + * @param context context |
| 61 | + * @param endpoint 接入点 |
| 62 | + * @param secretId API密钥 secretId |
| 63 | + * @param secretKey API密钥 secretKey |
| 64 | + * @param secretToken API密钥 secretToken |
| 65 | + * @param debug 是否是调试模式 会输出日志 |
| 66 | + * @param isCloseTrack 是否关闭数据上报 |
| 67 | + */ |
| 68 | + public static void init(Context context, |
| 69 | + String topicIdPrefix, |
| 70 | + String endpoint, String secretId, String secretKey, String secretToken, |
| 71 | + boolean debug, boolean isCloseTrack) { |
| 72 | + QCloudTrackService.topicIdPrefix = topicIdPrefix; |
| 73 | + QCloudTrackService.debug = debug; |
| 74 | + QCloudTrackService.isCloseTrack = isCloseTrack; |
| 75 | + // NetworkUtils.getLocalMachineIP() 获取本地网卡ip,如果不指定,默认填充服务端接收到的网络出口ip |
| 76 | + AsyncProducerConfig config = new AsyncProducerConfig(context.getApplicationContext(), endpoint, secretId, secretKey, secretToken, NetworkUtils.getLocalMachineIP()); |
| 77 | + // 构建一个客户端实例 |
| 78 | + QCloudTrackService.clsClient = new AsyncProducerClient(config); |
| 79 | + |
| 80 | + // 添加网络探测插件 |
| 81 | + CLSAdapter adapter = CLSAdapter.getInstance(); |
| 82 | + adapter.addPlugin(new CLSNetDiagnosisPlugin()); |
| 83 | + CLSConfig clsConfig = new CLSConfig(context.getApplicationContext()); |
| 84 | + clsConfig.endpoint = endpoint; |
| 85 | + clsConfig.accessKeyId = secretId; |
| 86 | + clsConfig.accessKeySecret = secretKey; |
| 87 | + clsConfig.securityToken = secretToken; |
| 88 | + clsConfig.topicId = topicIdPrefix + "_" + NET_DIAGNOSIS_TOPIC_ID; |
| 89 | + // 发布时,建议关闭,即配置为config.debuggable = false。 |
| 90 | + clsConfig.debuggable = debug; |
| 91 | + adapter.init(clsConfig); |
| 92 | + } |
| 93 | + |
| 94 | + public static void track(String eventCode, Map<String, String> params) { |
| 95 | + if (QCloudTrackService.isCloseTrack || !isIncludeTrackService()) return; |
| 96 | + |
| 97 | + if (clsClient == null) { |
| 98 | + throw new IllegalArgumentException("Please call the init method of TrackService first"); |
| 99 | + } |
| 100 | + |
| 101 | + for (int i = 0; i < 10000; ++i) { |
| 102 | + List<LogItem> logItems = new ArrayList<>(); |
| 103 | + LogItem logItem = new LogItem(); |
| 104 | + for (String key : params.keySet()){ |
| 105 | + logItem.PushBack(key, params.get(key)); |
| 106 | + } |
| 107 | + logItems.add(logItem); |
| 108 | + try { |
| 109 | + clsClient.putLogs(QCloudTrackService.topicIdPrefix + "_" + eventCode, logItems, |
| 110 | + result -> { |
| 111 | + if (debug) { |
| 112 | + StringBuilder mapAsString = new StringBuilder("{"); |
| 113 | + for (String key : params.keySet()) { |
| 114 | + mapAsString.append(key).append("=").append(params.get(key)).append(", "); |
| 115 | + } |
| 116 | + mapAsString.delete(mapAsString.length() - 2, mapAsString.length()).append("}"); |
| 117 | + Log.i(TAG, String.format("eventCode: %s, params: %s => result: %s", QCloudTrackService.topicIdPrefix + "_" + eventCode, mapAsString, result.toString())); |
| 118 | + } |
| 119 | + } |
| 120 | + ); |
| 121 | + } catch (InterruptedException | ProducerException e) { |
| 122 | + e.printStackTrace(); |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + // TODO: 2023/6/21 添加适合sdk用的网络探测方法 |
| 128 | + |
| 129 | + /** |
| 130 | + * 设置是否关闭数据上报 |
| 131 | + * @param isCloseTrack 是否关闭数据上报 |
| 132 | + */ |
| 133 | + public static void setIsCloseTrack(boolean isCloseTrack) { |
| 134 | + QCloudTrackService.isCloseTrack = isCloseTrack; |
| 135 | + } |
| 136 | + |
| 137 | + private static boolean isIncludeTrackService() { |
| 138 | + try { |
| 139 | + Class.forName("com.tencent.qcloud.track.QCloudTrackService"); |
| 140 | + return true; |
| 141 | + } catch (ClassNotFoundException e) { |
| 142 | + return false; |
| 143 | + } |
| 144 | + } |
| 145 | +} |
0 commit comments