Skip to content

Commit

Permalink
perf: 增加业务维度的日志量占用统计指标 #3428
Browse files Browse the repository at this point in the history
  • Loading branch information
jsonwan committed Feb 17, 2025
1 parent dba49cd commit ffebf8d
Show file tree
Hide file tree
Showing 12 changed files with 273 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ private void writeLogs(Map<ExecuteObjectCompositeKey, ServiceExecuteObjectLogDTO
if (log.isDebugEnabled()) {
log.debug("Write file task initial logs, executionLogs: {}", executionLogs);
}
logService.writeFileLogs(taskInstance.getCreateTime(), new ArrayList<>(executionLogs.values()));
logService.writeFileLogs(taskInstance, new ArrayList<>(executionLogs.values()));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ protected final void handleStartGseTaskError(GseTaskResponse gseTaskResponse) {
executeObjectTask.setTotalTime(TaskCostCalculator.calculate(gseTask.getStartTime(), now, null));
executeObjectTask.setStatus(ExecuteObjectTaskStatusEnum.SUBMIT_FAILED);
}
logService.batchWriteScriptLog(taskInstance.getCreateTime(), stepInstanceId, executeCount, batch, scriptLogs);
logService.batchWriteScriptLog(taskInstance, stepInstanceId, executeCount, batch, scriptLogs);
scriptExecuteObjectTaskService.batchUpdateTasks(targetExecuteObjectTaskMap.values());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@
import com.tencent.bk.job.execute.model.FileSourceTaskLogDTO;
import com.tencent.bk.job.execute.model.StepInstanceBaseDTO;
import com.tencent.bk.job.execute.model.StepInstanceDTO;
import com.tencent.bk.job.execute.model.TaskInstanceDTO;
import com.tencent.bk.job.execute.service.AccountService;
import com.tencent.bk.job.execute.service.FileSourceTaskLogService;
import com.tencent.bk.job.execute.service.LogService;
import com.tencent.bk.job.execute.service.StepInstanceService;
import com.tencent.bk.job.execute.service.TaskInstanceService;
import com.tencent.bk.job.file_gateway.api.inner.ServiceFileSourceTaskResource;
import com.tencent.bk.job.file_gateway.consts.TaskStatusEnum;
import com.tencent.bk.job.file_gateway.model.req.inner.ClearTaskFilesReq;
Expand Down Expand Up @@ -73,6 +75,7 @@
public class ThirdFilePrepareService {
private final ResultHandleManager resultHandleManager;
private final ServiceFileSourceTaskResource fileSourceTaskResource;
private final TaskInstanceService taskInstanceService;
private final StepInstanceService stepInstanceService;
private final FileSourceTaskLogService fileSourceTaskLogService;
private final AccountService accountService;
Expand All @@ -85,6 +88,7 @@ public class ThirdFilePrepareService {
@Autowired
public ThirdFilePrepareService(ResultHandleManager resultHandleManager,
ServiceFileSourceTaskResource fileSourceTaskResource,
TaskInstanceService taskInstanceService,
StepInstanceService stepInstanceService,
FileSourceTaskLogService fileSourceTaskLogService,
AccountService accountService,
Expand All @@ -93,6 +97,7 @@ public ThirdFilePrepareService(ResultHandleManager resultHandleManager,
TaskExecuteMQEventDispatcher taskExecuteMQEventDispatcher) {
this.resultHandleManager = resultHandleManager;
this.fileSourceTaskResource = fileSourceTaskResource;
this.taskInstanceService = taskInstanceService;
this.stepInstanceService = stepInstanceService;
this.fileSourceTaskLogService = fileSourceTaskLogService;
this.accountService = accountService;
Expand Down Expand Up @@ -388,13 +393,15 @@ private ThirdFilePrepareTask asyncWatchThirdFilePulling(
boolean isForRetry,
ThirdFilePrepareTaskResultHandler resultHandler
) {
ThirdFilePrepareTask batchResultHandleTask =
new ThirdFilePrepareTask(
stepInstance,
fileSourceList,
batchTaskId,
isForRetry,
new RecordableThirdFilePrepareTaskResultHandler(stepInstance, resultHandler));
TaskInstanceDTO taskInstance = taskInstanceService.getTaskInstance(stepInstance.getTaskInstanceId());
ThirdFilePrepareTask batchResultHandleTask = new ThirdFilePrepareTask(
taskInstance,
stepInstance,
fileSourceList,
batchTaskId,
isForRetry,
new RecordableThirdFilePrepareTaskResultHandler(stepInstance, resultHandler)
);
batchResultHandleTask.initDependentService(
fileSourceTaskResource, stepInstanceService, accountService,
fileWorkerHostService, logService, taskExecuteMQEventDispatcher, fileSourceTaskLogService
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import com.tencent.bk.job.execute.model.FileSourceDTO;
import com.tencent.bk.job.execute.model.FileSourceTaskLogDTO;
import com.tencent.bk.job.execute.model.StepInstanceDTO;
import com.tencent.bk.job.execute.model.TaskInstanceDTO;
import com.tencent.bk.job.execute.service.AccountService;
import com.tencent.bk.job.execute.service.FileSourceTaskLogService;
import com.tencent.bk.job.execute.service.LogService;
Expand Down Expand Up @@ -81,6 +82,7 @@
@Slf4j
public class ThirdFilePrepareTask implements ContinuousScheduledTask, JobTaskContext {

private final TaskInstanceDTO taskInstance;
private final StepInstanceDTO stepInstance;
private final List<FileSourceDTO> fileSourceList;
private final String batchTaskId;
Expand Down Expand Up @@ -109,11 +111,13 @@ public class ThirdFilePrepareTask implements ContinuousScheduledTask, JobTaskCon
private final TaskContext taskContext;

public ThirdFilePrepareTask(
TaskInstanceDTO taskInstance,
StepInstanceDTO stepInstance,
List<FileSourceDTO> fileSourceList,
String batchTaskId,
boolean isForRetry,
ThirdFilePrepareTaskResultHandler resultHandler) {
this.taskInstance = taskInstance;
this.stepInstance = stepInstance;
this.fileSourceList = fileSourceList;
this.batchTaskId = batchTaskId;
Expand Down Expand Up @@ -447,7 +451,7 @@ private void writeLogs(StepInstanceDTO stepInstance,
serviceExecuteObjectLogDTOList.add(buildServiceHostLogDTO(host, logDTO));
}
logService.writeFileLogsWithTimestamp(
stepInstance.getCreateTime(),
taskInstance,
serviceExecuteObjectLogDTOList,
System.currentTimeMillis()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -950,7 +950,7 @@ private String formatSpeed(int speed) {

private void writeFileTaskLogContent(Map<ExecuteObjectCompositeKey, ServiceExecuteObjectLogDTO> executionLogs) {
if (!executionLogs.isEmpty()) {
logService.writeFileLogs(taskInstance.getCreateTime(), new ArrayList<>(executionLogs.values()));
logService.writeFileLogs(taskInstance, new ArrayList<>(executionLogs.values()));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ private ExecuteObject findExecuteObject(ExecuteObjectGseKey executeObjectGseKey)
}

private void saveScriptLogContent(List<ServiceExecuteObjectScriptLogDTO> logs) {
logService.batchWriteScriptLog(taskInstance.getCreateTime(), stepInstanceId, stepInstance.getExecuteCount(),
logService.batchWriteScriptLog(taskInstance, stepInstanceId, stepInstance.getExecuteCount(),
stepInstance.getBatch(), logs);
}

Expand Down Expand Up @@ -603,7 +603,7 @@ protected void saveFailInfoForUnfinishedExecuteObjectTask(ExecuteObjectTaskStatu
return logService.buildSystemScriptLog(stepInstance, executeObject, errorMsg,
executeObjectTask.getScriptLogOffset(), endTime);
}).collect(Collectors.toList());
logService.batchWriteScriptLog(taskInstance.getCreateTime(), stepInstanceId, stepInstance.getExecuteCount(),
logService.batchWriteScriptLog(taskInstance, stepInstanceId, stepInstance.getExecuteCount(),
stepInstance.getBatch(), scriptLogs);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ public class ExecuteMetricsConstants {
* 任务启动指标
*/
public static final String NAME_JOB_TASK_START = "job.task.start";
/**
* 日志大小(字节)
*/
public static final String NAME_JOB_LOG_BYTES = "job.log.bytes";


// tag
/**
* 任务启动方式
Expand All @@ -51,6 +57,20 @@ public class ExecuteMetricsConstants {
* 任务类型
*/
public static final String TAG_KEY_TASK_TYPE = "task_type";
/**
* Job业务ID
*/
public static final String TAG_KEY_APP_ID = "app_id";
/**
* 定时任务ID
*/
public static final String TAG_KEY_CRON_TASK_ID = "cron_task_id";
/**
* AppCode
*/
public static final String TAG_KEY_APP_CODE = "app_code";


/**
* 任务类型:快速执行脚本
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Tencent is pleased to support the open source community by making BK-JOB蓝鲸智云作业平台 available.
*
* Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
*
* BK-JOB蓝鲸智云作业平台 is licensed under the MIT License.
*
* License for BK-JOB蓝鲸智云作业平台:
* --------------------------------------------------------------------
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
* to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of
* the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
* THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/

package com.tencent.bk.job.execute.metrics;

import com.tencent.bk.job.logsvr.model.service.ServiceBatchSaveLogRequest;

public interface LogSampler {

/**
* 尝试记录日志大小指标,异常不抛出,内部做日志记录
*
* @param request 保存日志请求
*/
void tryToRecordLogSizeMetrics(ServiceBatchSaveLogRequest request);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/*
* Tencent is pleased to support the open source community by making BK-JOB蓝鲸智云作业平台 available.
*
* Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
*
* BK-JOB蓝鲸智云作业平台 is licensed under the MIT License.
*
* License for BK-JOB蓝鲸智云作业平台:
* --------------------------------------------------------------------
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
* to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of
* the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
* THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/

package com.tencent.bk.job.execute.metrics.impl;

import com.tencent.bk.job.execute.metrics.ExecuteMetricsConstants;
import com.tencent.bk.job.execute.metrics.LogSampler;
import com.tencent.bk.job.logsvr.model.service.ServiceBatchSaveLogRequest;
import com.tencent.bk.job.logsvr.model.service.ServiceExecuteObjectLogDTO;
import com.tencent.bk.job.logsvr.model.service.ServiceExecuteObjectScriptLogDTO;
import com.tencent.bk.job.logsvr.model.service.ServiceFileTaskLogDTO;
import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.Tags;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.nio.charset.StandardCharsets;
import java.util.List;

/**
* 记录任务日志大小指标
*/
@Slf4j
@Service
public class LogSamplerImpl implements LogSampler {

private final MeterRegistry meterRegistry;

@Autowired
public LogSamplerImpl(MeterRegistry meterRegistry) {
this.meterRegistry = meterRegistry;
}

@Override
public void tryToRecordLogSizeMetrics(ServiceBatchSaveLogRequest request) {
try {
Iterable<Tag> tags = buildTags(request);
long logBytes = calcLogsTotalBytes(request.getLogs());
if (log.isDebugEnabled()) {
log.debug(
"tryToRecordLogSizeMetrics: appId={}, cronTaskId={}, appCode={}, logBytes={}",
request.getAppId(),
request.getCronTaskId(),
request.getAppCode(),
logBytes
);
}
Counter.builder(ExecuteMetricsConstants.NAME_JOB_LOG_BYTES)
.tags(tags)
.register(meterRegistry)
.increment(logBytes);
} catch (Exception e) {
log.warn("tryToRecordLogSizeMetrics error", e);
}
}

/**
* 构造指标的标签
*
* @param request 保存日志请求
* @return 指标标签列表
*/
private Iterable<Tag> buildTags(ServiceBatchSaveLogRequest request) {
Long appId = request.getAppId();
Long cronTaskId = request.getCronTaskId();
String appCode = request.getAppCode();
return Tags.of(
Tag.of(ExecuteMetricsConstants.TAG_KEY_APP_ID, String.valueOf(appId)),
Tag.of(ExecuteMetricsConstants.TAG_KEY_CRON_TASK_ID, String.valueOf(cronTaskId)),
Tag.of(ExecuteMetricsConstants.TAG_KEY_APP_CODE, appCode)
);
}

/**
* 计算批量日志中所有日志的字节大小总和
*
* @param logs 日志列表
* @return 日志总字节大小
*/
private long calcLogsTotalBytes(List<ServiceExecuteObjectLogDTO> logs) {
if (CollectionUtils.isEmpty(logs)) {
return 0;
}
return logs.stream()
.mapToLong(this::calcLogBytes)
.sum();
}

/**
* 计算单个日志的字节数
*
* @param log 日志内容
* @return 单个日志的字节数
*/
private long calcLogBytes(ServiceExecuteObjectLogDTO log) {
if (log == null) {
return 0L;
}
ServiceExecuteObjectScriptLogDTO scriptLog = log.getScriptLog();
// 脚本类型的日志,直接获取contentSizeBytes
if (scriptLog != null) {
return scriptLog.getContentSizeBytes();
}
// 文件类型的日志,计算content字段的字节数并汇总
List<ServiceFileTaskLogDTO> fileTaskLogs = log.getFileTaskLogs();
if (CollectionUtils.isEmpty(fileTaskLogs)) {
return 0L;
}
long totalBytes = 0L;
for (ServiceFileTaskLogDTO fileTaskLog : fileTaskLogs) {
String content = fileTaskLog.getContent();
if (content != null) {
totalBytes += content.getBytes(StandardCharsets.UTF_8).length;
}
}
return totalBytes;
}
}
Loading

0 comments on commit ffebf8d

Please sign in to comment.