|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.eventmesh.openconnect.api.monitor; |
| 19 | + |
| 20 | +import java.util.concurrent.atomic.AtomicLong; |
| 21 | +import java.util.concurrent.atomic.LongAdder; |
| 22 | + |
| 23 | +import lombok.Getter; |
| 24 | +import lombok.extern.slf4j.Slf4j; |
| 25 | + |
| 26 | +@Slf4j |
| 27 | +@Getter |
| 28 | +public abstract class AbstractConnectorMonitor implements Monitor { |
| 29 | + |
| 30 | + private final String taskId; |
| 31 | + private final String jobId; |
| 32 | + private final String ip; |
| 33 | + private final LongAdder totalRecordNum; |
| 34 | + private final LongAdder totalTimeCost; |
| 35 | + protected final AtomicLong startTime; |
| 36 | + private final AtomicLong maxTimeCost; |
| 37 | + private long averageTime = 0; |
| 38 | + private double tps = 0; |
| 39 | + |
| 40 | + public AbstractConnectorMonitor(String taskId, String jobId, String ip) { |
| 41 | + this.taskId = taskId; |
| 42 | + this.jobId = jobId; |
| 43 | + this.ip = ip; |
| 44 | + this.totalRecordNum = new LongAdder(); |
| 45 | + this.totalTimeCost = new LongAdder(); |
| 46 | + this.startTime = new AtomicLong(System.currentTimeMillis()); |
| 47 | + this.maxTimeCost = new AtomicLong(); |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public synchronized void recordProcess(long timeCost) { |
| 52 | + totalRecordNum.increment(); |
| 53 | + totalTimeCost.add(timeCost); |
| 54 | + maxTimeCost.updateAndGet(max -> Math.max(max, timeCost)); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public synchronized void recordProcess(int recordCount, long timeCost) { |
| 59 | + totalRecordNum.add(recordCount); |
| 60 | + totalTimeCost.add(timeCost); |
| 61 | + maxTimeCost.updateAndGet(max -> Math.max(max, timeCost)); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public synchronized void printMetrics() { |
| 66 | + long totalRecords = totalRecordNum.sum(); |
| 67 | + long totalCost = totalTimeCost.sum(); |
| 68 | + averageTime = totalRecords > 0 ? totalCost / totalRecords : 0; |
| 69 | + long elapsedTime = (System.currentTimeMillis() - startTime.get()) / 1000; // in seconds |
| 70 | + tps = elapsedTime > 0 ? (double) totalRecords / elapsedTime : 0; |
| 71 | + |
| 72 | + log.info("========== Metrics =========="); |
| 73 | + log.info("TaskId: {}|JobId: {}|ip: {}", taskId, jobId, ip); |
| 74 | + log.info("Total records: {}", totalRecordNum); |
| 75 | + log.info("Total time (ms): {}", totalTimeCost); |
| 76 | + log.info("Max time per record (ms): {}", maxTimeCost); |
| 77 | + log.info("Average time per record (ms): {}", averageTime); |
| 78 | + log.info("TPS: {}", tps); |
| 79 | + } |
| 80 | +} |
0 commit comments