From a8c8e206a3407619651cd61e12f989c28feb8048 Mon Sep 17 00:00:00 2001 From: "youngjin.kim2" Date: Wed, 29 Jun 2022 16:42:19 +0900 Subject: [PATCH] [#8993] Add apdex alarm --- .../pinpoint/batch/alarm/CheckerRegistry.java | 8 ++ .../alarm/checker/ApdexScoreChecker.java | 48 +++++++++ .../collector/ResponseTimeDataCollector.java | 12 +++ .../alarm/checker/ApdexScoreCheckerTest.java | 99 +++++++++++++++++++ .../pinpoint/web/alarm/CheckerCategory.java | 2 + 5 files changed, 169 insertions(+) create mode 100644 batch/src/main/java/com/navercorp/pinpoint/batch/alarm/checker/ApdexScoreChecker.java create mode 100644 batch/src/test/java/com/navercorp/pinpoint/batch/alarm/checker/ApdexScoreCheckerTest.java diff --git a/batch/src/main/java/com/navercorp/pinpoint/batch/alarm/CheckerRegistry.java b/batch/src/main/java/com/navercorp/pinpoint/batch/alarm/CheckerRegistry.java index a110b217253d..a11ae0f92491 100644 --- a/batch/src/main/java/com/navercorp/pinpoint/batch/alarm/CheckerRegistry.java +++ b/batch/src/main/java/com/navercorp/pinpoint/batch/alarm/CheckerRegistry.java @@ -2,6 +2,7 @@ import com.navercorp.pinpoint.batch.alarm.checker.AlarmChecker; +import com.navercorp.pinpoint.batch.alarm.checker.ApdexScoreChecker; import com.navercorp.pinpoint.batch.alarm.checker.DataSourceConnectionUsageRateChecker; import com.navercorp.pinpoint.batch.alarm.checker.DeadlockChecker; import com.navercorp.pinpoint.batch.alarm.checker.ErrorCountChecker; @@ -89,6 +90,13 @@ public AlarmChecker createChecker(DataCollector dataCollector, Rule rule) { } }); + put(CheckerCategory.APDEX_SCORE, new AlarmCheckerFactory() { + @Override + public AlarmChecker createChecker(DataCollector dataCollector, Rule rule) { + return new ApdexScoreChecker((ResponseTimeDataCollector) dataCollector, rule); + } + }); + put(CheckerCategory.SLOW_COUNT_TO_CALLEE, new AlarmCheckerFactory() { @Override public AlarmChecker createChecker(DataCollector dataCollector, Rule rule) { diff --git a/batch/src/main/java/com/navercorp/pinpoint/batch/alarm/checker/ApdexScoreChecker.java b/batch/src/main/java/com/navercorp/pinpoint/batch/alarm/checker/ApdexScoreChecker.java new file mode 100644 index 000000000000..42e2e09d0d78 --- /dev/null +++ b/batch/src/main/java/com/navercorp/pinpoint/batch/alarm/checker/ApdexScoreChecker.java @@ -0,0 +1,48 @@ +/* + * Copyright 2022 NAVER Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.navercorp.pinpoint.batch.alarm.checker; + +import com.navercorp.pinpoint.batch.alarm.collector.ResponseTimeDataCollector; +import com.navercorp.pinpoint.web.alarm.vo.Rule; +import com.navercorp.pinpoint.web.applicationmap.histogram.ApdexScore; + +/** + * @author youngjin.kim2 + */ +public class ApdexScoreChecker extends LongValueAlarmChecker { + + public ApdexScoreChecker(ResponseTimeDataCollector dataCollector, Rule rule) { + super(rule, "%", dataCollector); + } + + @Override + protected Long getDetectedValue() { + final ResponseTimeDataCollector dataCollector = (ResponseTimeDataCollector) this.dataCollector; + + final long satisfiedCount = dataCollector.getFastCount(); + final long toleratingCount = dataCollector.getNormalCount(); + final long totalSamples = dataCollector.getTotalCount(); + final double score = (new ApdexScore(satisfiedCount, toleratingCount, totalSamples)).getApdexScore(); + + return (long) (score * 100.0); + } + + @Override + protected boolean decideResult(Long value) { + return value <= rule.getThreshold(); + } + +} diff --git a/batch/src/main/java/com/navercorp/pinpoint/batch/alarm/collector/ResponseTimeDataCollector.java b/batch/src/main/java/com/navercorp/pinpoint/batch/alarm/collector/ResponseTimeDataCollector.java index 9938f1ca3a2a..f309faf08e22 100644 --- a/batch/src/main/java/com/navercorp/pinpoint/batch/alarm/collector/ResponseTimeDataCollector.java +++ b/batch/src/main/java/com/navercorp/pinpoint/batch/alarm/collector/ResponseTimeDataCollector.java @@ -38,6 +38,8 @@ public class ResponseTimeDataCollector extends DataCollector { private final long slotInterval; private final AtomicBoolean init =new AtomicBoolean(false); // need to consider a race condition when checkers start simultaneously. + private long fastCount = 0; + private long normalCount = 0; private long slowCount = 0; private long errorCount = 0; private long totalCount = 0; @@ -89,6 +91,8 @@ private long calculatePercent(long value) { private void sum(Collection timeHistograms) { for (TimeHistogram timeHistogram : timeHistograms) { + fastCount += timeHistogram.getFastCount(); + normalCount += timeHistogram.getNormalCount(); slowCount += timeHistogram.getSlowCount(); slowCount += timeHistogram.getVerySlowCount(); errorCount += timeHistogram.getTotalErrorCount(); @@ -96,6 +100,14 @@ private void sum(Collection timeHistograms) { } } + public long getFastCount() { + return fastCount; + } + + public long getNormalCount() { + return normalCount; + } + public long getSlowCount() { return slowCount; } diff --git a/batch/src/test/java/com/navercorp/pinpoint/batch/alarm/checker/ApdexScoreCheckerTest.java b/batch/src/test/java/com/navercorp/pinpoint/batch/alarm/checker/ApdexScoreCheckerTest.java new file mode 100644 index 000000000000..a92f58b9ad8c --- /dev/null +++ b/batch/src/test/java/com/navercorp/pinpoint/batch/alarm/checker/ApdexScoreCheckerTest.java @@ -0,0 +1,99 @@ +/* + * Copyright 2022 NAVER Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.navercorp.pinpoint.batch.alarm.checker; + +import com.navercorp.pinpoint.batch.alarm.collector.ResponseTimeDataCollector; +import com.navercorp.pinpoint.common.trace.ServiceType; +import com.navercorp.pinpoint.web.alarm.CheckerCategory; +import com.navercorp.pinpoint.web.alarm.DataCollectorCategory; +import com.navercorp.pinpoint.web.alarm.vo.Rule; +import com.navercorp.pinpoint.web.applicationmap.histogram.TimeHistogram; +import com.navercorp.pinpoint.web.dao.MapResponseDao; +import com.navercorp.pinpoint.web.vo.Application; +import com.navercorp.pinpoint.web.vo.ResponseTime; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.util.LinkedList; +import java.util.List; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +/** + * @author youngjin.kim2 + */ +public class ApdexScoreCheckerTest { + + private static final String SERVICE_NAME = "local_service"; + private static final String SERVICE_TYPE = "tomcat"; + + private static MapResponseDao mockMapResponseDAO; + + @BeforeClass + public static void before() { + mockMapResponseDAO = (application, range) -> { + List list = new LinkedList<>(); + long timeStamp = 1409814914298L; + ResponseTime responseTime = new ResponseTime(SERVICE_NAME, ServiceType.STAND_ALONE, timeStamp); + list.add(responseTime); + + for (int i=0 ; i < 5; i++) { + for (int j=0 ; j < 5; j++) { + TimeHistogram histogram = new TimeHistogram(ServiceType.STAND_ALONE, timeStamp); + histogram.addCallCountByElapsedTime(1000, false); + histogram.addCallCountByElapsedTime(1000, false); + histogram.addCallCountByElapsedTime(1000, false); + histogram.addCallCountByElapsedTime(1000, false); + histogram.addCallCountByElapsedTime(6000, false); + responseTime.addResponseTime("agent_" + i + "_" + j, histogram); + } + timeStamp += 1; + } + + return list; + }; + } + + /* + * alert conditions not satisfied + */ + @Test + public void checkTest1() { + Application application = new Application(SERVICE_NAME, ServiceType.STAND_ALONE); + ResponseTimeDataCollector collector = new ResponseTimeDataCollector(DataCollectorCategory.RESPONSE_TIME, application, mockMapResponseDAO, System.currentTimeMillis(), 300000); + Rule rule = new Rule(SERVICE_NAME, SERVICE_TYPE, CheckerCategory.SLOW_COUNT.getName(), 90, "testGroup", false, false, false, ""); + ApdexScoreChecker checker = new ApdexScoreChecker(collector, rule); + + checker.check(); + assertTrue(checker.isDetected()); + } + + /* + * alert conditions not satisfied + */ + @Test + public void checkTest2() { + Application application = new Application(SERVICE_NAME, ServiceType.STAND_ALONE); + ResponseTimeDataCollector collector = new ResponseTimeDataCollector(DataCollectorCategory.RESPONSE_TIME, application, mockMapResponseDAO, System.currentTimeMillis(), 300000); + Rule rule = new Rule(SERVICE_NAME, SERVICE_TYPE, CheckerCategory.SLOW_COUNT.getName(), 75, "testGroup", false, false, false, ""); + ApdexScoreChecker checker = new ApdexScoreChecker(collector, rule); + + checker.check(); + assertFalse(checker.isDetected()); + } + +} diff --git a/web/src/main/java/com/navercorp/pinpoint/web/alarm/CheckerCategory.java b/web/src/main/java/com/navercorp/pinpoint/web/alarm/CheckerCategory.java index bd2c51e09331..1e0012430bec 100644 --- a/web/src/main/java/com/navercorp/pinpoint/web/alarm/CheckerCategory.java +++ b/web/src/main/java/com/navercorp/pinpoint/web/alarm/CheckerCategory.java @@ -35,6 +35,8 @@ public enum CheckerCategory { ERROR_RATE("ERROR RATE", DataCollectorCategory.RESPONSE_TIME), TOTAL_COUNT("TOTAL COUNT", DataCollectorCategory.RESPONSE_TIME), + + APDEX_SCORE("APDEX SCORE", DataCollectorCategory.RESPONSE_TIME), SLOW_COUNT_TO_CALLEE("SLOW COUNT TO CALLEE", DataCollectorCategory.CALLER_STAT),