Skip to content

Commit

Permalink
feat: 支持 cls 上报 (#102)
Browse files Browse the repository at this point in the history
* feat: 支持 cls 上报
  • Loading branch information
livehigh authored Apr 16, 2024
1 parent 7ca0e88 commit 90a9c60
Show file tree
Hide file tree
Showing 13 changed files with 1,071 additions and 554 deletions.
569 changes: 368 additions & 201 deletions demo-album/lib/cos-wx-sdk-v5.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion demo-album/lib/cos-wx-sdk-v5.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion demo/ciDemo/asr.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ function getAsrQueue() {
}

function putAsrQueue() {
// 任务所在的队列 ID,请使用查询队列(https://cloud.tencent.com/document/product/460/46946)获取或前往万象控制台(https://cloud.tencent.com/document/product/460/46487)在存储桶中查询
// 任务所在的队列 ID,请使用查询队列(https://cloud.tencent.com/document/product/460/46234)获取或前往万象控制台(https://cloud.tencent.com/document/product/460/46487)在存储桶中查询
var queueId = 'pcc77499e85c311edb9865254008618d9';
var host = config.Bucket + '.ci.' + config.Region + '.myqcloud.com/asrqueue/' + queueId;
var url = 'https://' + host;
Expand Down
25 changes: 25 additions & 0 deletions demo/lib/cls.min.js

Large diffs are not rendered by default.

569 changes: 368 additions & 201 deletions demo/lib/cos-wx-sdk-v5.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion demo/lib/cos-wx-sdk-v5.min.js

Large diffs are not rendered by default.

11 changes: 9 additions & 2 deletions demo/tools.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
var COS = require('./lib/cos-wx-sdk-v5');
var config = require('./config');
const Beacon = require('./lib/beacon_mp.min');
const ClsClient = require('./lib/cls.min');
const clsClient = new ClsClient({
topicId: 'xxxxxx-xxxx-xxxx-xxxx-xxxxxx', // 日志主题 id
region: 'ap-guangzhou', // 日志主题所在地域,比如 ap-guangzhou,需在小程序平台设置域名白名单:https://ap-guangzhou.cls.tencentcs.com
maxRetainDuration: 30, // 默认 30s
maxRetainSize: 20, // 默认20条
});

// 签名回调
var getAuthorization = function (options, callback) {
Expand Down Expand Up @@ -87,11 +94,11 @@ var getAuthorization = function (options, callback) {
};

var cos = new COS({
// EnableTracker: true, // 开启灯塔上报
// Beacon,
getAuthorization: getAuthorization,
// 是否使用全球加速域名。开启该配置后仅以下接口支持操作:putObject、getObject、headObject、optionsObject、multipartInit、multipartListPart、multipartUpload、multipartAbort、multipartComplete、multipartList、sliceUploadFile、uploadFiles
// UseAccelerate: true,
// BeaconReporter: Beacon, // 开启灯塔上报
// ClsReporter: clsClient, // 开启 cls 上报
});

// 回调统一处理函数
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cos-wx-sdk-v5",
"version": "1.6.1",
"version": "1.6.2",
"description": "小程序 SDK for [腾讯云对象存储服务](https://cloud.tencent.com/product/cos)",
"main": "demo/lib/cos-wx-sdk-v5.min.js",
"scripts": {
Expand Down
32 changes: 25 additions & 7 deletions src/advance.js
Original file line number Diff line number Diff line change
Expand Up @@ -943,15 +943,18 @@ async function uploadFile(params, callback) {
var fileInfo = { TaskId: '' };

// 上传链路
if (self.options.EnableTracker) {
if (self.options.EnableReporter) {
const accelerate =
self.options.UseAccelerate ||
(typeof self.options.Domain === 'string' && self.options.Domain.includes('accelerate.'));
const realApi = FileSize > SliceSize ? 'sliceUploadFile' : 'putObject';
params.tracker = new Tracker({
Beacon: self.options.Beacon,
Beacon: self.options.BeaconReporter,
clsReporter: self.options.ClsReporter,
bucket: params.Bucket,
region: params.Region,
apiName: 'uploadFile',
realApi,
fileKey: params.Key,
fileSize: FileSize,
accelerate,
Expand Down Expand Up @@ -979,7 +982,7 @@ async function uploadFile(params, callback) {
var _onFileFinish = params.onFileFinish;
var onFileFinish = function (err, data) {
// 格式化上报参数并上报
params.tracker && params.tracker.formatResult(err, data);
params.tracker && params.tracker.report(err, data);
_onFileFinish && _onFileFinish(err, data, fileInfo);
callback && callback(err, data);
};
Expand Down Expand Up @@ -1043,15 +1046,18 @@ async function uploadFiles(params, callback) {
TotalSize += FileSize;

// 单个文件上传链路
if (self.options.EnableTracker) {
if (self.options.EnableReporter) {
const accelerate =
self.options.UseAccelerate ||
(typeof self.options.Domain === 'string' && self.options.Domain.includes('accelerate.'));
const realApi = FileSize > SliceSize ? 'sliceUploadFile' : 'putObject';
fileParams.tracker = new Tracker({
Beacon: self.options.Beacon,
Beacon: self.options.BeaconReporter,
clsReporter: self.options.ClsReporter,
bucket: fileParams.Bucket,
region: fileParams.Region,
apiName: 'uploadFiles',
realApi,
fileKey: fileParams.Key,
fileSize: FileSize,
accelerate,
Expand Down Expand Up @@ -1089,7 +1095,7 @@ async function uploadFiles(params, callback) {
var _onFileFinish = fileParams.onFileFinish;
var onFileFinish = function (err, data) {
// 格式化上报参数并上报
fileParams.tracker && fileParams.tracker.formatResult(err, data);
fileParams.tracker && fileParams.tracker.report(err, data);
_onFileFinish && _onFileFinish(err, data);
onTotalFileFinish && onTotalFileFinish(err, data, fileInfo);
};
Expand Down Expand Up @@ -1166,6 +1172,7 @@ function sliceCopyFile(params, callback) {
Key: Key,
UploadId: UploadData.UploadId,
Parts: Parts,
tracker: params.tracker,
calledBySdk: 'sliceCopyFile',
},
tryCallback
Expand Down Expand Up @@ -1217,6 +1224,8 @@ function sliceCopyFile(params, callback) {
UploadId: UploadData.UploadId,
PartNumber: PartNumber,
CopySourceRange: CopySourceRange,
tracker: params.tracker,
calledBySdk: 'sliceCopyFile',
onProgress: function (data) {
FinishSize += data.loaded - preAddSize;
preAddSize = data.loaded;
Expand Down Expand Up @@ -1257,6 +1266,8 @@ function sliceCopyFile(params, callback) {
Region: Region,
Key: Key,
Headers: TargetHeader,
tracker: params.tracker,
calledBySdk: 'sliceCopyFile',
},
function (err, data) {
if (err) return callback(err);
Expand Down Expand Up @@ -1285,6 +1296,8 @@ function sliceCopyFile(params, callback) {
Region: Region,
Key: Key,
UploadId: UploadId,
tracker: params.tracker,
calledBySdk: 'sliceCopyFile',
},
function (err, PartListData) {
if (err) {
Expand Down Expand Up @@ -1383,6 +1396,8 @@ function sliceCopyFile(params, callback) {
Bucket: SourceBucket,
Region: SourceRegion,
Key: SourceKey,
tracker: params.tracker,
calledBySdk: 'sliceCopyFile',
},
function (err, data) {
if (err) {
Expand All @@ -1400,14 +1415,15 @@ function sliceCopyFile(params, callback) {
return;
}

params.tracker && params.tracker.setParams({ httpSize: FileSize });
onProgress = util.throttleOnProgress.call(self, FileSize, params.onProgress);

// 开始上传
if (FileSize <= CopySliceSize) {
if (!params.Headers['x-cos-metadata-directive']) {
params.Headers['x-cos-metadata-directive'] = 'Copy';
}
self.putObjectCopy(params, function (err, data) {
self.putObjectCopy(Object.assign(params, { calledBySdk: 'sliceCopyFile' }), function (err, data) {
if (err) {
onProgress(null, true);
return callback(err);
Expand Down Expand Up @@ -1466,6 +1482,8 @@ function copySliceItem(params, callback) {
PartNumber: PartNumber,
CopySourceRange: CopySourceRange,
onProgress: params.onProgress,
tracker: params.tracker,
calledBySdk: params.calledBySdk,
},
function (err, data) {
tryCallback(err || null, data);
Expand Down
Loading

0 comments on commit 90a9c60

Please sign in to comment.