From 036b82cda4bb57af1232dad4b00daf5d814fdab7 Mon Sep 17 00:00:00 2001 From: Abbas Hussain Date: Sun, 20 Dec 2020 22:20:11 -0800 Subject: [PATCH 01/25] Add new stats endpoint and dummy response --- .../ReportsSchedulerPlugin.kt | 4 +- .../resthandler/ReportStatsRestHandler.kt | 95 +++++++++++++++++++ 2 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportStatsRestHandler.kt diff --git a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/ReportsSchedulerPlugin.kt b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/ReportsSchedulerPlugin.kt index 2ab96e4b..286bbc86 100644 --- a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/ReportsSchedulerPlugin.kt +++ b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/ReportsSchedulerPlugin.kt @@ -38,6 +38,7 @@ import com.amazon.opendistroforelasticsearch.reportsscheduler.resthandler.Report import com.amazon.opendistroforelasticsearch.reportsscheduler.resthandler.ReportInstanceListRestHandler import com.amazon.opendistroforelasticsearch.reportsscheduler.resthandler.ReportInstancePollRestHandler import com.amazon.opendistroforelasticsearch.reportsscheduler.resthandler.ReportInstanceRestHandler +import com.amazon.opendistroforelasticsearch.reportsscheduler.resthandler.ReportStatsRestHandler import com.amazon.opendistroforelasticsearch.reportsscheduler.scheduler.ReportDefinitionJobParser import com.amazon.opendistroforelasticsearch.reportsscheduler.scheduler.ReportDefinitionJobRunner import com.amazon.opendistroforelasticsearch.reportsscheduler.settings.PluginSettings @@ -153,7 +154,8 @@ class ReportsSchedulerPlugin : Plugin(), ActionPlugin, JobSchedulerExtension { ReportInstanceRestHandler(), ReportInstanceListRestHandler(), OnDemandReportRestHandler(), - ReportInstancePollRestHandler() + ReportInstancePollRestHandler(), + ReportStatsRestHandler() ) } diff --git a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportStatsRestHandler.kt b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportStatsRestHandler.kt new file mode 100644 index 00000000..be01909c --- /dev/null +++ b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportStatsRestHandler.kt @@ -0,0 +1,95 @@ +/* + * Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). + * You may not use this file except in compliance with the License. + * A copy of the License is located at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * or in the "license" file accompanying this file. This file 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.amazon.opendistroforelasticsearch.reportsscheduler.resthandler + +import com.amazon.opendistroforelasticsearch.reportsscheduler.ReportsSchedulerPlugin.Companion.BASE_REPORTS_URI +import com.amazon.opendistroforelasticsearch.reportsscheduler.action.CreateReportDefinitionAction +import com.amazon.opendistroforelasticsearch.reportsscheduler.action.DeleteReportDefinitionAction +import com.amazon.opendistroforelasticsearch.reportsscheduler.action.GetReportDefinitionAction +import com.amazon.opendistroforelasticsearch.reportsscheduler.action.ReportDefinitionActions +import com.amazon.opendistroforelasticsearch.reportsscheduler.action.UpdateReportDefinitionAction +import com.amazon.opendistroforelasticsearch.reportsscheduler.model.CreateReportDefinitionRequest +import com.amazon.opendistroforelasticsearch.reportsscheduler.model.DeleteReportDefinitionRequest +import com.amazon.opendistroforelasticsearch.reportsscheduler.model.GetReportDefinitionRequest +import com.amazon.opendistroforelasticsearch.reportsscheduler.model.RestTag.REPORT_DEFINITION_ID_FIELD +import com.amazon.opendistroforelasticsearch.reportsscheduler.model.UpdateReportDefinitionRequest +import com.amazon.opendistroforelasticsearch.reportsscheduler.util.contentParserNextToken +import org.elasticsearch.client.node.NodeClient +import org.elasticsearch.rest.BaseRestHandler +import org.elasticsearch.rest.BaseRestHandler.RestChannelConsumer +import org.elasticsearch.rest.BytesRestResponse +import org.elasticsearch.rest.RestHandler.Route +import org.elasticsearch.rest.RestRequest +import org.elasticsearch.rest.RestRequest.Method.DELETE +import org.elasticsearch.rest.RestRequest.Method.GET +import org.elasticsearch.rest.RestRequest.Method.POST +import org.elasticsearch.rest.RestRequest.Method.PUT +import org.elasticsearch.rest.RestStatus + +/** + * Rest handler for getting reporting backend stats + * This handler uses [ReportDefinitionActions]. TODO: change this + */ +internal class ReportStatsRestHandler : BaseRestHandler() { + companion object { + private const val REPORT_STATS_ACTION = "report_definition_stats" + private const val REPORT_STATS_URL = "$BASE_REPORTS_URI/stats" + public const val COUNT = 4 + } + + /** + * {@inheritDoc} + */ + override fun getName(): String { + return REPORT_STATS_ACTION + } + + /** + * {@inheritDoc} + */ + override fun routes(): List { + return listOf( + /** + * Get reporting backend stats + * Request URL: GET REPORT_STATS_URL + * Request body: Ref [com.amazon.opendistroforelasticsearch.reportsscheduler.model.GetReportDefinitionRequest] + * Response body: Ref [com.amazon.opendistroforelasticsearch.reportsscheduler.model.GetReportDefinitionResponse] + */ + Route(GET, "$REPORT_STATS_URL") + ) + } + + /** + * {@inheritDoc} + */ + override fun responseParams(): Set { + return setOf() + } + + /** + * {@inheritDoc} + */ + override fun prepareRequest(request: RestRequest, client: NodeClient): RestChannelConsumer { + return when (request.method()) { + GET -> RestChannelConsumer { + it.sendResponse(BytesRestResponse(RestStatus.OK, "{\"count\" : \"15\" }")) + } + else -> RestChannelConsumer { + it.sendResponse(BytesRestResponse(RestStatus.METHOD_NOT_ALLOWED, "${request.method()} is not allowed")) + } + } + } +} From 665b0dfb4a0780d0e22d1403e32f92aba6a82777 Mon Sep 17 00:00:00 2001 From: Abbas Hussain Date: Sun, 20 Dec 2020 22:57:04 -0800 Subject: [PATCH 02/25] Add metric classes --- .../metrics/BasicCounter.java | 42 ++++++++ .../reportsscheduler/metrics/Counter.java | 26 +++++ .../reportsscheduler/metrics/GaugeMetric.java | 38 +++++++ .../reportsscheduler/metrics/Metric.java | 31 ++++++ .../reportsscheduler/metrics/Metrics.java | 28 +++++ .../metrics/NumericMetric.java | 49 +++++++++ .../metrics/RollingCounter.java | 102 ++++++++++++++++++ .../reportsscheduler/metrics/MetricName.kt | 38 +++++++ .../resthandler/ReportStatsRestHandler.kt | 23 ++-- 9 files changed, 364 insertions(+), 13 deletions(-) create mode 100644 reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/BasicCounter.java create mode 100644 reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Counter.java create mode 100644 reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/GaugeMetric.java create mode 100644 reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Metric.java create mode 100644 reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Metrics.java create mode 100644 reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/NumericMetric.java create mode 100644 reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/RollingCounter.java create mode 100644 reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricName.kt diff --git a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/BasicCounter.java b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/BasicCounter.java new file mode 100644 index 00000000..a9b7a626 --- /dev/null +++ b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/BasicCounter.java @@ -0,0 +1,42 @@ +/* + * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). + * You may not use this file except in compliance with the License. + * A copy of the License is located at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * or in the "license" file accompanying this file. This file 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.amazon.opendistroforelasticsearch.reportsscheduler.metrics; + +import java.util.concurrent.atomic.LongAdder; + +public class BasicCounter implements Counter { + private LongAdder count = new LongAdder(); + + @Override + public void increment() { + count.increment(); + } + + @Override + public void add(long n) { + count.add(n); + } + + @Override + public Long getValue() { + return count.longValue(); + } + + @Override + public void reset() { + count.reset(); + } +} diff --git a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Counter.java b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Counter.java new file mode 100644 index 00000000..b01dce5d --- /dev/null +++ b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Counter.java @@ -0,0 +1,26 @@ +/* + * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). + * You may not use this file except in compliance with the License. + * A copy of the License is located at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * or in the "license" file accompanying this file. This file 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.amazon.opendistroforelasticsearch.reportsscheduler.metrics; + +public interface Counter { + void increment(); + + void add(long n); + + T getValue(); + + void reset(); +} diff --git a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/GaugeMetric.java b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/GaugeMetric.java new file mode 100644 index 00000000..d0cbc831 --- /dev/null +++ b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/GaugeMetric.java @@ -0,0 +1,38 @@ +/* + * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). + * You may not use this file except in compliance with the License. + * A copy of the License is located at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * or in the "license" file accompanying this file. This file 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.amazon.opendistroforelasticsearch.reportsscheduler.metrics; + +import java.util.function.Supplier; + +/** + * Gauge metric, an instant value like cpu usage, state and so on + */ +public class GaugeMetric extends Metric { + private Supplier loadValue; + + public GaugeMetric(String name, Supplier supplier) { + super(name); + this.loadValue = supplier; + } + + public String getName() { + return super.getName(); + } + + public T getValue() { + return loadValue.get(); + } +} diff --git a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Metric.java b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Metric.java new file mode 100644 index 00000000..e3e36893 --- /dev/null +++ b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Metric.java @@ -0,0 +1,31 @@ +/* + * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). + * You may not use this file except in compliance with the License. + * A copy of the License is located at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * or in the "license" file accompanying this file. This file 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.amazon.opendistroforelasticsearch.reportsscheduler.metrics; + +public abstract class Metric { + + private String name; + + public Metric(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public abstract T getValue(); +} diff --git a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Metrics.java b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Metrics.java new file mode 100644 index 00000000..2b0ed6b8 --- /dev/null +++ b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Metrics.java @@ -0,0 +1,28 @@ +/* + * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). + * You may not use this file except in compliance with the License. + * A copy of the License is located at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * or in the "license" file accompanying this file. This file 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.amazon.opendistroforelasticsearch.reportsscheduler.metrics; + +import java.util.concurrent.ConcurrentHashMap; + +public class Metrics { + + private static Metrics metrics = new Metrics(); + private ConcurrentHashMap> registeredMetricsByName = new ConcurrentHashMap<>(); + + public static Metrics getInstance() { + return metrics; + } +} diff --git a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/NumericMetric.java b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/NumericMetric.java new file mode 100644 index 00000000..b6389309 --- /dev/null +++ b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/NumericMetric.java @@ -0,0 +1,49 @@ +/* + * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). + * You may not use this file except in compliance with the License. + * A copy of the License is located at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * or in the "license" file accompanying this file. This file 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.amazon.opendistroforelasticsearch.reportsscheduler.metrics; + +public class NumericMetric extends Metric { + private Counter counter; + + public NumericMetric(String name, Counter counter) { + super(name); + this.counter = counter; + } + + public String getName() { + return super.getName(); + } + + public Counter getCounter() { + return counter; + } + + public void increment() { + counter.increment(); + } + + public void increment(long n) { + counter.add(n); + } + + public T getValue() { + return counter.getValue(); + } + + public void clear() { + counter.reset(); + } +} diff --git a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/RollingCounter.java b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/RollingCounter.java new file mode 100644 index 00000000..792ffaa3 --- /dev/null +++ b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/RollingCounter.java @@ -0,0 +1,102 @@ +/* + * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). + * You may not use this file except in compliance with the License. + * A copy of the License is located at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * or in the "license" file accompanying this file. This file 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.amazon.opendistroforelasticsearch.reportsscheduler.metrics; + +import java.time.Clock; +import java.util.concurrent.ConcurrentSkipListMap; +import java.util.concurrent.atomic.LongAdder; + +/** + * Rolling counter. The count is refreshed every interval. In every interval the count is cumulative. + */ +public class RollingCounter implements Counter { + private static final long METRICS_ROLLING_WINDOW_VALUE = 3600L; + private static final long METRICS_ROLLING_INTERVAL_VALUE = 60L; + + private final long capacity; + private final long window; + private final long interval; + private final Clock clock; + private final ConcurrentSkipListMap time2CountWin; + private final LongAdder count; + + public RollingCounter() { + this(METRICS_ROLLING_WINDOW_VALUE, METRICS_ROLLING_INTERVAL_VALUE); + } + + public RollingCounter(long window, long interval, Clock clock) { + this.window = window; + this.interval = interval; + this.clock = clock; + time2CountWin = new ConcurrentSkipListMap<>(); + count = new LongAdder(); + capacity = window / interval * 2; + } + + public RollingCounter(long window, long interval) { + this(window, interval, Clock.systemDefaultZone()); + } + + @Override + public void increment() { + add(1L); + } + + @Override + public void add(long n) { + trim(); + time2CountWin.compute(getKey(clock.millis()), (k, v) -> (v == null) ? n : v + n); + } + + @Override + public Long getValue() { + return getValue(getPreKey(clock.millis())); + } + + public long getValue(long key) { + Long res = time2CountWin.get(key); + if (res == null) { + return 0; + } + return res; + } + + public long getSum() { + return count.longValue(); + } + + private void trim() { + if (time2CountWin.size() > capacity) { + time2CountWin.headMap(getKey(clock.millis() - window * 1000)).clear(); + } + } + + private long getKey(long millis) { + return millis / 1000 / this.interval; + } + + private long getPreKey(long millis) { + return getKey(millis) - 1; + } + + public int size() { + return time2CountWin.size(); + } + + public void reset() { + time2CountWin.clear(); + } +} diff --git a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricName.kt b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricName.kt new file mode 100644 index 00000000..e516e5ad --- /dev/null +++ b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricName.kt @@ -0,0 +1,38 @@ +/* + * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). + * You may not use this file except in compliance with the License. + * A copy of the License is located at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * or in the "license" file accompanying this file. This file 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.amazon.opendistroforelasticsearch.reportsscheduler.metrics + +import com.amazon.opendistroforelasticsearch.reportsscheduler.resthandler.ReportStatsRestHandler.Companion.COUNT + +enum class MetricName(val metricName: String, val value: Int, val numerical: Boolean) { + + REPORT_DEFINITION_CREATE_COUNT("report_definition_create_count", COUNT, true), + REPORT_DEFINITION_CREATE_USER_ERROR("report_definition_create_user_error", COUNT, true), + REPORT_DEFINITION_CREATE_SYSTEM_ERROR("report_definition_create_system_error", COUNT, true), + REPORT_DEFINITION_UPDATE_COUNT("report_definition_update_count", COUNT, true), + REPORT_DEFINITION_UPDATE_USER_ERROR("report_definition_update_user_error", COUNT, true), + REPORT_DEFINITION_UPDATE_SYSTEM_ERROR("report_definition_update_system_error", COUNT, true), + REPORT_DEFINITION_DELETE_COUNT("report_definition_delete_count", COUNT, true), + REPORT_DEFINITION_DELETE_USER_ERROR("report_definition_delete_user_error", COUNT, true), + REPORT_DEFINITION_DELETE_SYSTEM_ERROR("report_definition_delete_system_error", COUNT, true), + REPORT_DEFINITION_LIST_COUNT("report_definition_list_count", COUNT, true), + REPORT_DEFINITION_LIST_USER_ERROR("report_definition_list_user_error", COUNT, true), + REPORT_DEFINITION_LIST_SYSTEM_ERROR("report_definition_list_system_error", COUNT, true), + + REPORT_INSTANCE_LIST_COUNT("report_instance_list_count", COUNT, true), + REPORT_INSTANCE_LIST_USER_ERROR("report_instance_list_user_error", COUNT, true), + REPORT_INSTANCE_LIST_SYSTEM_ERROR("report_instance_list_system_error", COUNT, true) +} diff --git a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportStatsRestHandler.kt b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportStatsRestHandler.kt index be01909c..e46a3b26 100644 --- a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportStatsRestHandler.kt +++ b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportStatsRestHandler.kt @@ -16,27 +16,24 @@ package com.amazon.opendistroforelasticsearch.reportsscheduler.resthandler import com.amazon.opendistroforelasticsearch.reportsscheduler.ReportsSchedulerPlugin.Companion.BASE_REPORTS_URI -import com.amazon.opendistroforelasticsearch.reportsscheduler.action.CreateReportDefinitionAction -import com.amazon.opendistroforelasticsearch.reportsscheduler.action.DeleteReportDefinitionAction -import com.amazon.opendistroforelasticsearch.reportsscheduler.action.GetReportDefinitionAction +// import com.amazon.opendistroforelasticsearch.reportsscheduler.action.CreateReportDefinitionAction +// import com.amazon.opendistroforelasticsearch.reportsscheduler.action.DeleteReportDefinitionAction +// import com.amazon.opendistroforelasticsearch.reportsscheduler.action.GetReportDefinitionAction import com.amazon.opendistroforelasticsearch.reportsscheduler.action.ReportDefinitionActions -import com.amazon.opendistroforelasticsearch.reportsscheduler.action.UpdateReportDefinitionAction -import com.amazon.opendistroforelasticsearch.reportsscheduler.model.CreateReportDefinitionRequest -import com.amazon.opendistroforelasticsearch.reportsscheduler.model.DeleteReportDefinitionRequest -import com.amazon.opendistroforelasticsearch.reportsscheduler.model.GetReportDefinitionRequest -import com.amazon.opendistroforelasticsearch.reportsscheduler.model.RestTag.REPORT_DEFINITION_ID_FIELD -import com.amazon.opendistroforelasticsearch.reportsscheduler.model.UpdateReportDefinitionRequest -import com.amazon.opendistroforelasticsearch.reportsscheduler.util.contentParserNextToken +// import com.amazon.opendistroforelasticsearch.reportsscheduler.action.UpdateReportDefinitionAction +// import com.amazon.opendistroforelasticsearch.reportsscheduler.model.CreateReportDefinitionRequest +// import com.amazon.opendistroforelasticsearch.reportsscheduler.model.DeleteReportDefinitionRequest +// import com.amazon.opendistroforelasticsearch.reportsscheduler.model.GetReportDefinitionRequest +// import com.amazon.opendistroforelasticsearch.reportsscheduler.model.RestTag.REPORT_DEFINITION_ID_FIELD +// import com.amazon.opendistroforelasticsearch.reportsscheduler.model.UpdateReportDefinitionRequest +// import com.amazon.opendistroforelasticsearch.reportsscheduler.util.contentParserNextToken import org.elasticsearch.client.node.NodeClient import org.elasticsearch.rest.BaseRestHandler import org.elasticsearch.rest.BaseRestHandler.RestChannelConsumer import org.elasticsearch.rest.BytesRestResponse import org.elasticsearch.rest.RestHandler.Route import org.elasticsearch.rest.RestRequest -import org.elasticsearch.rest.RestRequest.Method.DELETE import org.elasticsearch.rest.RestRequest.Method.GET -import org.elasticsearch.rest.RestRequest.Method.POST -import org.elasticsearch.rest.RestRequest.Method.PUT import org.elasticsearch.rest.RestStatus /** From a742391aa12c270b307292be8517c7f54800dec9 Mon Sep 17 00:00:00 2001 From: Abbas Hussain Date: Sun, 20 Dec 2020 23:52:33 -0800 Subject: [PATCH 03/25] Add json dependencies and more metric classes --- reports-scheduler/build.gradle | 1 + .../metrics/MetricFactory.java | 40 ++++++++ .../reportsscheduler/metrics/MetricName.java | 91 +++++++++++++++++++ .../reportsscheduler/metrics/Metrics.java | 63 +++++++++++++ .../reportsscheduler/metrics/MetricName.kt | 38 -------- 5 files changed, 195 insertions(+), 38 deletions(-) create mode 100644 reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricFactory.java create mode 100644 reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricName.java delete mode 100644 reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricName.kt diff --git a/reports-scheduler/build.gradle b/reports-scheduler/build.gradle index 28d5891d..b1ef146f 100644 --- a/reports-scheduler/build.gradle +++ b/reports-scheduler/build.gradle @@ -127,6 +127,7 @@ dependencies { compile "${group}:common-utils:${opendistroVersion}.2" compileOnly "${group}:opendistro-job-scheduler-spi:${opendistroVersion}.0" compile group: 'com.google.guava', name: 'guava', version: '15.0' + compile "org.json:json:20180813" testImplementation( 'org.assertj:assertj-core:3.16.1', 'org.junit.jupiter:junit-jupiter-api:5.6.2' diff --git a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricFactory.java b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricFactory.java new file mode 100644 index 00000000..3ce23cc6 --- /dev/null +++ b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricFactory.java @@ -0,0 +1,40 @@ +/* + * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). + * You may not use this file except in compliance with the License. + * A copy of the License is located at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * or in the "license" file accompanying this file. This file 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.amazon.opendistroforelasticsearch.reportsscheduler.metrics; + +public class MetricFactory { + public static Metric createMetric(MetricName name) { + + switch (name) { + case REQ_TOTAL: + case DEFAULT_CURSOR_REQUEST_TOTAL: + case DEFAULT: + case PPL_REQ_TOTAL: + return new NumericMetric<>(name.getName(), new BasicCounter()); + case REQ_COUNT_TOTAL: + case DEFAULT_CURSOR_REQUEST_COUNT_TOTAL: + case FAILED_REQ_COUNT_CUS: + case FAILED_REQ_COUNT_SYS: + case FAILED_REQ_COUNT_CB: + case PPL_REQ_COUNT_TOTAL: + case PPL_FAILED_REQ_COUNT_CUS: + case PPL_FAILED_REQ_COUNT_SYS: + return new NumericMetric<>(name.getName(), new RollingCounter()); + default: + return new NumericMetric<>(name.getName(), new BasicCounter()); + } + } +} diff --git a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricName.java b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricName.java new file mode 100644 index 00000000..e15c8365 --- /dev/null +++ b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricName.java @@ -0,0 +1,91 @@ +/* + * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). + * You may not use this file except in compliance with the License. + * A copy of the License is located at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * or in the "license" file accompanying this file. This file 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.amazon.opendistroforelasticsearch.reportsscheduler.metrics; + +//enum class MetricName(val metricName: String, val value: Int, val numerical: Boolean) { +// +// REPORT_DEFINITION_CREATE_COUNT("report_definition_create_count", COUNT, true), +// REPORT_DEFINITION_CREATE_USER_ERROR("report_definition_create_user_error", COUNT, true), +// REPORT_DEFINITION_CREATE_SYSTEM_ERROR("report_definition_create_system_error", COUNT, true), +// REPORT_DEFINITION_UPDATE_COUNT("report_definition_update_count", COUNT, true), +// REPORT_DEFINITION_UPDATE_USER_ERROR("report_definition_update_user_error", COUNT, true), +// REPORT_DEFINITION_UPDATE_SYSTEM_ERROR("report_definition_update_system_error", COUNT, true), +// REPORT_DEFINITION_DELETE_COUNT("report_definition_delete_count", COUNT, true), +// REPORT_DEFINITION_DELETE_USER_ERROR("report_definition_delete_user_error", COUNT, true), +// REPORT_DEFINITION_DELETE_SYSTEM_ERROR("report_definition_delete_system_error", COUNT, true), +// REPORT_DEFINITION_LIST_COUNT("report_definition_list_count", COUNT, true), +// REPORT_DEFINITION_LIST_USER_ERROR("report_definition_list_user_error", COUNT, true), +// REPORT_DEFINITION_LIST_SYSTEM_ERROR("report_definition_list_system_error", COUNT, true), +// +// REPORT_INSTANCE_LIST_COUNT("report_instance_list_count", COUNT, true), +// REPORT_INSTANCE_LIST_USER_ERROR("report_instance_list_user_error", COUNT, true), +// REPORT_INSTANCE_LIST_SYSTEM_ERROR("report_instance_list_system_error", COUNT, true) +//} + + +import com.google.common.collect.ImmutableSet; + +import java.util.Arrays; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; + +public enum MetricName { + REQ_TOTAL("request_total"), + REQ_COUNT_TOTAL("request_count"), + FAILED_REQ_COUNT_SYS("failed_request_count_syserr"), + FAILED_REQ_COUNT_CUS("failed_request_count_cuserr"), + FAILED_REQ_COUNT_CB("failed_request_count_cb"), + DEFAULT_CURSOR_REQUEST_TOTAL("default_cursor_request_total"), + DEFAULT_CURSOR_REQUEST_COUNT_TOTAL("default_cursor_request_count"), + DEFAULT("default"), + + PPL_REQ_TOTAL("ppl_request_total"), + PPL_REQ_COUNT_TOTAL("ppl_request_count"), + PPL_FAILED_REQ_COUNT_SYS("ppl_failed_request_count_syserr"), + PPL_FAILED_REQ_COUNT_CUS("ppl_failed_request_count_cuserr"); + + private String name; + + MetricName(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public static List getNames() { + return Arrays.stream(MetricName.values()).map(v -> v.name).collect(Collectors.toList()); + } + + + private static Set NUMERICAL_METRIC = new ImmutableSet.Builder() + .add(PPL_REQ_TOTAL) + .add(PPL_REQ_COUNT_TOTAL) + .add(PPL_FAILED_REQ_COUNT_SYS) + .add(PPL_FAILED_REQ_COUNT_CUS) + .build(); + + public boolean isNumerical() { + return this == REQ_TOTAL || this == REQ_COUNT_TOTAL || this == FAILED_REQ_COUNT_SYS + || this == FAILED_REQ_COUNT_CUS || this == FAILED_REQ_COUNT_CB || this == DEFAULT + || this == DEFAULT_CURSOR_REQUEST_TOTAL || this == DEFAULT_CURSOR_REQUEST_COUNT_TOTAL + || NUMERICAL_METRIC.contains(this); + } +} + + diff --git a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Metrics.java b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Metrics.java index 2b0ed6b8..4f97c58e 100644 --- a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Metrics.java +++ b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Metrics.java @@ -13,8 +13,12 @@ * permissions and limitations under the License. */ +// import org.json.JSONObject; + package com.amazon.opendistroforelasticsearch.reportsscheduler.metrics; +import java.util.ArrayList; +import java.util.List; import java.util.concurrent.ConcurrentHashMap; public class Metrics { @@ -22,7 +26,66 @@ public class Metrics { private static Metrics metrics = new Metrics(); private ConcurrentHashMap> registeredMetricsByName = new ConcurrentHashMap<>(); + private Metrics() { + } + + public void registerDefaultMetrics() { + for (MetricName metricName : MetricName.values()) { + registerMetric(MetricFactory.createMetric(metricName)); + } + } + + public void registerMetric(Metric metric) { + registeredMetricsByName.put(metric.getName(), metric); + } + + public void unregisterMetric(String name) { + if (name == null) { + return; + } + + registeredMetricsByName.remove(name); + } + + public Metric getMetric(String name) { + if (name == null) { + return null; + } + + return registeredMetricsByName.get(name); + } + + public NumericMetric getNumericalMetric(MetricName metricName) { + String name = metricName.getName(); + if (!metricName.isNumerical()) { + name = MetricName.DEFAULT.getName(); + } + + return (NumericMetric) registeredMetricsByName.get(name); + } + + public List> getAllMetrics() { + return new ArrayList<>(registeredMetricsByName.values()); + } + public static Metrics getInstance() { return metrics; } + +// public String collectToJSON() { +// JSONObject metricsJSONObject = new JSONObject(); +// +// for (Metric metric : registeredMetricsByName.values()) { +// if (metric.getName().equals("default")) { +// continue; +// } +// metricsJSONObject.put(metric.getName(), metric.getValue()); +// } +// +// return metricsJSONObject.toString(); +// } + + public void clear() { + registeredMetricsByName.clear(); + } } diff --git a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricName.kt b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricName.kt deleted file mode 100644 index e516e5ad..00000000 --- a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricName.kt +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * or in the "license" file accompanying this file. This file 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.amazon.opendistroforelasticsearch.reportsscheduler.metrics - -import com.amazon.opendistroforelasticsearch.reportsscheduler.resthandler.ReportStatsRestHandler.Companion.COUNT - -enum class MetricName(val metricName: String, val value: Int, val numerical: Boolean) { - - REPORT_DEFINITION_CREATE_COUNT("report_definition_create_count", COUNT, true), - REPORT_DEFINITION_CREATE_USER_ERROR("report_definition_create_user_error", COUNT, true), - REPORT_DEFINITION_CREATE_SYSTEM_ERROR("report_definition_create_system_error", COUNT, true), - REPORT_DEFINITION_UPDATE_COUNT("report_definition_update_count", COUNT, true), - REPORT_DEFINITION_UPDATE_USER_ERROR("report_definition_update_user_error", COUNT, true), - REPORT_DEFINITION_UPDATE_SYSTEM_ERROR("report_definition_update_system_error", COUNT, true), - REPORT_DEFINITION_DELETE_COUNT("report_definition_delete_count", COUNT, true), - REPORT_DEFINITION_DELETE_USER_ERROR("report_definition_delete_user_error", COUNT, true), - REPORT_DEFINITION_DELETE_SYSTEM_ERROR("report_definition_delete_system_error", COUNT, true), - REPORT_DEFINITION_LIST_COUNT("report_definition_list_count", COUNT, true), - REPORT_DEFINITION_LIST_USER_ERROR("report_definition_list_user_error", COUNT, true), - REPORT_DEFINITION_LIST_SYSTEM_ERROR("report_definition_list_system_error", COUNT, true), - - REPORT_INSTANCE_LIST_COUNT("report_instance_list_count", COUNT, true), - REPORT_INSTANCE_LIST_USER_ERROR("report_instance_list_user_error", COUNT, true), - REPORT_INSTANCE_LIST_SYSTEM_ERROR("report_instance_list_system_error", COUNT, true) -} From 7a8d7c42bb6c7f1ac355aa5c6b1fe47d36ba7bf7 Mon Sep 17 00:00:00 2001 From: Abbas Hussain Date: Sun, 20 Dec 2020 23:58:30 -0800 Subject: [PATCH 04/25] Collect metrics to json --- .../reportsscheduler/metrics/Metrics.java | 27 +++++++++---------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Metrics.java b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Metrics.java index 4f97c58e..642fb275 100644 --- a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Metrics.java +++ b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Metrics.java @@ -13,10 +13,9 @@ * permissions and limitations under the License. */ -// import org.json.JSONObject; - package com.amazon.opendistroforelasticsearch.reportsscheduler.metrics; +import org.json.JSONObject; import java.util.ArrayList; import java.util.List; import java.util.concurrent.ConcurrentHashMap; @@ -72,18 +71,18 @@ public static Metrics getInstance() { return metrics; } -// public String collectToJSON() { -// JSONObject metricsJSONObject = new JSONObject(); -// -// for (Metric metric : registeredMetricsByName.values()) { -// if (metric.getName().equals("default")) { -// continue; -// } -// metricsJSONObject.put(metric.getName(), metric.getValue()); -// } -// -// return metricsJSONObject.toString(); -// } + public String collectToJSON() { + JSONObject metricsJSONObject = new JSONObject(); + + for (Metric metric : registeredMetricsByName.values()) { + if (metric.getName().equals("default")) { + continue; + } + metricsJSONObject.put(metric.getName(), metric.getValue()); + } + + return metricsJSONObject.toString(); + } public void clear() { registeredMetricsByName.clear(); From 6739842c385ac57b8d71c53913c351f8d24312c5 Mon Sep 17 00:00:00 2001 From: Abbas Hussain Date: Mon, 21 Dec 2020 00:13:27 -0800 Subject: [PATCH 05/25] Add metrics json to stats response --- .../reportsscheduler/ReportsSchedulerPlugin.kt | 5 +++++ .../reportsscheduler/resthandler/ReportStatsRestHandler.kt | 3 ++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/ReportsSchedulerPlugin.kt b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/ReportsSchedulerPlugin.kt index 286bbc86..33082fd9 100644 --- a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/ReportsSchedulerPlugin.kt +++ b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/ReportsSchedulerPlugin.kt @@ -32,6 +32,7 @@ import com.amazon.opendistroforelasticsearch.reportsscheduler.action.UpdateRepor import com.amazon.opendistroforelasticsearch.reportsscheduler.index.ReportDefinitionsIndex import com.amazon.opendistroforelasticsearch.reportsscheduler.index.ReportDefinitionsIndex.REPORT_DEFINITIONS_INDEX_NAME import com.amazon.opendistroforelasticsearch.reportsscheduler.index.ReportInstancesIndex +import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.Metrics import com.amazon.opendistroforelasticsearch.reportsscheduler.resthandler.OnDemandReportRestHandler import com.amazon.opendistroforelasticsearch.reportsscheduler.resthandler.ReportDefinitionListRestHandler import com.amazon.opendistroforelasticsearch.reportsscheduler.resthandler.ReportDefinitionRestHandler @@ -42,6 +43,7 @@ import com.amazon.opendistroforelasticsearch.reportsscheduler.resthandler.Report import com.amazon.opendistroforelasticsearch.reportsscheduler.scheduler.ReportDefinitionJobParser import com.amazon.opendistroforelasticsearch.reportsscheduler.scheduler.ReportDefinitionJobRunner import com.amazon.opendistroforelasticsearch.reportsscheduler.settings.PluginSettings + import org.elasticsearch.action.ActionRequest import org.elasticsearch.action.ActionResponse import org.elasticsearch.client.Client @@ -148,6 +150,9 @@ class ReportsSchedulerPlugin : Plugin(), ActionPlugin, JobSchedulerExtension { indexNameExpressionResolver: IndexNameExpressionResolver, nodesInCluster: Supplier ): List { + + Metrics.getInstance().registerDefaultMetrics() + return listOf( ReportDefinitionRestHandler(), ReportDefinitionListRestHandler(), diff --git a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportStatsRestHandler.kt b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportStatsRestHandler.kt index e46a3b26..54d16b6a 100644 --- a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportStatsRestHandler.kt +++ b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportStatsRestHandler.kt @@ -27,6 +27,7 @@ import com.amazon.opendistroforelasticsearch.reportsscheduler.action.ReportDefin // import com.amazon.opendistroforelasticsearch.reportsscheduler.model.RestTag.REPORT_DEFINITION_ID_FIELD // import com.amazon.opendistroforelasticsearch.reportsscheduler.model.UpdateReportDefinitionRequest // import com.amazon.opendistroforelasticsearch.reportsscheduler.util.contentParserNextToken +import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.Metrics import org.elasticsearch.client.node.NodeClient import org.elasticsearch.rest.BaseRestHandler import org.elasticsearch.rest.BaseRestHandler.RestChannelConsumer @@ -82,7 +83,7 @@ internal class ReportStatsRestHandler : BaseRestHandler() { override fun prepareRequest(request: RestRequest, client: NodeClient): RestChannelConsumer { return when (request.method()) { GET -> RestChannelConsumer { - it.sendResponse(BytesRestResponse(RestStatus.OK, "{\"count\" : \"15\" }")) + it.sendResponse(BytesRestResponse(RestStatus.OK, Metrics.getInstance().collectToJSON())) } else -> RestChannelConsumer { it.sendResponse(BytesRestResponse(RestStatus.METHOD_NOT_ALLOWED, "${request.method()} is not allowed")) From a5611d76650689becb1bbd1e4c78764ed96b6769 Mon Sep 17 00:00:00 2001 From: Abbas Hussain Date: Mon, 21 Dec 2020 03:01:00 -0800 Subject: [PATCH 06/25] Add json unflattener --- reports-scheduler/build.gradle | 2 ++ 1 file changed, 2 insertions(+) diff --git a/reports-scheduler/build.gradle b/reports-scheduler/build.gradle index b1ef146f..6dd48957 100644 --- a/reports-scheduler/build.gradle +++ b/reports-scheduler/build.gradle @@ -128,6 +128,8 @@ dependencies { compileOnly "${group}:opendistro-job-scheduler-spi:${opendistroVersion}.0" compile group: 'com.google.guava', name: 'guava', version: '15.0' compile "org.json:json:20180813" + compile group: 'com.github.wnameless', name: 'json-flattener', version: '0.1.0' + testImplementation( 'org.assertj:assertj-core:3.16.1', 'org.junit.jupiter:junit-jupiter-api:5.6.2' From 43444a50133480b900105f53e792b7ba2c8f1d83 Mon Sep 17 00:00:00 2001 From: Abbas Hussain Date: Mon, 21 Dec 2020 03:03:08 -0800 Subject: [PATCH 07/25] Add Json unflattening logic --- .../reportsscheduler/metrics/Metrics.java | 19 +++++++++++++++++++ .../resthandler/ReportStatsRestHandler.kt | 3 ++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Metrics.java b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Metrics.java index 642fb275..26809ef2 100644 --- a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Metrics.java +++ b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Metrics.java @@ -20,6 +20,9 @@ import java.util.List; import java.util.concurrent.ConcurrentHashMap; +// import com.github.wnameless.json.flattener.JsonFlattener; +import com.github.wnameless.json.unflattener.JsonUnflattener; + public class Metrics { private static Metrics metrics = new Metrics(); @@ -84,6 +87,22 @@ public String collectToJSON() { return metricsJSONObject.toString(); } + public String collectToFlattenedJSON() { + String flattenedJson = "{\"report_definition.create.count\":2,\"report_definition.create.total\":4," + + "\"report_definition.create.system_error\":2,\"report_definition.create.customer_error\":3," + + "\"report_definition.list.count\":2,\"report_definition.list.total\":4," + + "\"report_definition.list.system_error\":2,\"report_definition.list.customer_error\":3," + + "\"report_instance.create.count\":2,\"report_instance.create.total\":4," + + "\"report_instance.create.system_error\":2,\"report_instance.create.customer_error\":3}"; + String nestedJson = "{\"report_definition\":{\"create\":{\"count\":2," + + "\"total\":4,\"system_error\":2,\"customer_error\":3},\"list\":" + + "{\"count\":2,\"total\":4,\"system_error\":2,\"customer_error\":3}}," + + "\"report_instance\":{\"create\":{\"count\":2,\"total\":4,\"system_error\":2," + + "\"customer_error\":3}}}"; + String metricsJson = JsonUnflattener.unflatten(flattenedJson); + return metricsJson; + } + public void clear() { registeredMetricsByName.clear(); } diff --git a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportStatsRestHandler.kt b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportStatsRestHandler.kt index 54d16b6a..0279f277 100644 --- a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportStatsRestHandler.kt +++ b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportStatsRestHandler.kt @@ -83,7 +83,8 @@ internal class ReportStatsRestHandler : BaseRestHandler() { override fun prepareRequest(request: RestRequest, client: NodeClient): RestChannelConsumer { return when (request.method()) { GET -> RestChannelConsumer { - it.sendResponse(BytesRestResponse(RestStatus.OK, Metrics.getInstance().collectToJSON())) +// it.sendResponse(BytesRestResponse(RestStatus.OK, Metrics.getInstance().collectToJSON())) + it.sendResponse(BytesRestResponse(RestStatus.OK, Metrics.getInstance().collectToFlattenedJSON())) } else -> RestChannelConsumer { it.sendResponse(BytesRestResponse(RestStatus.METHOD_NOT_ALLOWED, "${request.method()} is not allowed")) From fddeb3e96082545f5b47523d6a3c81faaa5f4edf Mon Sep 17 00:00:00 2001 From: Abbas Hussain Date: Wed, 23 Dec 2020 14:48:03 -0800 Subject: [PATCH 08/25] Add metric names for reporting backend --- .../metrics/MetricFactory.java | 61 +++++++-- .../reportsscheduler/metrics/MetricName.java | 124 ++++++++++-------- 2 files changed, 122 insertions(+), 63 deletions(-) diff --git a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricFactory.java b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricFactory.java index 3ce23cc6..41104107 100644 --- a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricFactory.java +++ b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricFactory.java @@ -19,19 +19,58 @@ public class MetricFactory { public static Metric createMetric(MetricName name) { switch (name) { - case REQ_TOTAL: - case DEFAULT_CURSOR_REQUEST_TOTAL: + case REPORT_DEFINITION_CREATE_TOTAL: + case REPORT_DEFINITION_UPDATE_TOTAL: + case REPORT_DEFINITION_INFO_TOTAL: + case REPORT_DEFINITION_DELETE_TOTAL: + case REPORT_DEFINITION_LIST_TOTAL: + case REPORT_INSTANCE_UPDATE_TOTAL: + case REPORT_INSTANCE_INFO_TOTAL: + case REPORT_INSTANCE_LIST_TOTAL: + case REPORT_FROM_DEFINITION_TOTAL: + case REPORT_FROM_DEFINITION_ID_TOTAL: case DEFAULT: - case PPL_REQ_TOTAL: return new NumericMetric<>(name.getName(), new BasicCounter()); - case REQ_COUNT_TOTAL: - case DEFAULT_CURSOR_REQUEST_COUNT_TOTAL: - case FAILED_REQ_COUNT_CUS: - case FAILED_REQ_COUNT_SYS: - case FAILED_REQ_COUNT_CB: - case PPL_REQ_COUNT_TOTAL: - case PPL_FAILED_REQ_COUNT_CUS: - case PPL_FAILED_REQ_COUNT_SYS: + + case REPORT_DEFINITION_CREATE_INTERVAL_COUNT: + case REPORT_DEFINITION_CREATE_USER_ERROR: + case REPORT_DEFINITION_CREATE_SYSTEM_ERROR: + + case REPORT_DEFINITION_UPDATE_INTERVAL_COUNT: + case REPORT_DEFINITION_UPDATE_USER_ERROR: + case REPORT_DEFINITION_UPDATE_SYSTEM_ERROR: + + case REPORT_DEFINITION_INFO_INTERVAL_COUNT: + case REPORT_DEFINITION_INFO_USER_ERROR: + case REPORT_DEFINITION_INFO_SYSTEM_ERROR: + + case REPORT_DEFINITION_DELETE_INTERVAL_COUNT: + case REPORT_DEFINITION_DELETE_USER_ERROR: + case REPORT_DEFINITION_DELETE_SYSTEM_ERROR: + + case REPORT_DEFINITION_LIST_INTERVAL_COUNT: + case REPORT_DEFINITION_LIST_USER_ERROR: + case REPORT_DEFINITION_LIST_SYSTEM_ERROR: + + case REPORT_INSTANCE_UPDATE_INTERVAL_COUNT: + case REPORT_INSTANCE_UPDATE_USER_ERROR: + case REPORT_INSTANCE_UPDATE_SYSTEM_ERROR: + + case REPORT_INSTANCE_INFO_COUNT: + case REPORT_INSTANCE_INFO_USER_ERROR: + case REPORT_INSTANCE_INFO_SYSTEM_ERROR: + + case REPORT_INSTANCE_LIST_INTERVAL_COUNT: + case REPORT_INSTANCE_LIST_USER_ERROR: + case REPORT_INSTANCE_LIST_SYSTEM_ERROR: + + case REPORT_FROM_DEFINITION_INTERVAL_COUNT: + case REPORT_FROM_DEFINITION_USER_ERROR: + case REPORT_FROM_DEFINITION_SYSTEM_ERROR: + + case REPORT_FROM_DEFINITION_ID_INTERVAL_COUNT: + case REPORT_FROM_DEFINITION_ID_USER_ERROR: + case REPORT_FROM_DEFINITION_ID_SYSTEM_ERROR: return new NumericMetric<>(name.getName(), new RollingCounter()); default: return new NumericMetric<>(name.getName(), new BasicCounter()); diff --git a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricName.java b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricName.java index e15c8365..cb15724e 100644 --- a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricName.java +++ b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricName.java @@ -15,77 +15,97 @@ package com.amazon.opendistroforelasticsearch.reportsscheduler.metrics; -//enum class MetricName(val metricName: String, val value: Int, val numerical: Boolean) { -// -// REPORT_DEFINITION_CREATE_COUNT("report_definition_create_count", COUNT, true), -// REPORT_DEFINITION_CREATE_USER_ERROR("report_definition_create_user_error", COUNT, true), -// REPORT_DEFINITION_CREATE_SYSTEM_ERROR("report_definition_create_system_error", COUNT, true), -// REPORT_DEFINITION_UPDATE_COUNT("report_definition_update_count", COUNT, true), -// REPORT_DEFINITION_UPDATE_USER_ERROR("report_definition_update_user_error", COUNT, true), -// REPORT_DEFINITION_UPDATE_SYSTEM_ERROR("report_definition_update_system_error", COUNT, true), -// REPORT_DEFINITION_DELETE_COUNT("report_definition_delete_count", COUNT, true), -// REPORT_DEFINITION_DELETE_USER_ERROR("report_definition_delete_user_error", COUNT, true), -// REPORT_DEFINITION_DELETE_SYSTEM_ERROR("report_definition_delete_system_error", COUNT, true), -// REPORT_DEFINITION_LIST_COUNT("report_definition_list_count", COUNT, true), -// REPORT_DEFINITION_LIST_USER_ERROR("report_definition_list_user_error", COUNT, true), -// REPORT_DEFINITION_LIST_SYSTEM_ERROR("report_definition_list_system_error", COUNT, true), -// -// REPORT_INSTANCE_LIST_COUNT("report_instance_list_count", COUNT, true), -// REPORT_INSTANCE_LIST_USER_ERROR("report_instance_list_user_error", COUNT, true), -// REPORT_INSTANCE_LIST_SYSTEM_ERROR("report_instance_list_system_error", COUNT, true) -//} - - -import com.google.common.collect.ImmutableSet; - import java.util.Arrays; import java.util.List; -import java.util.Set; import java.util.stream.Collectors; public enum MetricName { - REQ_TOTAL("request_total"), - REQ_COUNT_TOTAL("request_count"), - FAILED_REQ_COUNT_SYS("failed_request_count_syserr"), - FAILED_REQ_COUNT_CUS("failed_request_count_cuserr"), - FAILED_REQ_COUNT_CB("failed_request_count_cb"), - DEFAULT_CURSOR_REQUEST_TOTAL("default_cursor_request_total"), - DEFAULT_CURSOR_REQUEST_COUNT_TOTAL("default_cursor_request_count"), - DEFAULT("default"), - - PPL_REQ_TOTAL("ppl_request_total"), - PPL_REQ_COUNT_TOTAL("ppl_request_count"), - PPL_FAILED_REQ_COUNT_SYS("ppl_failed_request_count_syserr"), - PPL_FAILED_REQ_COUNT_CUS("ppl_failed_request_count_cuserr"); + + REPORT_DEFINITION_CREATE_TOTAL("report_definition.create.total", true), + REPORT_DEFINITION_CREATE_INTERVAL_COUNT("report_definition.create.count", true), + REPORT_DEFINITION_CREATE_USER_ERROR("report_definition.create.user_error", true), + REPORT_DEFINITION_CREATE_SYSTEM_ERROR("report_definition.create.system_error.", true), + + REPORT_DEFINITION_UPDATE_TOTAL("report_definition.update.total", true), + REPORT_DEFINITION_UPDATE_INTERVAL_COUNT("report_definition.update.count", true), + REPORT_DEFINITION_UPDATE_USER_ERROR("report_definition.update.user_error", true), + REPORT_DEFINITION_UPDATE_SYSTEM_ERROR("report_definition.update.system_error.", true), + + REPORT_DEFINITION_INFO_TOTAL("report_definition.info.total", true), + REPORT_DEFINITION_INFO_INTERVAL_COUNT("report_definition.info.count", true), + REPORT_DEFINITION_INFO_USER_ERROR("report_definition.info.user_error", true), + REPORT_DEFINITION_INFO_SYSTEM_ERROR("report_definition.info.system_error.", true), + + REPORT_DEFINITION_DELETE_TOTAL("report_definition.delete.total", true), + REPORT_DEFINITION_DELETE_INTERVAL_COUNT("report_definition.delete.count", true), + REPORT_DEFINITION_DELETE_USER_ERROR("report_definition.delete.user_error", true), + REPORT_DEFINITION_DELETE_SYSTEM_ERROR("report_definition.delete.system_error.", true), + + REPORT_DEFINITION_LIST_TOTAL("report_definition.list.total", true), + REPORT_DEFINITION_LIST_INTERVAL_COUNT("report_definition.list.count", true), + REPORT_DEFINITION_LIST_USER_ERROR("report_definition.list.user_error", true), + REPORT_DEFINITION_LIST_SYSTEM_ERROR("report_definition.list.system_error.", true), + + REPORT_INSTANCE_UPDATE_TOTAL("report_instance.update.total", true), + REPORT_INSTANCE_UPDATE_INTERVAL_COUNT("report_instance.update.count", true), + REPORT_INSTANCE_UPDATE_USER_ERROR("report_instance.update.user_error", true), + REPORT_INSTANCE_UPDATE_SYSTEM_ERROR("report_instance.update.system_error.", true), + + REPORT_INSTANCE_INFO_TOTAL("report_instance.info.total", true), + REPORT_INSTANCE_INFO_COUNT("report_instance.info.count", true), + REPORT_INSTANCE_INFO_USER_ERROR("report_instance.info.user_error", true), + REPORT_INSTANCE_INFO_SYSTEM_ERROR("report_instance.info.system_error.", true), + + REPORT_INSTANCE_LIST_TOTAL("report_instance.list.total", true), + REPORT_INSTANCE_LIST_INTERVAL_COUNT("report_instance.list.count", true), + REPORT_INSTANCE_LIST_USER_ERROR("report_instance.list.user_error", true), + REPORT_INSTANCE_LIST_SYSTEM_ERROR("report_instance.list.system_error.", true), + + REPORT_FROM_DEFINITION_TOTAL("on_demand.create.total", true), + REPORT_FROM_DEFINITION_INTERVAL_COUNT("on_demand.create.count", true), + REPORT_FROM_DEFINITION_USER_ERROR("on_demand.create.user_error", true), + REPORT_FROM_DEFINITION_SYSTEM_ERROR("on_demand.create.system_error.", true), + + REPORT_FROM_DEFINITION_ID_TOTAL("on_demand_from_definition.create.total", true), + REPORT_FROM_DEFINITION_ID_INTERVAL_COUNT("on_demand_from_definition.create.count", true), + REPORT_FROM_DEFINITION_ID_USER_ERROR("on_demand_from_definition.create.user_error", true), + REPORT_FROM_DEFINITION_ID_SYSTEM_ERROR("on_demand_from_definition.create.system_error.", true), + + DEFAULT("default", true); private String name; + private boolean numerical; - MetricName(String name) { + MetricName(String name, boolean numerical) { this.name = name; + this.numerical = numerical; } public String getName() { return name; } + public boolean isNumerical() { + return numerical; + } + public static List getNames() { return Arrays.stream(MetricName.values()).map(v -> v.name).collect(Collectors.toList()); } - - private static Set NUMERICAL_METRIC = new ImmutableSet.Builder() - .add(PPL_REQ_TOTAL) - .add(PPL_REQ_COUNT_TOTAL) - .add(PPL_FAILED_REQ_COUNT_SYS) - .add(PPL_FAILED_REQ_COUNT_CUS) - .build(); - - public boolean isNumerical() { - return this == REQ_TOTAL || this == REQ_COUNT_TOTAL || this == FAILED_REQ_COUNT_SYS - || this == FAILED_REQ_COUNT_CUS || this == FAILED_REQ_COUNT_CB || this == DEFAULT - || this == DEFAULT_CURSOR_REQUEST_TOTAL || this == DEFAULT_CURSOR_REQUEST_COUNT_TOTAL - || NUMERICAL_METRIC.contains(this); - } +// private static Set NUMERICAL_METRIC = new ImmutableSet.Builder() +// .add(PPL_REQ_TOTAL) +// .add(PPL_REQ_COUNT_TOTAL) +// .add(PPL_FAILED_REQ_COUNT_SYS) +// .add(PPL_FAILED_REQ_COUNT_CUS) +// .build(); + +// public boolean isNumerical() { +// return this == REQ_TOTAL || this == REQ_COUNT_TOTAL || this == FAILED_REQ_COUNT_SYS +// || this == FAILED_REQ_COUNT_CUS || this == FAILED_REQ_COUNT_CB || this == DEFAULT +// || this == DEFAULT_CURSOR_REQUEST_TOTAL || this == DEFAULT_CURSOR_REQUEST_COUNT_TOTAL +// || NUMERICAL_METRIC.contains(this); +// } } From 75a92fc237f4b15b07497d41bbaf55e2f4dd7b4b Mon Sep 17 00:00:00 2001 From: Abbas Hussain Date: Mon, 28 Dec 2020 14:50:31 -0800 Subject: [PATCH 09/25] Add new test endpoint and url params --- .../reportsscheduler/metrics/Metrics.java | 30 ++++++++++++++++++- .../reportsscheduler/model/RestTag.kt | 2 ++ .../resthandler/ReportStatsRestHandler.kt | 18 ++++++++--- 3 files changed, 45 insertions(+), 5 deletions(-) diff --git a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Metrics.java b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Metrics.java index 26809ef2..5a085275 100644 --- a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Metrics.java +++ b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Metrics.java @@ -15,11 +15,18 @@ package com.amazon.opendistroforelasticsearch.reportsscheduler.metrics; +import org.elasticsearch.rest.RestRequest; import org.json.JSONObject; +import java.time.Clock; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.concurrent.ConcurrentHashMap; +import static com.amazon.opendistroforelasticsearch.reportsscheduler.model.RestTag.STATS_START_TIME; +import static com.amazon.opendistroforelasticsearch.reportsscheduler.model.RestTag.STATS_END_TIME; + // import com.github.wnameless.json.flattener.JsonFlattener; import com.github.wnameless.json.unflattener.JsonUnflattener; @@ -27,6 +34,7 @@ public class Metrics { private static Metrics metrics = new Metrics(); private ConcurrentHashMap> registeredMetricsByName = new ConcurrentHashMap<>(); + private final Clock clock = Clock.systemDefaultZone(); private Metrics() { } @@ -84,6 +92,8 @@ public String collectToJSON() { metricsJSONObject.put(metric.getName(), metric.getValue()); } + metricsJSONObject.put("time", clock.millis()); + return metricsJSONObject.toString(); } @@ -99,10 +109,28 @@ public String collectToFlattenedJSON() { "{\"count\":2,\"total\":4,\"system_error\":2,\"customer_error\":3}}," + "\"report_instance\":{\"create\":{\"count\":2,\"total\":4,\"system_error\":2," + "\"customer_error\":3}}}"; - String metricsJson = JsonUnflattener.unflatten(flattenedJson); + String metricsJson = JsonUnflattener.unflatten(collectToJSON()); return metricsJson; } + + public String requestString(RestRequest request) { + Map map = new HashMap<>(); + map.put("path" , request.path()); + map.put("rawPath", request.rawPath()); + map.put("uri" , request.uri()); + return map.toString(); + } + + public String test(RestRequest request) { + Map map = new HashMap<>(); + map.put("startTime" , request.param(STATS_START_TIME, "0000-00-00T00:00:00")); + map.put("endTime" , request.param(STATS_END_TIME, "0000-00-00T00:00:00")); + return map.toString(); + } + + + public void clear() { registeredMetricsByName.clear(); } diff --git a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/RestTag.kt b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/RestTag.kt index 541a29db..85352dfc 100644 --- a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/RestTag.kt +++ b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/RestTag.kt @@ -38,6 +38,8 @@ internal object RestTag { const val END_TIME_FIELD = "endTimeMs" const val REPORT_DEFINITION_FIELD = "reportDefinition" const val REPORT_DEFINITION_ID_FIELD = "reportDefinitionId" + const val STATS_START_TIME = "startTime" + const val STATS_END_TIME = "endTime" const val REPORT_DEFINITION_DETAILS_FIELD = "reportDefinitionDetails" const val FROM_INDEX_FIELD = "fromIndex" const val MAX_ITEMS_FIELD = "maxItems" diff --git a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportStatsRestHandler.kt b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportStatsRestHandler.kt index 0279f277..8490bdcc 100644 --- a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportStatsRestHandler.kt +++ b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportStatsRestHandler.kt @@ -25,6 +25,8 @@ import com.amazon.opendistroforelasticsearch.reportsscheduler.action.ReportDefin // import com.amazon.opendistroforelasticsearch.reportsscheduler.model.DeleteReportDefinitionRequest // import com.amazon.opendistroforelasticsearch.reportsscheduler.model.GetReportDefinitionRequest // import com.amazon.opendistroforelasticsearch.reportsscheduler.model.RestTag.REPORT_DEFINITION_ID_FIELD +import com.amazon.opendistroforelasticsearch.reportsscheduler.model.RestTag.STATS_START_TIME +import com.amazon.opendistroforelasticsearch.reportsscheduler.model.RestTag.STATS_END_TIME // import com.amazon.opendistroforelasticsearch.reportsscheduler.model.UpdateReportDefinitionRequest // import com.amazon.opendistroforelasticsearch.reportsscheduler.util.contentParserNextToken import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.Metrics @@ -45,6 +47,7 @@ internal class ReportStatsRestHandler : BaseRestHandler() { companion object { private const val REPORT_STATS_ACTION = "report_definition_stats" private const val REPORT_STATS_URL = "$BASE_REPORTS_URI/stats" + private const val REPORT_TEST_URL = "$BASE_REPORTS_URI/test" public const val COUNT = 4 } @@ -66,7 +69,8 @@ internal class ReportStatsRestHandler : BaseRestHandler() { * Request body: Ref [com.amazon.opendistroforelasticsearch.reportsscheduler.model.GetReportDefinitionRequest] * Response body: Ref [com.amazon.opendistroforelasticsearch.reportsscheduler.model.GetReportDefinitionResponse] */ - Route(GET, "$REPORT_STATS_URL") + Route(GET, "$REPORT_STATS_URL"), + Route(GET, "$REPORT_TEST_URL") ) } @@ -74,7 +78,7 @@ internal class ReportStatsRestHandler : BaseRestHandler() { * {@inheritDoc} */ override fun responseParams(): Set { - return setOf() + return setOf(STATS_START_TIME, STATS_END_TIME) } /** @@ -83,8 +87,14 @@ internal class ReportStatsRestHandler : BaseRestHandler() { override fun prepareRequest(request: RestRequest, client: NodeClient): RestChannelConsumer { return when (request.method()) { GET -> RestChannelConsumer { -// it.sendResponse(BytesRestResponse(RestStatus.OK, Metrics.getInstance().collectToJSON())) - it.sendResponse(BytesRestResponse(RestStatus.OK, Metrics.getInstance().collectToFlattenedJSON())) + when (request.path()) { + REPORT_STATS_URL -> { + it.sendResponse(BytesRestResponse(RestStatus.OK, Metrics.getInstance().collectToFlattenedJSON())) + } + REPORT_TEST_URL -> { + it.sendResponse(BytesRestResponse(RestStatus.OK, Metrics.getInstance().test(request))) + } + } } else -> RestChannelConsumer { it.sendResponse(BytesRestResponse(RestStatus.METHOD_NOT_ALLOWED, "${request.method()} is not allowed")) From 7b8e2e0f37cc093b1d64a12cef173f50a23f60ef Mon Sep 17 00:00:00 2001 From: Abbas Hussain Date: Mon, 28 Dec 2020 15:49:14 -0800 Subject: [PATCH 10/25] Place metric counts for some report defintion APIs --- .../reportsscheduler/index/ReportDefinitionsIndex.kt | 8 ++++++++ .../model/CreateReportDefinitionRequest.kt | 9 +++++++-- .../model/CreateReportDefinitionResponse.kt | 7 ++++++- .../model/DeleteReportDefinitionRequest.kt | 7 ++++++- .../model/DeleteReportDefinitionResponse.kt | 7 ++++++- .../model/GetReportDefinitionRequest.kt | 7 ++++++- .../model/GetReportDefinitionResponse.kt | 7 ++++++- .../model/UpdateReportDefinitionRequest.kt | 12 ++++++++++-- .../model/UpdateReportDefinitionResponse.kt | 7 ++++++- .../resthandler/ReportDefinitionRestHandler.kt | 10 ++++++++++ 10 files changed, 71 insertions(+), 10 deletions(-) diff --git a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/index/ReportDefinitionsIndex.kt b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/index/ReportDefinitionsIndex.kt index b8fbe88f..66c48368 100644 --- a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/index/ReportDefinitionsIndex.kt +++ b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/index/ReportDefinitionsIndex.kt @@ -17,6 +17,8 @@ package com.amazon.opendistroforelasticsearch.reportsscheduler.index import com.amazon.opendistroforelasticsearch.reportsscheduler.ReportsSchedulerPlugin.Companion.LOG_PREFIX +import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.MetricName +import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.Metrics import com.amazon.opendistroforelasticsearch.reportsscheduler.model.ReportDefinitionDetails import com.amazon.opendistroforelasticsearch.reportsscheduler.model.ReportDefinitionDetailsSearchResults import com.amazon.opendistroforelasticsearch.reportsscheduler.model.RestTag.ACCESS_LIST_FIELD @@ -84,10 +86,12 @@ internal object ReportDefinitionsIndex { if (response.isAcknowledged) { log.info("$LOG_PREFIX:Index $REPORT_DEFINITIONS_INDEX_NAME creation Acknowledged") } else { + Metrics.getInstance().getNumericalMetric(MetricName.REPORT_DEFINITION_CREATE_SYSTEM_ERROR).increment() throw IllegalStateException("$LOG_PREFIX:Index $REPORT_DEFINITIONS_INDEX_NAME creation not Acknowledged") } } catch (exception: Exception) { if (exception !is ResourceAlreadyExistsException && exception.cause !is ResourceAlreadyExistsException) { + Metrics.getInstance().getNumericalMetric(MetricName.REPORT_DEFINITION_CREATE_SYSTEM_ERROR).increment() throw exception } } @@ -117,6 +121,7 @@ internal object ReportDefinitionsIndex { val actionFuture = client.index(indexRequest) val response = actionFuture.actionGet(PluginSettings.operationTimeoutMs) return if (response.result != DocWriteResponse.Result.CREATED) { + Metrics.getInstance().getNumericalMetric(MetricName.REPORT_DEFINITION_CREATE_SYSTEM_ERROR) log.warn("$LOG_PREFIX:createReportDefinition - response:$response") null } else { @@ -136,6 +141,7 @@ internal object ReportDefinitionsIndex { val response = actionFuture.actionGet(PluginSettings.operationTimeoutMs) return if (response.sourceAsString == null) { log.warn("$LOG_PREFIX:getReportDefinition - $id not found; response:$response") + Metrics.getInstance().getNumericalMetric(MetricName.REPORT_DEFINITION_INFO_SYSTEM_ERROR) null } else { val parser = XContentType.JSON.xContent().createParser(NamedXContentRegistry.EMPTY, @@ -198,6 +204,7 @@ internal object ReportDefinitionsIndex { val actionFuture = client.update(updateRequest) val response = actionFuture.actionGet(PluginSettings.operationTimeoutMs) if (response.result != DocWriteResponse.Result.UPDATED) { + Metrics.getInstance().getNumericalMetric(MetricName.REPORT_DEFINITION_UPDATE_SYSTEM_ERROR) log.warn("$LOG_PREFIX:updateReportDefinition failed for $id; response:$response") } return response.result == DocWriteResponse.Result.UPDATED @@ -216,6 +223,7 @@ internal object ReportDefinitionsIndex { val actionFuture = client.delete(deleteRequest) val response = actionFuture.actionGet(PluginSettings.operationTimeoutMs) if (response.result != DocWriteResponse.Result.DELETED) { + Metrics.getInstance().getNumericalMetric(MetricName.REPORT_DEFINITION_DELETE_SYSTEM_ERROR) log.warn("$LOG_PREFIX:deleteReportDefinition failed for $id; response:$response") } return response.result == DocWriteResponse.Result.DELETED diff --git a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/CreateReportDefinitionRequest.kt b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/CreateReportDefinitionRequest.kt index 227f9664..1a4c2f21 100644 --- a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/CreateReportDefinitionRequest.kt +++ b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/CreateReportDefinitionRequest.kt @@ -17,6 +17,8 @@ package com.amazon.opendistroforelasticsearch.reportsscheduler.model import com.amazon.opendistroforelasticsearch.reportsscheduler.ReportsSchedulerPlugin.Companion.LOG_PREFIX +import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.MetricName +import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.Metrics import com.amazon.opendistroforelasticsearch.reportsscheduler.model.RestTag.REPORT_DEFINITION_FIELD import com.amazon.opendistroforelasticsearch.reportsscheduler.util.createJsonParser import com.amazon.opendistroforelasticsearch.reportsscheduler.util.logger @@ -59,7 +61,7 @@ internal class CreateReportDefinitionRequest : ActionRequest, ToXContentObject { constructor(input: StreamInput) : this(input.createJsonParser()) /** - * Parse the data from parser and create [GetAllReportDefinitionsResponse] object + * Parse the data from parser and create [CreateReportDefinitionRequest] object * @param parser data referenced at parser */ constructor(parser: XContentParser) : super() { @@ -76,7 +78,10 @@ internal class CreateReportDefinitionRequest : ActionRequest, ToXContentObject { } } } - reportDefinition ?: throw IllegalArgumentException("$REPORT_DEFINITION_FIELD field absent") + reportDefinition ?: run { + Metrics.getInstance().getNumericalMetric(MetricName.REPORT_DEFINITION_CREATE_USER_ERROR).increment() + throw IllegalArgumentException("$REPORT_DEFINITION_FIELD field absent") + } this.reportDefinition = reportDefinition } diff --git a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/CreateReportDefinitionResponse.kt b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/CreateReportDefinitionResponse.kt index 16a6f0d8..db984ae9 100644 --- a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/CreateReportDefinitionResponse.kt +++ b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/CreateReportDefinitionResponse.kt @@ -17,6 +17,8 @@ package com.amazon.opendistroforelasticsearch.reportsscheduler.model import com.amazon.opendistroforelasticsearch.reportsscheduler.ReportsSchedulerPlugin.Companion.LOG_PREFIX +import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.MetricName +import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.Metrics import com.amazon.opendistroforelasticsearch.reportsscheduler.model.RestTag.REPORT_DEFINITION_ID_FIELD import com.amazon.opendistroforelasticsearch.reportsscheduler.util.logger import org.elasticsearch.common.io.stream.StreamInput @@ -68,7 +70,10 @@ internal class CreateReportDefinitionResponse( } } } - reportDefinitionId ?: throw IllegalArgumentException("$REPORT_DEFINITION_ID_FIELD field absent") + reportDefinitionId ?: run { + Metrics.getInstance().getNumericalMetric(MetricName.REPORT_DEFINITION_CREATE_SYSTEM_ERROR).increment() + throw IllegalArgumentException("$REPORT_DEFINITION_ID_FIELD field absent") + } return CreateReportDefinitionResponse(reportDefinitionId) } } diff --git a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/DeleteReportDefinitionRequest.kt b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/DeleteReportDefinitionRequest.kt index 4e8514f6..59d50a55 100644 --- a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/DeleteReportDefinitionRequest.kt +++ b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/DeleteReportDefinitionRequest.kt @@ -17,6 +17,8 @@ package com.amazon.opendistroforelasticsearch.reportsscheduler.model import com.amazon.opendistroforelasticsearch.reportsscheduler.ReportsSchedulerPlugin.Companion.LOG_PREFIX +import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.MetricName +import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.Metrics import com.amazon.opendistroforelasticsearch.reportsscheduler.model.RestTag.REPORT_DEFINITION_ID_FIELD import com.amazon.opendistroforelasticsearch.reportsscheduler.util.logger import org.elasticsearch.action.ActionRequest @@ -74,7 +76,10 @@ internal class DeleteReportDefinitionRequest( } } } - reportDefinitionId ?: throw IllegalArgumentException("$REPORT_DEFINITION_ID_FIELD field absent") + reportDefinitionId ?: run { + Metrics.getInstance().getNumericalMetric(MetricName.REPORT_DEFINITION_DELETE_USER_ERROR).increment() + throw IllegalArgumentException("$REPORT_DEFINITION_ID_FIELD field absent") + } return DeleteReportDefinitionRequest(reportDefinitionId) } } diff --git a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/DeleteReportDefinitionResponse.kt b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/DeleteReportDefinitionResponse.kt index 8972119d..f90be021 100644 --- a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/DeleteReportDefinitionResponse.kt +++ b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/DeleteReportDefinitionResponse.kt @@ -17,6 +17,8 @@ package com.amazon.opendistroforelasticsearch.reportsscheduler.model import com.amazon.opendistroforelasticsearch.reportsscheduler.ReportsSchedulerPlugin.Companion.LOG_PREFIX +import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.MetricName +import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.Metrics import com.amazon.opendistroforelasticsearch.reportsscheduler.model.RestTag.REPORT_DEFINITION_ID_FIELD import com.amazon.opendistroforelasticsearch.reportsscheduler.util.logger import org.elasticsearch.common.io.stream.StreamInput @@ -68,7 +70,10 @@ internal data class DeleteReportDefinitionResponse( } } } - reportDefinitionId ?: throw IllegalArgumentException("$REPORT_DEFINITION_ID_FIELD field absent") + reportDefinitionId ?: run { + Metrics.getInstance().getNumericalMetric(MetricName.REPORT_DEFINITION_DELETE_SYSTEM_ERROR).increment() + throw IllegalArgumentException("$REPORT_DEFINITION_ID_FIELD field absent") + } return DeleteReportDefinitionResponse(reportDefinitionId) } } diff --git a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/GetReportDefinitionRequest.kt b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/GetReportDefinitionRequest.kt index 154f2f14..f269f2ab 100644 --- a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/GetReportDefinitionRequest.kt +++ b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/GetReportDefinitionRequest.kt @@ -17,6 +17,8 @@ package com.amazon.opendistroforelasticsearch.reportsscheduler.model import com.amazon.opendistroforelasticsearch.reportsscheduler.ReportsSchedulerPlugin.Companion.LOG_PREFIX +import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.MetricName +import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.Metrics import com.amazon.opendistroforelasticsearch.reportsscheduler.model.RestTag.REPORT_DEFINITION_ID_FIELD import com.amazon.opendistroforelasticsearch.reportsscheduler.util.logger import org.elasticsearch.action.ActionRequest @@ -74,7 +76,10 @@ internal class GetReportDefinitionRequest( } } } - reportDefinitionId ?: throw IllegalArgumentException("$REPORT_DEFINITION_ID_FIELD field absent") + reportDefinitionId ?: run { + Metrics.getInstance().getNumericalMetric(MetricName.REPORT_DEFINITION_INFO_USER_ERROR).increment() + throw IllegalArgumentException("$REPORT_DEFINITION_ID_FIELD field absent") + } return GetReportDefinitionRequest(reportDefinitionId) } } diff --git a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/GetReportDefinitionResponse.kt b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/GetReportDefinitionResponse.kt index 41ed43f5..2d4edeac 100644 --- a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/GetReportDefinitionResponse.kt +++ b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/GetReportDefinitionResponse.kt @@ -17,6 +17,8 @@ package com.amazon.opendistroforelasticsearch.reportsscheduler.model import com.amazon.opendistroforelasticsearch.reportsscheduler.ReportsSchedulerPlugin.Companion.LOG_PREFIX +import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.MetricName +import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.Metrics import com.amazon.opendistroforelasticsearch.reportsscheduler.util.createJsonParser import com.amazon.opendistroforelasticsearch.reportsscheduler.util.logger import org.elasticsearch.common.io.stream.StreamInput @@ -74,7 +76,10 @@ internal class GetReportDefinitionResponse : BaseResponse { } } } - reportDefinition ?: throw IllegalArgumentException("${RestTag.REPORT_DEFINITION_FIELD} field absent") + reportDefinition ?: run { + Metrics.getInstance().getNumericalMetric(MetricName.REPORT_DEFINITION_INFO_SYSTEM_ERROR).increment() + throw IllegalArgumentException("${RestTag.REPORT_DEFINITION_FIELD} field absent") + } this.reportDefinitionDetails = reportDefinition this.filterSensitiveInfo = false // Sensitive info Must have filtered when created json object } diff --git a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/UpdateReportDefinitionRequest.kt b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/UpdateReportDefinitionRequest.kt index a98c586b..28e2619a 100644 --- a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/UpdateReportDefinitionRequest.kt +++ b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/UpdateReportDefinitionRequest.kt @@ -17,6 +17,8 @@ package com.amazon.opendistroforelasticsearch.reportsscheduler.model import com.amazon.opendistroforelasticsearch.reportsscheduler.ReportsSchedulerPlugin.Companion.LOG_PREFIX +import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.MetricName +import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.Metrics import com.amazon.opendistroforelasticsearch.reportsscheduler.model.RestTag.REPORT_DEFINITION_FIELD import com.amazon.opendistroforelasticsearch.reportsscheduler.model.RestTag.REPORT_DEFINITION_ID_FIELD import com.amazon.opendistroforelasticsearch.reportsscheduler.util.createJsonParser @@ -83,8 +85,14 @@ internal class UpdateReportDefinitionRequest : ActionRequest, ToXContentObject { } } } - reportDefinitionId ?: throw IllegalArgumentException("$REPORT_DEFINITION_ID_FIELD field absent") - reportDefinition ?: throw IllegalArgumentException("$REPORT_DEFINITION_FIELD field absent") + reportDefinitionId ?: run { + Metrics.getInstance().getNumericalMetric(MetricName.REPORT_DEFINITION_UPDATE_USER_ERROR).increment() + throw IllegalArgumentException("$REPORT_DEFINITION_ID_FIELD field absent") + } + reportDefinition ?: run { + Metrics.getInstance().getNumericalMetric(MetricName.REPORT_DEFINITION_UPDATE_USER_ERROR).increment() + throw IllegalArgumentException("$REPORT_DEFINITION_FIELD field absent") + } this.reportDefinitionId = reportDefinitionId this.reportDefinition = reportDefinition } diff --git a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/UpdateReportDefinitionResponse.kt b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/UpdateReportDefinitionResponse.kt index 624382d6..68a6aa14 100644 --- a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/UpdateReportDefinitionResponse.kt +++ b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/UpdateReportDefinitionResponse.kt @@ -17,6 +17,8 @@ package com.amazon.opendistroforelasticsearch.reportsscheduler.model import com.amazon.opendistroforelasticsearch.reportsscheduler.ReportsSchedulerPlugin.Companion.LOG_PREFIX +import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.MetricName +import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.Metrics import com.amazon.opendistroforelasticsearch.reportsscheduler.model.RestTag.REPORT_DEFINITION_ID_FIELD import com.amazon.opendistroforelasticsearch.reportsscheduler.util.logger import org.elasticsearch.common.io.stream.StreamInput @@ -68,7 +70,10 @@ internal class UpdateReportDefinitionResponse( } } } - reportDefinitionId ?: throw IllegalArgumentException("$REPORT_DEFINITION_ID_FIELD field absent") + reportDefinitionId ?: run { + Metrics.getInstance().getNumericalMetric(MetricName.REPORT_DEFINITION_UPDATE_USER_ERROR).increment() + throw IllegalArgumentException("$REPORT_DEFINITION_ID_FIELD field absent") + } return UpdateReportDefinitionResponse(reportDefinitionId) } } diff --git a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportDefinitionRestHandler.kt b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportDefinitionRestHandler.kt index dce33d51..c43808ff 100644 --- a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportDefinitionRestHandler.kt +++ b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportDefinitionRestHandler.kt @@ -21,6 +21,8 @@ import com.amazon.opendistroforelasticsearch.reportsscheduler.action.DeleteRepor import com.amazon.opendistroforelasticsearch.reportsscheduler.action.GetReportDefinitionAction import com.amazon.opendistroforelasticsearch.reportsscheduler.action.ReportDefinitionActions import com.amazon.opendistroforelasticsearch.reportsscheduler.action.UpdateReportDefinitionAction +import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.Metrics +import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.MetricName import com.amazon.opendistroforelasticsearch.reportsscheduler.model.CreateReportDefinitionRequest import com.amazon.opendistroforelasticsearch.reportsscheduler.model.DeleteReportDefinitionRequest import com.amazon.opendistroforelasticsearch.reportsscheduler.model.GetReportDefinitionRequest @@ -105,22 +107,30 @@ internal class ReportDefinitionRestHandler : BaseRestHandler() { override fun prepareRequest(request: RestRequest, client: NodeClient): RestChannelConsumer { return when (request.method()) { POST -> RestChannelConsumer { + Metrics.getInstance().getNumericalMetric(MetricName.REPORT_DEFINITION_CREATE_TOTAL).increment() + Metrics.getInstance().getNumericalMetric(MetricName.REPORT_DEFINITION_CREATE_INTERVAL_COUNT).increment() client.execute(CreateReportDefinitionAction.ACTION_TYPE, CreateReportDefinitionRequest(request.contentParserNextToken()), RestResponseToXContentListener(it)) } PUT -> RestChannelConsumer { + Metrics.getInstance().getNumericalMetric(MetricName.REPORT_DEFINITION_UPDATE_TOTAL).increment() + Metrics.getInstance().getNumericalMetric(MetricName.REPORT_DEFINITION_UPDATE_INTERVAL_COUNT).increment() client.execute( UpdateReportDefinitionAction.ACTION_TYPE, UpdateReportDefinitionRequest(request.contentParserNextToken(), request.param(REPORT_DEFINITION_ID_FIELD)), RestResponseToXContentListener(it)) } GET -> RestChannelConsumer { + Metrics.getInstance().getNumericalMetric(MetricName.REPORT_DEFINITION_INFO_TOTAL).increment() + Metrics.getInstance().getNumericalMetric(MetricName.REPORT_DEFINITION_INFO_INTERVAL_COUNT).increment() client.execute(GetReportDefinitionAction.ACTION_TYPE, GetReportDefinitionRequest(request.param(REPORT_DEFINITION_ID_FIELD)), RestResponseToXContentListener(it)) } DELETE -> RestChannelConsumer { + Metrics.getInstance().getNumericalMetric(MetricName.REPORT_DEFINITION_DELETE_TOTAL).increment() + Metrics.getInstance().getNumericalMetric(MetricName.REPORT_DEFINITION_DELETE_INTERVAL_COUNT).increment() client.execute(DeleteReportDefinitionAction.ACTION_TYPE, DeleteReportDefinitionRequest(request.param(REPORT_DEFINITION_ID_FIELD)), RestResponseToXContentListener(it)) From c5b73199ca508be7a96713f7fde0cf01d1c42d53 Mon Sep 17 00:00:00 2001 From: Abbas Hussain Date: Tue, 29 Dec 2020 11:11:08 -0800 Subject: [PATCH 11/25] Add a metric and some tests --- .../reportsscheduler/metrics/MetricName.java | 2 + .../reportsscheduler/metrics/Metrics.java | 1 - .../metrics/BasicCounterTest.java | 44 +++++++++ .../reportsscheduler/metrics/MetricsTest.java | 78 ++++++++++++++++ .../metrics/NumericMetricTest.java | 43 +++++++++ .../metrics/RollingCounterTest.java | 89 +++++++++++++++++++ 6 files changed, 256 insertions(+), 1 deletion(-) create mode 100644 reports-scheduler/src/test/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/BasicCounterTest.java create mode 100644 reports-scheduler/src/test/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricsTest.java create mode 100644 reports-scheduler/src/test/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/NumericMetricTest.java create mode 100644 reports-scheduler/src/test/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/RollingCounterTest.java diff --git a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricName.java b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricName.java index cb15724e..54370a4d 100644 --- a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricName.java +++ b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricName.java @@ -71,6 +71,8 @@ public enum MetricName { REPORT_FROM_DEFINITION_ID_USER_ERROR("on_demand_from_definition.create.user_error", true), REPORT_FROM_DEFINITION_ID_SYSTEM_ERROR("on_demand_from_definition.create.system_error.", true), + REPORT_PERMISSION_ERROR("permission_error", true), + DEFAULT("default", true); private String name; diff --git a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Metrics.java b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Metrics.java index 5a085275..32659ead 100644 --- a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Metrics.java +++ b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Metrics.java @@ -27,7 +27,6 @@ import static com.amazon.opendistroforelasticsearch.reportsscheduler.model.RestTag.STATS_START_TIME; import static com.amazon.opendistroforelasticsearch.reportsscheduler.model.RestTag.STATS_END_TIME; -// import com.github.wnameless.json.flattener.JsonFlattener; import com.github.wnameless.json.unflattener.JsonUnflattener; public class Metrics { diff --git a/reports-scheduler/src/test/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/BasicCounterTest.java b/reports-scheduler/src/test/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/BasicCounterTest.java new file mode 100644 index 00000000..64a39ef7 --- /dev/null +++ b/reports-scheduler/src/test/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/BasicCounterTest.java @@ -0,0 +1,44 @@ +/* + * Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). + * You may not use this file except in compliance with the License. + * A copy of the License is located at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * or in the "license" file accompanying this file. This file 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.amazon.opendistroforelasticsearch.reportsscheduler.metrics; + +import org.junit.Test; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; + +public class BasicCounterTest { + + @Test + public void increment() { + BasicCounter counter = new BasicCounter(); + for (int i=0; i<5; ++i) { + counter.increment(); + } + + assertThat(counter.getValue(), equalTo(5L)); + } + + @Test + public void incrementN() { + BasicCounter counter = new BasicCounter(); + counter.add(5); + + assertThat(counter.getValue(), equalTo(5L)); + } + +} diff --git a/reports-scheduler/src/test/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricsTest.java b/reports-scheduler/src/test/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricsTest.java new file mode 100644 index 00000000..58888a35 --- /dev/null +++ b/reports-scheduler/src/test/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricsTest.java @@ -0,0 +1,78 @@ +/* + * Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). + * You may not use this file except in compliance with the License. + * A copy of the License is located at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * or in the "license" file accompanying this file. This file 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.amazon.opendistroforelasticsearch.reportsscheduler.metrics; + +import org.json.JSONObject; +import org.junit.Test; + +import java.util.List; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.notNullValue; +public class MetricsTest { + @Test + public void registerMetric() { + Metrics.getInstance().clear(); + Metrics.getInstance().registerMetric(new NumericMetric("test", new BasicCounter())); + + assertThat(Metrics.getInstance().getAllMetrics().size(), equalTo(1)); + } + + @Test + public void unRegisterMetric() { + Metrics.getInstance().clear(); + Metrics.getInstance().registerMetric(new NumericMetric("test1", new BasicCounter())); + Metrics.getInstance().registerMetric(new NumericMetric("test2", new BasicCounter())); + assertThat(Metrics.getInstance().getAllMetrics().size(), equalTo(2)); + + Metrics.getInstance().unregisterMetric("test2"); + assertThat(Metrics.getInstance().getAllMetrics().size(), equalTo(1)); + } + + @Test + public void getMetric() { + Metrics.getInstance().clear(); + Metrics.getInstance().registerMetric(new NumericMetric("test1", new BasicCounter())); + Metric metric = Metrics.getInstance().getMetric("test1"); + + assertThat(metric, notNullValue()); + } + + + @Test + public void getAllMetric() { + Metrics.getInstance().clear(); + Metrics.getInstance().registerMetric(new NumericMetric("test1", new BasicCounter())); + Metrics.getInstance().registerMetric(new NumericMetric("test2", new BasicCounter())); + List> list = Metrics.getInstance().getAllMetrics(); + + assertThat(list.size(), equalTo(2)); + } + + @Test + public void collectToJSON() { + Metrics.getInstance().clear(); + Metrics.getInstance().registerMetric(new NumericMetric("test1", new BasicCounter())); + Metrics.getInstance().registerMetric(new NumericMetric("test2", new BasicCounter())); + String res = Metrics.getInstance().collectToJSON(); + JSONObject jsonObject = new JSONObject(res); + + assertThat(jsonObject.getLong("test1"), equalTo(0L)); + assertThat(jsonObject.getInt("test2"), equalTo(0)); + } +} diff --git a/reports-scheduler/src/test/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/NumericMetricTest.java b/reports-scheduler/src/test/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/NumericMetricTest.java new file mode 100644 index 00000000..bff37b5e --- /dev/null +++ b/reports-scheduler/src/test/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/NumericMetricTest.java @@ -0,0 +1,43 @@ +/* + * Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). + * You may not use this file except in compliance with the License. + * A copy of the License is located at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * or in the "license" file accompanying this file. This file 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.amazon.opendistroforelasticsearch.reportsscheduler.metrics; + +import org.junit.Test; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; + +public class NumericMetricTest { + + @Test + public void increment() { + NumericMetric metric = new NumericMetric<>("test", new BasicCounter()); + for (int i=0; i<5; ++i) { + metric.increment(); + } + + assertThat(metric.getValue(), equalTo(5L)); + } + + @Test + public void add() { + NumericMetric metric = new NumericMetric<>("test", new BasicCounter()); + metric.increment(5); + + assertThat(metric.getValue(), equalTo(5L)); + } +} diff --git a/reports-scheduler/src/test/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/RollingCounterTest.java b/reports-scheduler/src/test/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/RollingCounterTest.java new file mode 100644 index 00000000..4ed43fa8 --- /dev/null +++ b/reports-scheduler/src/test/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/RollingCounterTest.java @@ -0,0 +1,89 @@ +/* + * Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). + * You may not use this file except in compliance with the License. + * A copy of the License is located at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * or in the "license" file accompanying this file. This file 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.amazon.opendistroforelasticsearch.reportsscheduler.metrics; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; + +import java.time.Clock; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.lessThanOrEqualTo; +import static org.mockito.Mockito.when; + +@RunWith(MockitoJUnitRunner.class) +public class RollingCounterTest { + + @Mock + Clock clock; + + @Test + public void increment() { + RollingCounter counter = new RollingCounter(3, 1, clock); + for (int i=0; i<5; ++i) { + counter.increment(); + } + + assertThat(counter.getValue(), equalTo(0L)); + + when(clock.millis()).thenReturn(1000L); // 1 second passed + assertThat(counter.getValue(), equalTo(5L)); + + counter.increment(); + counter.increment(); + + when(clock.millis()).thenReturn(2000L); // 1 second passed + assertThat(counter.getValue(), lessThanOrEqualTo(3L)); + + when(clock.millis()).thenReturn(3000L); // 1 second passed + assertThat(counter.getValue(), equalTo(0L)); + + } + + @Test + public void add() { + RollingCounter counter = new RollingCounter(3, 1, clock); + + counter.add(6); + assertThat(counter.getValue(), equalTo(0L)); + + when(clock.millis()).thenReturn(1000L); // 1 second passed + assertThat(counter.getValue(), equalTo(6L)); + + counter.add(4); + when(clock.millis()).thenReturn(2000L); // 1 second passed + assertThat(counter.getValue(), equalTo(4L)); + + when(clock.millis()).thenReturn(3000L); // 1 second passed + assertThat(counter.getValue(), equalTo(0L)); + } + + @Test + public void trim() { + RollingCounter counter = new RollingCounter(2, 1, clock); + + for (int i=1; i<6; ++i) { + counter.increment(); + assertThat(counter.size(), equalTo(i)); + when(clock.millis()).thenReturn(i * 1000L); // i seconds passed + } + counter.increment(); + assertThat(counter.size(), lessThanOrEqualTo(3)); + } +} From 86117b67b5b02226c9c9993ba7753d72430690cd Mon Sep 17 00:00:00 2001 From: Abbas Hussain Date: Mon, 4 Jan 2021 12:44:36 -0800 Subject: [PATCH 12/25] Place more metrics --- .../reportsscheduler/metrics/MetricFactory.java | 4 +++- .../reportsscheduler/metrics/MetricName.java | 2 +- .../model/GetAllReportDefinitionsRequest.kt | 3 +++ .../model/GetAllReportInstancesRequest.kt | 3 +++ .../model/GetReportDefinitionRequest.kt | 2 +- .../model/GetReportInstanceRequest.kt | 8 +++++++- .../model/InContextReportCreateRequest.kt | 17 ++++++++++++++--- .../model/OnDemandReportCreateRequest.kt | 8 +++++++- .../model/UpdateReportInstanceStatusRequest.kt | 12 ++++++++++-- .../resthandler/OnDemandReportRestHandler.kt | 6 ++++++ .../ReportDefinitionListRestHandler.kt | 5 +++++ .../ReportInstanceListRestHandler.kt | 4 ++++ .../resthandler/ReportInstanceRestHandler.kt | 6 ++++++ 13 files changed, 70 insertions(+), 10 deletions(-) diff --git a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricFactory.java b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricFactory.java index 41104107..68dd14b3 100644 --- a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricFactory.java +++ b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricFactory.java @@ -56,7 +56,7 @@ public static Metric createMetric(MetricName name) { case REPORT_INSTANCE_UPDATE_USER_ERROR: case REPORT_INSTANCE_UPDATE_SYSTEM_ERROR: - case REPORT_INSTANCE_INFO_COUNT: + case REPORT_INSTANCE_INFO_INTERVAL_COUNT: case REPORT_INSTANCE_INFO_USER_ERROR: case REPORT_INSTANCE_INFO_SYSTEM_ERROR: @@ -71,6 +71,8 @@ public static Metric createMetric(MetricName name) { case REPORT_FROM_DEFINITION_ID_INTERVAL_COUNT: case REPORT_FROM_DEFINITION_ID_USER_ERROR: case REPORT_FROM_DEFINITION_ID_SYSTEM_ERROR: + + case REPORT_PERMISSION_ERROR: return new NumericMetric<>(name.getName(), new RollingCounter()); default: return new NumericMetric<>(name.getName(), new BasicCounter()); diff --git a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricName.java b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricName.java index 54370a4d..90d141a5 100644 --- a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricName.java +++ b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricName.java @@ -52,7 +52,7 @@ public enum MetricName { REPORT_INSTANCE_UPDATE_SYSTEM_ERROR("report_instance.update.system_error.", true), REPORT_INSTANCE_INFO_TOTAL("report_instance.info.total", true), - REPORT_INSTANCE_INFO_COUNT("report_instance.info.count", true), + REPORT_INSTANCE_INFO_INTERVAL_COUNT("report_instance.info.count", true), REPORT_INSTANCE_INFO_USER_ERROR("report_instance.info.user_error", true), REPORT_INSTANCE_INFO_SYSTEM_ERROR("report_instance.info.system_error.", true), diff --git a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/GetAllReportDefinitionsRequest.kt b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/GetAllReportDefinitionsRequest.kt index 2aa19e87..f39199ef 100644 --- a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/GetAllReportDefinitionsRequest.kt +++ b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/GetAllReportDefinitionsRequest.kt @@ -17,6 +17,8 @@ package com.amazon.opendistroforelasticsearch.reportsscheduler.model import com.amazon.opendistroforelasticsearch.reportsscheduler.ReportsSchedulerPlugin.Companion.LOG_PREFIX +import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.Metrics +import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.MetricName import com.amazon.opendistroforelasticsearch.reportsscheduler.model.RestTag.FROM_INDEX_FIELD import com.amazon.opendistroforelasticsearch.reportsscheduler.model.RestTag.MAX_ITEMS_FIELD import com.amazon.opendistroforelasticsearch.reportsscheduler.settings.PluginSettings @@ -100,6 +102,7 @@ internal class GetAllReportDefinitionsRequest( return if (fromIndex < 0) { val exception = ActionRequestValidationException() exception.addValidationError("fromIndex should be grater than 0") + Metrics.getInstance().getNumericalMetric(MetricName.REPORT_DEFINITION_LIST_USER_ERROR).increment() exception } else { null diff --git a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/GetAllReportInstancesRequest.kt b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/GetAllReportInstancesRequest.kt index 7098d25b..a9179312 100644 --- a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/GetAllReportInstancesRequest.kt +++ b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/GetAllReportInstancesRequest.kt @@ -17,6 +17,8 @@ package com.amazon.opendistroforelasticsearch.reportsscheduler.model import com.amazon.opendistroforelasticsearch.reportsscheduler.ReportsSchedulerPlugin.Companion.LOG_PREFIX +import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.Metrics +import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.MetricName import com.amazon.opendistroforelasticsearch.reportsscheduler.model.RestTag.FROM_INDEX_FIELD import com.amazon.opendistroforelasticsearch.reportsscheduler.model.RestTag.MAX_ITEMS_FIELD import com.amazon.opendistroforelasticsearch.reportsscheduler.settings.PluginSettings @@ -100,6 +102,7 @@ internal data class GetAllReportInstancesRequest( return if (fromIndex < 0) { val exception = ActionRequestValidationException() exception.addValidationError("fromIndex should be grater than 0") + Metrics.getInstance().getNumericalMetric(MetricName.REPORT_INSTANCE_LIST_USER_ERROR).increment() exception } else { null diff --git a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/GetReportDefinitionRequest.kt b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/GetReportDefinitionRequest.kt index f269f2ab..034fe0cd 100644 --- a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/GetReportDefinitionRequest.kt +++ b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/GetReportDefinitionRequest.kt @@ -17,8 +17,8 @@ package com.amazon.opendistroforelasticsearch.reportsscheduler.model import com.amazon.opendistroforelasticsearch.reportsscheduler.ReportsSchedulerPlugin.Companion.LOG_PREFIX -import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.MetricName import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.Metrics +import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.MetricName import com.amazon.opendistroforelasticsearch.reportsscheduler.model.RestTag.REPORT_DEFINITION_ID_FIELD import com.amazon.opendistroforelasticsearch.reportsscheduler.util.logger import org.elasticsearch.action.ActionRequest diff --git a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/GetReportInstanceRequest.kt b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/GetReportInstanceRequest.kt index a37faf40..0d08e45e 100644 --- a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/GetReportInstanceRequest.kt +++ b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/GetReportInstanceRequest.kt @@ -17,6 +17,8 @@ package com.amazon.opendistroforelasticsearch.reportsscheduler.model import com.amazon.opendistroforelasticsearch.reportsscheduler.ReportsSchedulerPlugin.Companion.LOG_PREFIX +import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.Metrics +import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.MetricName import com.amazon.opendistroforelasticsearch.reportsscheduler.model.RestTag.REPORT_INSTANCE_ID_FIELD import com.amazon.opendistroforelasticsearch.reportsscheduler.util.logger import org.elasticsearch.action.ActionRequest @@ -73,7 +75,11 @@ internal class GetReportInstanceRequest( } } } - reportInstanceId ?: throw IllegalArgumentException("$REPORT_INSTANCE_ID_FIELD field absent") + reportInstanceId ?: run { + Metrics.getInstance().getNumericalMetric(MetricName.REPORT_INSTANCE_INFO_USER_ERROR).increment() + throw IllegalArgumentException("$REPORT_INSTANCE_ID_FIELD field absent") + } + return GetReportInstanceRequest(reportInstanceId) } } diff --git a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/InContextReportCreateRequest.kt b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/InContextReportCreateRequest.kt index 20099af7..b4e3d779 100644 --- a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/InContextReportCreateRequest.kt +++ b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/InContextReportCreateRequest.kt @@ -17,6 +17,8 @@ package com.amazon.opendistroforelasticsearch.reportsscheduler.model import com.amazon.opendistroforelasticsearch.reportsscheduler.ReportsSchedulerPlugin.Companion.LOG_PREFIX +import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.Metrics +import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.MetricName import com.amazon.opendistroforelasticsearch.reportsscheduler.model.ReportInstance.Status import com.amazon.opendistroforelasticsearch.reportsscheduler.model.RestTag.BEGIN_TIME_FIELD import com.amazon.opendistroforelasticsearch.reportsscheduler.model.RestTag.END_TIME_FIELD @@ -117,9 +119,18 @@ internal class InContextReportCreateRequest : ActionRequest, ToXContentObject { } } } - beginTime ?: throw IllegalArgumentException("$BEGIN_TIME_FIELD field absent") - endTime ?: throw IllegalArgumentException("$END_TIME_FIELD field absent") - status ?: throw IllegalArgumentException("$STATUS_FIELD field absent") + beginTime ?: run { + Metrics.getInstance().getNumericalMetric(MetricName.REPORT_FROM_DEFINITION_USER_ERROR).increment() + throw IllegalArgumentException("$BEGIN_TIME_FIELD field absent") + } + endTime ?: run { + Metrics.getInstance().getNumericalMetric(MetricName.REPORT_FROM_DEFINITION_USER_ERROR).increment() + throw IllegalArgumentException("$END_TIME_FIELD field absent") + } + status ?: run { + Metrics.getInstance().getNumericalMetric(MetricName.REPORT_FROM_DEFINITION_USER_ERROR).increment() + throw IllegalArgumentException("$STATUS_FIELD field absent") + } this.beginTime = beginTime this.endTime = endTime this.reportDefinitionDetails = reportDefinitionDetails diff --git a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/OnDemandReportCreateRequest.kt b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/OnDemandReportCreateRequest.kt index d1d55c7a..31b34d6e 100644 --- a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/OnDemandReportCreateRequest.kt +++ b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/OnDemandReportCreateRequest.kt @@ -17,6 +17,8 @@ package com.amazon.opendistroforelasticsearch.reportsscheduler.model import com.amazon.opendistroforelasticsearch.reportsscheduler.ReportsSchedulerPlugin.Companion.LOG_PREFIX +import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.Metrics +import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.MetricName import com.amazon.opendistroforelasticsearch.reportsscheduler.model.RestTag.REPORT_DEFINITION_ID_FIELD import com.amazon.opendistroforelasticsearch.reportsscheduler.util.logger import org.elasticsearch.action.ActionRequest @@ -74,7 +76,11 @@ internal class OnDemandReportCreateRequest( } } } - reportDefinitionId ?: throw IllegalArgumentException("$REPORT_DEFINITION_ID_FIELD field absent") + reportDefinitionId ?: run { + Metrics.getInstance().getNumericalMetric(MetricName.REPORT_FROM_DEFINITION_ID_USER_ERROR).increment() + throw IllegalArgumentException("$REPORT_DEFINITION_ID_FIELD field absent") + } + return OnDemandReportCreateRequest(reportDefinitionId) } } diff --git a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/UpdateReportInstanceStatusRequest.kt b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/UpdateReportInstanceStatusRequest.kt index 03273b72..e11bf8c2 100644 --- a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/UpdateReportInstanceStatusRequest.kt +++ b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/UpdateReportInstanceStatusRequest.kt @@ -17,6 +17,8 @@ package com.amazon.opendistroforelasticsearch.reportsscheduler.model import com.amazon.opendistroforelasticsearch.reportsscheduler.ReportsSchedulerPlugin.Companion.LOG_PREFIX +import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.Metrics +import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.MetricName import com.amazon.opendistroforelasticsearch.reportsscheduler.model.ReportInstance.Status import com.amazon.opendistroforelasticsearch.reportsscheduler.model.RestTag.REPORT_INSTANCE_ID_FIELD import com.amazon.opendistroforelasticsearch.reportsscheduler.model.RestTag.STATUS_FIELD @@ -86,8 +88,14 @@ internal class UpdateReportInstanceStatusRequest( } } } - reportInstanceId ?: throw IllegalArgumentException("$REPORT_INSTANCE_ID_FIELD field absent") - status ?: throw IllegalArgumentException("$STATUS_FIELD field absent") + reportInstanceId ?: run { + Metrics.getInstance().getNumericalMetric(MetricName.REPORT_INSTANCE_UPDATE_USER_ERROR).increment() + throw IllegalArgumentException("$REPORT_INSTANCE_ID_FIELD field absent") + } + status ?: run { + Metrics.getInstance().getNumericalMetric(MetricName.REPORT_INSTANCE_UPDATE_USER_ERROR).increment() + throw IllegalArgumentException("$STATUS_FIELD field absent") + } return UpdateReportInstanceStatusRequest(reportInstanceId, status, statusText) } } diff --git a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/OnDemandReportRestHandler.kt b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/OnDemandReportRestHandler.kt index cac2e6a4..899ba0af 100644 --- a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/OnDemandReportRestHandler.kt +++ b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/OnDemandReportRestHandler.kt @@ -19,6 +19,8 @@ import com.amazon.opendistroforelasticsearch.reportsscheduler.ReportsSchedulerPl import com.amazon.opendistroforelasticsearch.reportsscheduler.action.InContextReportCreateAction import com.amazon.opendistroforelasticsearch.reportsscheduler.action.OnDemandReportCreateAction import com.amazon.opendistroforelasticsearch.reportsscheduler.action.ReportInstanceActions +import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.Metrics +import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.MetricName import com.amazon.opendistroforelasticsearch.reportsscheduler.model.InContextReportCreateRequest import com.amazon.opendistroforelasticsearch.reportsscheduler.model.OnDemandReportCreateRequest import com.amazon.opendistroforelasticsearch.reportsscheduler.model.RestTag.REPORT_DEFINITION_ID_FIELD @@ -86,11 +88,15 @@ internal class OnDemandReportRestHandler : BaseRestHandler() { override fun prepareRequest(request: RestRequest, client: NodeClient): RestChannelConsumer { return when (request.method()) { PUT -> RestChannelConsumer { + Metrics.getInstance().getNumericalMetric(MetricName.REPORT_FROM_DEFINITION_TOTAL).increment() + Metrics.getInstance().getNumericalMetric(MetricName.REPORT_FROM_DEFINITION_INTERVAL_COUNT).increment() client.execute(InContextReportCreateAction.ACTION_TYPE, InContextReportCreateRequest(request.contentParserNextToken()), RestResponseToXContentListener(it)) } POST -> RestChannelConsumer { + Metrics.getInstance().getNumericalMetric(MetricName.REPORT_FROM_DEFINITION_ID_TOTAL).increment() + Metrics.getInstance().getNumericalMetric(MetricName.REPORT_FROM_DEFINITION_ID_INTERVAL_COUNT).increment() client.execute(OnDemandReportCreateAction.ACTION_TYPE, OnDemandReportCreateRequest.parse(request.contentParserNextToken(), request.param(REPORT_DEFINITION_ID_FIELD)), RestResponseToXContentListener(it)) diff --git a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportDefinitionListRestHandler.kt b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportDefinitionListRestHandler.kt index c7819132..88dba442 100644 --- a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportDefinitionListRestHandler.kt +++ b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportDefinitionListRestHandler.kt @@ -18,10 +18,13 @@ package com.amazon.opendistroforelasticsearch.reportsscheduler.resthandler import com.amazon.opendistroforelasticsearch.reportsscheduler.ReportsSchedulerPlugin.Companion.BASE_REPORTS_URI import com.amazon.opendistroforelasticsearch.reportsscheduler.action.GetAllReportDefinitionsAction import com.amazon.opendistroforelasticsearch.reportsscheduler.action.ReportDefinitionActions +import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.Metrics +import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.MetricName import com.amazon.opendistroforelasticsearch.reportsscheduler.model.GetAllReportDefinitionsRequest import com.amazon.opendistroforelasticsearch.reportsscheduler.model.RestTag.FROM_INDEX_FIELD import com.amazon.opendistroforelasticsearch.reportsscheduler.model.RestTag.MAX_ITEMS_FIELD import com.amazon.opendistroforelasticsearch.reportsscheduler.settings.PluginSettings + import org.elasticsearch.client.node.NodeClient import org.elasticsearch.rest.BaseRestHandler import org.elasticsearch.rest.BaseRestHandler.RestChannelConsumer @@ -71,6 +74,8 @@ internal class ReportDefinitionListRestHandler : BaseRestHandler() { val maxItems = request.param(MAX_ITEMS_FIELD)?.toIntOrNull() ?: PluginSettings.defaultItemsQueryCount return when (request.method()) { GET -> RestChannelConsumer { + Metrics.getInstance().getNumericalMetric(MetricName.REPORT_DEFINITION_LIST_TOTAL).increment() + Metrics.getInstance().getNumericalMetric(MetricName.REPORT_DEFINITION_LIST_INTERVAL_COUNT).increment() client.execute(GetAllReportDefinitionsAction.ACTION_TYPE, GetAllReportDefinitionsRequest(from, maxItems), RestResponseToXContentListener(it)) diff --git a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportInstanceListRestHandler.kt b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportInstanceListRestHandler.kt index a03a2b9c..0b12b606 100644 --- a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportInstanceListRestHandler.kt +++ b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportInstanceListRestHandler.kt @@ -18,6 +18,8 @@ package com.amazon.opendistroforelasticsearch.reportsscheduler.resthandler import com.amazon.opendistroforelasticsearch.reportsscheduler.ReportsSchedulerPlugin.Companion.BASE_REPORTS_URI import com.amazon.opendistroforelasticsearch.reportsscheduler.action.GetAllReportInstancesAction import com.amazon.opendistroforelasticsearch.reportsscheduler.action.ReportInstanceActions +import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.Metrics +import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.MetricName import com.amazon.opendistroforelasticsearch.reportsscheduler.model.GetAllReportInstancesRequest import com.amazon.opendistroforelasticsearch.reportsscheduler.model.RestTag.FROM_INDEX_FIELD import com.amazon.opendistroforelasticsearch.reportsscheduler.model.RestTag.MAX_ITEMS_FIELD @@ -78,6 +80,8 @@ internal class ReportInstanceListRestHandler : BaseRestHandler() { val maxItems = request.param(MAX_ITEMS_FIELD)?.toIntOrNull() ?: PluginSettings.defaultItemsQueryCount return when (request.method()) { GET -> RestChannelConsumer { + Metrics.getInstance().getNumericalMetric(MetricName.REPORT_INSTANCE_LIST_TOTAL).increment() + Metrics.getInstance().getNumericalMetric(MetricName.REPORT_INSTANCE_LIST_INTERVAL_COUNT).increment() client.execute(GetAllReportInstancesAction.ACTION_TYPE, GetAllReportInstancesRequest(from, maxItems), RestResponseToXContentListener(it)) diff --git a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportInstanceRestHandler.kt b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportInstanceRestHandler.kt index e7256112..80638237 100644 --- a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportInstanceRestHandler.kt +++ b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportInstanceRestHandler.kt @@ -19,6 +19,8 @@ import com.amazon.opendistroforelasticsearch.reportsscheduler.ReportsSchedulerPl import com.amazon.opendistroforelasticsearch.reportsscheduler.action.GetReportInstanceAction import com.amazon.opendistroforelasticsearch.reportsscheduler.action.ReportInstanceActions import com.amazon.opendistroforelasticsearch.reportsscheduler.action.UpdateReportInstanceStatusAction +import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.Metrics +import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.MetricName import com.amazon.opendistroforelasticsearch.reportsscheduler.model.GetReportInstanceRequest import com.amazon.opendistroforelasticsearch.reportsscheduler.model.RestTag.REPORT_INSTANCE_ID_FIELD import com.amazon.opendistroforelasticsearch.reportsscheduler.model.UpdateReportInstanceStatusRequest @@ -86,11 +88,15 @@ internal class ReportInstanceRestHandler : BaseRestHandler() { val reportInstanceId = request.param(REPORT_INSTANCE_ID_FIELD) ?: throw IllegalArgumentException("Must specify id") return when (request.method()) { POST -> RestChannelConsumer { + Metrics.getInstance().getNumericalMetric(MetricName.REPORT_INSTANCE_UPDATE_TOTAL).increment() + Metrics.getInstance().getNumericalMetric(MetricName.REPORT_INSTANCE_UPDATE_INTERVAL_COUNT).increment() client.execute(UpdateReportInstanceStatusAction.ACTION_TYPE, UpdateReportInstanceStatusRequest.parse(request.contentParserNextToken(), reportInstanceId), RestResponseToXContentListener(it)) } GET -> RestChannelConsumer { + Metrics.getInstance().getNumericalMetric(MetricName.REPORT_INSTANCE_INFO_TOTAL).increment() + Metrics.getInstance().getNumericalMetric(MetricName.REPORT_INSTANCE_INFO_INTERVAL_COUNT).increment() client.execute(GetReportInstanceAction.ACTION_TYPE, GetReportInstanceRequest(reportInstanceId), RestResponseToXContentListener(it)) From 36a3c4816120e905ac14d088327b7dff41db25b0 Mon Sep 17 00:00:00 2001 From: Abbas Hussain Date: Mon, 4 Jan 2021 12:49:28 -0800 Subject: [PATCH 13/25] Refactor --- .../reportsscheduler/metrics/Metrics.java | 29 ------------------- .../resthandler/ReportStatsRestHandler.kt | 9 +----- 2 files changed, 1 insertion(+), 37 deletions(-) diff --git a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Metrics.java b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Metrics.java index 32659ead..8aed039e 100644 --- a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Metrics.java +++ b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Metrics.java @@ -97,39 +97,10 @@ public String collectToJSON() { } public String collectToFlattenedJSON() { - String flattenedJson = "{\"report_definition.create.count\":2,\"report_definition.create.total\":4," + - "\"report_definition.create.system_error\":2,\"report_definition.create.customer_error\":3," + - "\"report_definition.list.count\":2,\"report_definition.list.total\":4," + - "\"report_definition.list.system_error\":2,\"report_definition.list.customer_error\":3," + - "\"report_instance.create.count\":2,\"report_instance.create.total\":4," + - "\"report_instance.create.system_error\":2,\"report_instance.create.customer_error\":3}"; - String nestedJson = "{\"report_definition\":{\"create\":{\"count\":2," + - "\"total\":4,\"system_error\":2,\"customer_error\":3},\"list\":" + - "{\"count\":2,\"total\":4,\"system_error\":2,\"customer_error\":3}}," + - "\"report_instance\":{\"create\":{\"count\":2,\"total\":4,\"system_error\":2," + - "\"customer_error\":3}}}"; String metricsJson = JsonUnflattener.unflatten(collectToJSON()); return metricsJson; } - - public String requestString(RestRequest request) { - Map map = new HashMap<>(); - map.put("path" , request.path()); - map.put("rawPath", request.rawPath()); - map.put("uri" , request.uri()); - return map.toString(); - } - - public String test(RestRequest request) { - Map map = new HashMap<>(); - map.put("startTime" , request.param(STATS_START_TIME, "0000-00-00T00:00:00")); - map.put("endTime" , request.param(STATS_END_TIME, "0000-00-00T00:00:00")); - return map.toString(); - } - - - public void clear() { registeredMetricsByName.clear(); } diff --git a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportStatsRestHandler.kt b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportStatsRestHandler.kt index 8490bdcc..12d4f21d 100644 --- a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportStatsRestHandler.kt +++ b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportStatsRestHandler.kt @@ -87,14 +87,7 @@ internal class ReportStatsRestHandler : BaseRestHandler() { override fun prepareRequest(request: RestRequest, client: NodeClient): RestChannelConsumer { return when (request.method()) { GET -> RestChannelConsumer { - when (request.path()) { - REPORT_STATS_URL -> { - it.sendResponse(BytesRestResponse(RestStatus.OK, Metrics.getInstance().collectToFlattenedJSON())) - } - REPORT_TEST_URL -> { - it.sendResponse(BytesRestResponse(RestStatus.OK, Metrics.getInstance().test(request))) - } - } + it.sendResponse(BytesRestResponse(RestStatus.OK, Metrics.getInstance().collectToFlattenedJSON())) } else -> RestChannelConsumer { it.sendResponse(BytesRestResponse(RestStatus.METHOD_NOT_ALLOWED, "${request.method()} is not allowed")) From 15b93212e335794d104404aed5acfeccc6e5e55c Mon Sep 17 00:00:00 2001 From: Abbas Hussain Date: Mon, 4 Jan 2021 13:42:27 -0800 Subject: [PATCH 14/25] Refactor and place more metrics --- .../metrics/MetricFactory.java | 2 +- .../reportsscheduler/metrics/MetricName.java | 2 +- .../reportsscheduler/metrics/Metrics.java | 6 ---- .../action/ReportDefinitionActions.kt | 25 ++++++++++++-- .../action/ReportInstanceActions.kt | 34 ++++++++++++++++--- .../index/ReportDefinitionsIndex.kt | 1 - .../security/UserAccessManager.kt | 14 ++++++-- 7 files changed, 65 insertions(+), 19 deletions(-) diff --git a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricFactory.java b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricFactory.java index 68dd14b3..604a26c9 100644 --- a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricFactory.java +++ b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricFactory.java @@ -72,7 +72,7 @@ public static Metric createMetric(MetricName name) { case REPORT_FROM_DEFINITION_ID_USER_ERROR: case REPORT_FROM_DEFINITION_ID_SYSTEM_ERROR: - case REPORT_PERMISSION_ERROR: + case REPORT_PERMISSION_USER_ERROR: return new NumericMetric<>(name.getName(), new RollingCounter()); default: return new NumericMetric<>(name.getName(), new BasicCounter()); diff --git a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricName.java b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricName.java index 90d141a5..c7a4f169 100644 --- a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricName.java +++ b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricName.java @@ -71,7 +71,7 @@ public enum MetricName { REPORT_FROM_DEFINITION_ID_USER_ERROR("on_demand_from_definition.create.user_error", true), REPORT_FROM_DEFINITION_ID_SYSTEM_ERROR("on_demand_from_definition.create.system_error.", true), - REPORT_PERMISSION_ERROR("permission_error", true), + REPORT_PERMISSION_USER_ERROR("permission_error", true), DEFAULT("default", true); diff --git a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Metrics.java b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Metrics.java index 8aed039e..30d94d6a 100644 --- a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Metrics.java +++ b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Metrics.java @@ -15,18 +15,12 @@ package com.amazon.opendistroforelasticsearch.reportsscheduler.metrics; -import org.elasticsearch.rest.RestRequest; import org.json.JSONObject; import java.time.Clock; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; -import java.util.Map; import java.util.concurrent.ConcurrentHashMap; -import static com.amazon.opendistroforelasticsearch.reportsscheduler.model.RestTag.STATS_START_TIME; -import static com.amazon.opendistroforelasticsearch.reportsscheduler.model.RestTag.STATS_END_TIME; - import com.github.wnameless.json.unflattener.JsonUnflattener; public class Metrics { diff --git a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/action/ReportDefinitionActions.kt b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/action/ReportDefinitionActions.kt index f35414bf..8382233b 100644 --- a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/action/ReportDefinitionActions.kt +++ b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/action/ReportDefinitionActions.kt @@ -19,6 +19,8 @@ package com.amazon.opendistroforelasticsearch.reportsscheduler.action import com.amazon.opendistroforelasticsearch.commons.authuser.User import com.amazon.opendistroforelasticsearch.reportsscheduler.ReportsSchedulerPlugin.Companion.LOG_PREFIX import com.amazon.opendistroforelasticsearch.reportsscheduler.index.ReportDefinitionsIndex +import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.Metrics +import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.MetricName import com.amazon.opendistroforelasticsearch.reportsscheduler.model.CreateReportDefinitionRequest import com.amazon.opendistroforelasticsearch.reportsscheduler.model.CreateReportDefinitionResponse import com.amazon.opendistroforelasticsearch.reportsscheduler.model.DeleteReportDefinitionRequest @@ -74,8 +76,13 @@ internal object ReportDefinitionActions { UserAccessManager.validateUser(user) val currentReportDefinitionDetails = ReportDefinitionsIndex.getReportDefinition(request.reportDefinitionId) currentReportDefinitionDetails - ?: throw ElasticsearchStatusException("Report Definition ${request.reportDefinitionId} not found", RestStatus.NOT_FOUND) + ?: run { + Metrics.getInstance().getNumericalMetric(MetricName.REPORT_DEFINITION_UPDATE_USER_ERROR).increment() + throw ElasticsearchStatusException("Report Definition ${request.reportDefinitionId} not found", RestStatus.NOT_FOUND) + } + if (!UserAccessManager.doesUserHasAccess(user, currentReportDefinitionDetails.tenant, currentReportDefinitionDetails.access)) { + Metrics.getInstance().getNumericalMetric(MetricName.REPORT_PERMISSION_USER_ERROR).increment() throw ElasticsearchStatusException("Permission denied for Report Definition ${request.reportDefinitionId}", RestStatus.FORBIDDEN) } val currentTime = Instant.now() @@ -87,6 +94,7 @@ internal object ReportDefinitionActions { request.reportDefinition ) if (!ReportDefinitionsIndex.updateReportDefinition(request.reportDefinitionId, reportDefinitionDetails)) { + Metrics.getInstance().getNumericalMetric(MetricName.REPORT_DEFINITION_UPDATE_SYSTEM_ERROR).increment() throw ElasticsearchStatusException("Report Definition Update failed", RestStatus.INTERNAL_SERVER_ERROR) } return UpdateReportDefinitionResponse(request.reportDefinitionId) @@ -102,8 +110,13 @@ internal object ReportDefinitionActions { UserAccessManager.validateUser(user) val reportDefinitionDetails = ReportDefinitionsIndex.getReportDefinition(request.reportDefinitionId) reportDefinitionDetails - ?: throw ElasticsearchStatusException("Report Definition ${request.reportDefinitionId} not found", RestStatus.NOT_FOUND) + ?: run { + Metrics.getInstance().getNumericalMetric(MetricName.REPORT_DEFINITION_INFO_SYSTEM_ERROR).increment() + throw ElasticsearchStatusException("Report Definition ${request.reportDefinitionId} not found", RestStatus.NOT_FOUND) + } + if (!UserAccessManager.doesUserHasAccess(user, reportDefinitionDetails.tenant, reportDefinitionDetails.access)) { + Metrics.getInstance().getNumericalMetric(MetricName.REPORT_PERMISSION_USER_ERROR).increment() throw ElasticsearchStatusException("Permission denied for Report Definition ${request.reportDefinitionId}", RestStatus.FORBIDDEN) } return GetReportDefinitionResponse(reportDefinitionDetails, UserAccessManager.hasAllInfoAccess(user)) @@ -119,11 +132,17 @@ internal object ReportDefinitionActions { UserAccessManager.validateUser(user) val reportDefinitionDetails = ReportDefinitionsIndex.getReportDefinition(request.reportDefinitionId) reportDefinitionDetails - ?: throw ElasticsearchStatusException("Report Definition ${request.reportDefinitionId} not found", RestStatus.NOT_FOUND) + ?: run { + Metrics.getInstance().getNumericalMetric(MetricName.REPORT_DEFINITION_DELETE_USER_ERROR).increment() + throw ElasticsearchStatusException("Report Definition ${request.reportDefinitionId} not found", RestStatus.NOT_FOUND) + } + if (!UserAccessManager.doesUserHasAccess(user, reportDefinitionDetails.tenant, reportDefinitionDetails.access)) { + Metrics.getInstance().getNumericalMetric(MetricName.REPORT_PERMISSION_USER_ERROR).increment() throw ElasticsearchStatusException("Permission denied for Report Definition ${request.reportDefinitionId}", RestStatus.FORBIDDEN) } if (!ReportDefinitionsIndex.deleteReportDefinition(request.reportDefinitionId)) { + Metrics.getInstance().getNumericalMetric(MetricName.REPORT_DEFINITION_DELETE_SYSTEM_ERROR).increment() throw ElasticsearchStatusException("Report Definition ${request.reportDefinitionId} delete failed", RestStatus.REQUEST_TIMEOUT) } return DeleteReportDefinitionResponse(request.reportDefinitionId) diff --git a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/action/ReportInstanceActions.kt b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/action/ReportInstanceActions.kt index 082a227f..f0c83b8d 100644 --- a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/action/ReportInstanceActions.kt +++ b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/action/ReportInstanceActions.kt @@ -20,6 +20,8 @@ import com.amazon.opendistroforelasticsearch.commons.authuser.User import com.amazon.opendistroforelasticsearch.reportsscheduler.ReportsSchedulerPlugin.Companion.LOG_PREFIX import com.amazon.opendistroforelasticsearch.reportsscheduler.index.ReportDefinitionsIndex import com.amazon.opendistroforelasticsearch.reportsscheduler.index.ReportInstancesIndex +import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.Metrics +import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.MetricName import com.amazon.opendistroforelasticsearch.reportsscheduler.model.GetAllReportInstancesRequest import com.amazon.opendistroforelasticsearch.reportsscheduler.model.GetAllReportInstancesResponse import com.amazon.opendistroforelasticsearch.reportsscheduler.model.GetReportInstanceRequest @@ -68,7 +70,10 @@ internal object ReportInstanceActions { request.statusText, request.inContextDownloadUrlPath) val docId = ReportInstancesIndex.createReportInstance(reportInstance) - docId ?: throw ElasticsearchStatusException("Report Instance Creation failed", RestStatus.INTERNAL_SERVER_ERROR) + docId ?: run { + Metrics.getInstance().getNumericalMetric(MetricName.REPORT_FROM_DEFINITION_SYSTEM_ERROR).increment() + throw ElasticsearchStatusException("Report Instance Creation failed", RestStatus.INTERNAL_SERVER_ERROR) + } val reportInstanceCopy = reportInstance.copy(id = docId) return InContextReportCreateResponse(reportInstanceCopy, UserAccessManager.hasAllInfoAccess(user)) } @@ -84,8 +89,13 @@ internal object ReportInstanceActions { val currentTime = Instant.now() val reportDefinitionDetails = ReportDefinitionsIndex.getReportDefinition(request.reportDefinitionId) reportDefinitionDetails - ?: throw ElasticsearchStatusException("Report Definition ${request.reportDefinitionId} not found", RestStatus.NOT_FOUND) + ?: run { + Metrics.getInstance().getNumericalMetric(MetricName.REPORT_DEFINITION_INFO_USER_ERROR).increment() + throw ElasticsearchStatusException("Report Definition ${request.reportDefinitionId} not found", RestStatus.NOT_FOUND) + } + if (!UserAccessManager.doesUserHasAccess(user, reportDefinitionDetails.tenant, reportDefinitionDetails.access)) { + Metrics.getInstance().getNumericalMetric(MetricName.REPORT_PERMISSION_USER_ERROR).increment() throw ElasticsearchStatusException("Permission denied for Report Definition ${request.reportDefinitionId}", RestStatus.FORBIDDEN) } val beginTime: Instant = currentTime.minus(reportDefinitionDetails.reportDefinition.format.duration) @@ -101,7 +111,10 @@ internal object ReportInstanceActions { reportDefinitionDetails, currentStatus) val docId = ReportInstancesIndex.createReportInstance(reportInstance) - docId ?: throw ElasticsearchStatusException("Report Instance Creation failed", RestStatus.INTERNAL_SERVER_ERROR) + docId ?: run { + Metrics.getInstance().getNumericalMetric(MetricName.REPORT_FROM_DEFINITION_ID_SYSTEM_ERROR).increment() + throw ElasticsearchStatusException("Report Instance Creation failed", RestStatus.INTERNAL_SERVER_ERROR) + } val reportInstanceCopy = reportInstance.copy(id = docId) return OnDemandReportCreateResponse(reportInstanceCopy, UserAccessManager.hasAllInfoAccess(user)) } @@ -116,11 +129,16 @@ internal object ReportInstanceActions { UserAccessManager.validateUser(user) val currentReportInstance = ReportInstancesIndex.getReportInstance(request.reportInstanceId) currentReportInstance - ?: throw ElasticsearchStatusException("Report Instance ${request.reportInstanceId} not found", RestStatus.NOT_FOUND) + ?: run { + Metrics.getInstance().getNumericalMetric(MetricName.REPORT_INSTANCE_UPDATE_USER_ERROR).increment() + throw ElasticsearchStatusException("Report Instance ${request.reportInstanceId} not found", RestStatus.NOT_FOUND) + } if (!UserAccessManager.doesUserHasAccess(user, currentReportInstance.tenant, currentReportInstance.access)) { + Metrics.getInstance().getNumericalMetric(MetricName.REPORT_PERMISSION_USER_ERROR).increment() throw ElasticsearchStatusException("Permission denied for Report Definition ${request.reportInstanceId}", RestStatus.FORBIDDEN) } if (request.status == Status.Scheduled) { // Don't allow changing status to Scheduled + Metrics.getInstance().getNumericalMetric(MetricName.REPORT_INSTANCE_UPDATE_USER_ERROR).increment() throw ElasticsearchStatusException("Status cannot be updated to ${Status.Scheduled}", RestStatus.BAD_REQUEST) } val currentTime = Instant.now() @@ -128,6 +146,7 @@ internal object ReportInstanceActions { status = request.status, statusText = request.statusText) if (!ReportInstancesIndex.updateReportInstance(updatedReportInstance)) { + Metrics.getInstance().getNumericalMetric(MetricName.REPORT_INSTANCE_UPDATE_SYSTEM_ERROR).increment() throw ElasticsearchStatusException("Report Instance state update failed", RestStatus.INTERNAL_SERVER_ERROR) } return UpdateReportInstanceStatusResponse(request.reportInstanceId) @@ -143,8 +162,13 @@ internal object ReportInstanceActions { UserAccessManager.validateUser(user) val reportInstance = ReportInstancesIndex.getReportInstance(request.reportInstanceId) reportInstance - ?: throw ElasticsearchStatusException("Report Instance ${request.reportInstanceId} not found", RestStatus.NOT_FOUND) + ?: run { + Metrics.getInstance().getNumericalMetric(MetricName.REPORT_INSTANCE_INFO_USER_ERROR).increment() + throw ElasticsearchStatusException("Report Instance ${request.reportInstanceId} not found", RestStatus.NOT_FOUND) + } + if (!UserAccessManager.doesUserHasAccess(user, reportInstance.tenant, reportInstance.access)) { + Metrics.getInstance().getNumericalMetric(MetricName.REPORT_PERMISSION_USER_ERROR).increment() throw ElasticsearchStatusException("Permission denied for Report Definition ${request.reportInstanceId}", RestStatus.FORBIDDEN) } return GetReportInstanceResponse(reportInstance, UserAccessManager.hasAllInfoAccess(user)) diff --git a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/index/ReportDefinitionsIndex.kt b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/index/ReportDefinitionsIndex.kt index 66c48368..b62f865f 100644 --- a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/index/ReportDefinitionsIndex.kt +++ b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/index/ReportDefinitionsIndex.kt @@ -141,7 +141,6 @@ internal object ReportDefinitionsIndex { val response = actionFuture.actionGet(PluginSettings.operationTimeoutMs) return if (response.sourceAsString == null) { log.warn("$LOG_PREFIX:getReportDefinition - $id not found; response:$response") - Metrics.getInstance().getNumericalMetric(MetricName.REPORT_DEFINITION_INFO_SYSTEM_ERROR) null } else { val parser = XContentType.JSON.xContent().createParser(NamedXContentRegistry.EMPTY, diff --git a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/security/UserAccessManager.kt b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/security/UserAccessManager.kt index a178bd4e..40f7f4ea 100644 --- a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/security/UserAccessManager.kt +++ b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/security/UserAccessManager.kt @@ -17,6 +17,8 @@ package com.amazon.opendistroforelasticsearch.reportsscheduler.security import com.amazon.opendistroforelasticsearch.commons.authuser.User +import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.Metrics +import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.MetricName import com.amazon.opendistroforelasticsearch.reportsscheduler.settings.PluginSettings import com.amazon.opendistroforelasticsearch.reportsscheduler.settings.PluginSettings.FilterBy import org.elasticsearch.ElasticsearchStatusException @@ -48,6 +50,7 @@ internal object UserAccessManager { */ fun validateUser(user: User?) { if (isUserPrivateTenant(user) && user?.name == null) { + Metrics.getInstance().getNumericalMetric(MetricName.REPORT_PERMISSION_USER_ERROR).increment() throw ElasticsearchStatusException("User name not provided for private tenant access", RestStatus.FORBIDDEN) } @@ -56,20 +59,26 @@ internal object UserAccessManager { } FilterBy.User -> { // User name must be present user?.name - ?: throw ElasticsearchStatusException("Filter-by enabled with security disabled", - RestStatus.FORBIDDEN) + ?: run { + Metrics.getInstance().getNumericalMetric(MetricName.REPORT_PERMISSION_USER_ERROR).increment() + throw ElasticsearchStatusException("Filter-by enabled with security disabled", + RestStatus.FORBIDDEN) + } } FilterBy.Roles -> { // backend roles must be present if (user == null || user.roles.isNullOrEmpty()) { + Metrics.getInstance().getNumericalMetric(MetricName.REPORT_PERMISSION_USER_ERROR).increment() throw ElasticsearchStatusException("User doesn't have roles configured. Contact administrator.", RestStatus.FORBIDDEN) } else if (user.roles.stream().filter { !PluginSettings.ignoredRoles.contains(it) }.count() == 0L) { + Metrics.getInstance().getNumericalMetric(MetricName.REPORT_PERMISSION_USER_ERROR).increment() throw ElasticsearchStatusException("No distinguishing roles configured. Contact administrator.", RestStatus.FORBIDDEN) } } FilterBy.BackendRoles -> { // backend roles must be present if (user?.backendRoles.isNullOrEmpty()) { + Metrics.getInstance().getNumericalMetric(MetricName.REPORT_PERMISSION_USER_ERROR).increment() throw ElasticsearchStatusException("User doesn't have backend roles configured. Contact administrator.", RestStatus.FORBIDDEN) } @@ -83,6 +92,7 @@ internal object UserAccessManager { fun validatePollingUser(user: User?) { if (user != null) { // Check only if security is enabled if (user.name != KIBANA_SERVER_USER) { + Metrics.getInstance().getNumericalMetric(MetricName.REPORT_PERMISSION_USER_ERROR).increment() throw ElasticsearchStatusException("Permission denied", RestStatus.FORBIDDEN) } } From acf8db389669b19b5eb156e29090dacdb59c5a82 Mon Sep 17 00:00:00 2001 From: Abbas Hussain Date: Mon, 4 Jan 2021 13:46:11 -0800 Subject: [PATCH 15/25] Remove commented code --- .../reportsscheduler/metrics/MetricName.java | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricName.java b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricName.java index c7a4f169..572c13d1 100644 --- a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricName.java +++ b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricName.java @@ -94,20 +94,6 @@ public boolean isNumerical() { public static List getNames() { return Arrays.stream(MetricName.values()).map(v -> v.name).collect(Collectors.toList()); } - -// private static Set NUMERICAL_METRIC = new ImmutableSet.Builder() -// .add(PPL_REQ_TOTAL) -// .add(PPL_REQ_COUNT_TOTAL) -// .add(PPL_FAILED_REQ_COUNT_SYS) -// .add(PPL_FAILED_REQ_COUNT_CUS) -// .build(); - -// public boolean isNumerical() { -// return this == REQ_TOTAL || this == REQ_COUNT_TOTAL || this == FAILED_REQ_COUNT_SYS -// || this == FAILED_REQ_COUNT_CUS || this == FAILED_REQ_COUNT_CB || this == DEFAULT -// || this == DEFAULT_CURSOR_REQUEST_TOTAL || this == DEFAULT_CURSOR_REQUEST_COUNT_TOTAL -// || NUMERICAL_METRIC.contains(this); -// } } From b3aeaca0c169470ce10d4f9550e649cc1fbbf467 Mon Sep 17 00:00:00 2001 From: Abbas Hussain Date: Mon, 4 Jan 2021 13:48:23 -0800 Subject: [PATCH 16/25] Fx metric naming --- .../reportsscheduler/metrics/MetricName.java | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricName.java b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricName.java index 572c13d1..2095da60 100644 --- a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricName.java +++ b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricName.java @@ -24,54 +24,54 @@ public enum MetricName { REPORT_DEFINITION_CREATE_TOTAL("report_definition.create.total", true), REPORT_DEFINITION_CREATE_INTERVAL_COUNT("report_definition.create.count", true), REPORT_DEFINITION_CREATE_USER_ERROR("report_definition.create.user_error", true), - REPORT_DEFINITION_CREATE_SYSTEM_ERROR("report_definition.create.system_error.", true), + REPORT_DEFINITION_CREATE_SYSTEM_ERROR("report_definition.create.system_error", true), REPORT_DEFINITION_UPDATE_TOTAL("report_definition.update.total", true), REPORT_DEFINITION_UPDATE_INTERVAL_COUNT("report_definition.update.count", true), REPORT_DEFINITION_UPDATE_USER_ERROR("report_definition.update.user_error", true), - REPORT_DEFINITION_UPDATE_SYSTEM_ERROR("report_definition.update.system_error.", true), + REPORT_DEFINITION_UPDATE_SYSTEM_ERROR("report_definition.update.system_error", true), REPORT_DEFINITION_INFO_TOTAL("report_definition.info.total", true), REPORT_DEFINITION_INFO_INTERVAL_COUNT("report_definition.info.count", true), REPORT_DEFINITION_INFO_USER_ERROR("report_definition.info.user_error", true), - REPORT_DEFINITION_INFO_SYSTEM_ERROR("report_definition.info.system_error.", true), + REPORT_DEFINITION_INFO_SYSTEM_ERROR("report_definition.info.system_error", true), REPORT_DEFINITION_DELETE_TOTAL("report_definition.delete.total", true), REPORT_DEFINITION_DELETE_INTERVAL_COUNT("report_definition.delete.count", true), REPORT_DEFINITION_DELETE_USER_ERROR("report_definition.delete.user_error", true), - REPORT_DEFINITION_DELETE_SYSTEM_ERROR("report_definition.delete.system_error.", true), + REPORT_DEFINITION_DELETE_SYSTEM_ERROR("report_definition.delete.system_error", true), REPORT_DEFINITION_LIST_TOTAL("report_definition.list.total", true), REPORT_DEFINITION_LIST_INTERVAL_COUNT("report_definition.list.count", true), REPORT_DEFINITION_LIST_USER_ERROR("report_definition.list.user_error", true), - REPORT_DEFINITION_LIST_SYSTEM_ERROR("report_definition.list.system_error.", true), + REPORT_DEFINITION_LIST_SYSTEM_ERROR("report_definition.list.system_error", true), REPORT_INSTANCE_UPDATE_TOTAL("report_instance.update.total", true), REPORT_INSTANCE_UPDATE_INTERVAL_COUNT("report_instance.update.count", true), REPORT_INSTANCE_UPDATE_USER_ERROR("report_instance.update.user_error", true), - REPORT_INSTANCE_UPDATE_SYSTEM_ERROR("report_instance.update.system_error.", true), + REPORT_INSTANCE_UPDATE_SYSTEM_ERROR("report_instance.update.system_error", true), REPORT_INSTANCE_INFO_TOTAL("report_instance.info.total", true), REPORT_INSTANCE_INFO_INTERVAL_COUNT("report_instance.info.count", true), REPORT_INSTANCE_INFO_USER_ERROR("report_instance.info.user_error", true), - REPORT_INSTANCE_INFO_SYSTEM_ERROR("report_instance.info.system_error.", true), + REPORT_INSTANCE_INFO_SYSTEM_ERROR("report_instance.info.system_error", true), REPORT_INSTANCE_LIST_TOTAL("report_instance.list.total", true), REPORT_INSTANCE_LIST_INTERVAL_COUNT("report_instance.list.count", true), REPORT_INSTANCE_LIST_USER_ERROR("report_instance.list.user_error", true), - REPORT_INSTANCE_LIST_SYSTEM_ERROR("report_instance.list.system_error.", true), + REPORT_INSTANCE_LIST_SYSTEM_ERROR("report_instance.list.system_error", true), REPORT_FROM_DEFINITION_TOTAL("on_demand.create.total", true), REPORT_FROM_DEFINITION_INTERVAL_COUNT("on_demand.create.count", true), REPORT_FROM_DEFINITION_USER_ERROR("on_demand.create.user_error", true), - REPORT_FROM_DEFINITION_SYSTEM_ERROR("on_demand.create.system_error.", true), + REPORT_FROM_DEFINITION_SYSTEM_ERROR("on_demand.create.system_error", true), REPORT_FROM_DEFINITION_ID_TOTAL("on_demand_from_definition.create.total", true), REPORT_FROM_DEFINITION_ID_INTERVAL_COUNT("on_demand_from_definition.create.count", true), REPORT_FROM_DEFINITION_ID_USER_ERROR("on_demand_from_definition.create.user_error", true), - REPORT_FROM_DEFINITION_ID_SYSTEM_ERROR("on_demand_from_definition.create.system_error.", true), + REPORT_FROM_DEFINITION_ID_SYSTEM_ERROR("on_demand_from_definition.create.system_error", true), - REPORT_PERMISSION_USER_ERROR("permission_error", true), + REPORT_PERMISSION_USER_ERROR("permission_user_error", true), DEFAULT("default", true); From 534a46089ecb92cf1f4770a5450fe741e2f8ee74 Mon Sep 17 00:00:00 2001 From: Abbas Hussain Date: Mon, 4 Jan 2021 13:59:54 -0800 Subject: [PATCH 17/25] remove test code --- .../reportsscheduler/metrics/Metrics.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Metrics.java b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Metrics.java index 30d94d6a..1b9794ec 100644 --- a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Metrics.java +++ b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Metrics.java @@ -84,9 +84,6 @@ public String collectToJSON() { } metricsJSONObject.put(metric.getName(), metric.getValue()); } - - metricsJSONObject.put("time", clock.millis()); - return metricsJSONObject.toString(); } From 884ee9fd7b7602a1b63ee4c088cd0a91729f5159 Mon Sep 17 00:00:00 2001 From: Abbas Hussain Date: Mon, 4 Jan 2021 14:11:58 -0800 Subject: [PATCH 18/25] Fix copyright year --- reports-scheduler/gradle.properties | 2 +- .../reportsscheduler/metrics/BasicCounter.java | 2 +- .../reportsscheduler/metrics/Counter.java | 2 +- .../reportsscheduler/metrics/GaugeMetric.java | 2 +- .../reportsscheduler/metrics/Metric.java | 2 +- .../reportsscheduler/metrics/MetricFactory.java | 2 +- .../reportsscheduler/metrics/MetricName.java | 2 +- .../reportsscheduler/metrics/Metrics.java | 2 +- .../reportsscheduler/metrics/NumericMetric.java | 2 +- .../reportsscheduler/metrics/RollingCounter.java | 2 +- 10 files changed, 10 insertions(+), 10 deletions(-) diff --git a/reports-scheduler/gradle.properties b/reports-scheduler/gradle.properties index 6de2cfcd..52ef91df 100644 --- a/reports-scheduler/gradle.properties +++ b/reports-scheduler/gradle.properties @@ -1,5 +1,5 @@ # -# Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. +# Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"). # You may not use this file except in compliance with the License. diff --git a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/BasicCounter.java b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/BasicCounter.java index a9b7a626..a2a8f42d 100644 --- a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/BasicCounter.java +++ b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/BasicCounter.java @@ -1,5 +1,5 @@ /* - * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. diff --git a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Counter.java b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Counter.java index b01dce5d..44940c52 100644 --- a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Counter.java +++ b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Counter.java @@ -1,5 +1,5 @@ /* - * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. diff --git a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/GaugeMetric.java b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/GaugeMetric.java index d0cbc831..5c3218c0 100644 --- a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/GaugeMetric.java +++ b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/GaugeMetric.java @@ -1,5 +1,5 @@ /* - * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. diff --git a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Metric.java b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Metric.java index e3e36893..9c4efb8b 100644 --- a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Metric.java +++ b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Metric.java @@ -1,5 +1,5 @@ /* - * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. diff --git a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricFactory.java b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricFactory.java index 604a26c9..25cad9f3 100644 --- a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricFactory.java +++ b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. diff --git a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricName.java b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricName.java index 2095da60..c11e3a11 100644 --- a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricName.java +++ b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricName.java @@ -1,5 +1,5 @@ /* - * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. diff --git a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Metrics.java b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Metrics.java index 1b9794ec..5ea96b80 100644 --- a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Metrics.java +++ b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Metrics.java @@ -1,5 +1,5 @@ /* - * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. diff --git a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/NumericMetric.java b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/NumericMetric.java index b6389309..60c113d1 100644 --- a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/NumericMetric.java +++ b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/NumericMetric.java @@ -1,5 +1,5 @@ /* - * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. diff --git a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/RollingCounter.java b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/RollingCounter.java index 792ffaa3..3b42b034 100644 --- a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/RollingCounter.java +++ b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/RollingCounter.java @@ -1,5 +1,5 @@ /* - * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. From fa90f509dd425780685ef87b69504112f0cef869 Mon Sep 17 00:00:00 2001 From: Abbas Hussain Date: Mon, 4 Jan 2021 14:32:10 -0800 Subject: [PATCH 19/25] Address comments --- .../model/GetAllReportDefinitionsRequest.kt | 2 +- .../model/GetAllReportInstancesRequest.kt | 2 +- .../resthandler/ReportStatsRestHandler.kt | 10 ---------- 3 files changed, 2 insertions(+), 12 deletions(-) diff --git a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/GetAllReportDefinitionsRequest.kt b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/GetAllReportDefinitionsRequest.kt index f39199ef..1502e5ab 100644 --- a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/GetAllReportDefinitionsRequest.kt +++ b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/GetAllReportDefinitionsRequest.kt @@ -101,7 +101,7 @@ internal class GetAllReportDefinitionsRequest( override fun validate(): ActionRequestValidationException? { return if (fromIndex < 0) { val exception = ActionRequestValidationException() - exception.addValidationError("fromIndex should be grater than 0") + exception.addValidationError("fromIndex should be greater than 0") Metrics.getInstance().getNumericalMetric(MetricName.REPORT_DEFINITION_LIST_USER_ERROR).increment() exception } else { diff --git a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/GetAllReportInstancesRequest.kt b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/GetAllReportInstancesRequest.kt index a9179312..ef696ffa 100644 --- a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/GetAllReportInstancesRequest.kt +++ b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/GetAllReportInstancesRequest.kt @@ -101,7 +101,7 @@ internal data class GetAllReportInstancesRequest( override fun validate(): ActionRequestValidationException? { return if (fromIndex < 0) { val exception = ActionRequestValidationException() - exception.addValidationError("fromIndex should be grater than 0") + exception.addValidationError("fromIndex should be greater than 0") Metrics.getInstance().getNumericalMetric(MetricName.REPORT_INSTANCE_LIST_USER_ERROR).increment() exception } else { diff --git a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportStatsRestHandler.kt b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportStatsRestHandler.kt index 12d4f21d..a7b18729 100644 --- a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportStatsRestHandler.kt +++ b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportStatsRestHandler.kt @@ -16,19 +16,9 @@ package com.amazon.opendistroforelasticsearch.reportsscheduler.resthandler import com.amazon.opendistroforelasticsearch.reportsscheduler.ReportsSchedulerPlugin.Companion.BASE_REPORTS_URI -// import com.amazon.opendistroforelasticsearch.reportsscheduler.action.CreateReportDefinitionAction -// import com.amazon.opendistroforelasticsearch.reportsscheduler.action.DeleteReportDefinitionAction -// import com.amazon.opendistroforelasticsearch.reportsscheduler.action.GetReportDefinitionAction import com.amazon.opendistroforelasticsearch.reportsscheduler.action.ReportDefinitionActions -// import com.amazon.opendistroforelasticsearch.reportsscheduler.action.UpdateReportDefinitionAction -// import com.amazon.opendistroforelasticsearch.reportsscheduler.model.CreateReportDefinitionRequest -// import com.amazon.opendistroforelasticsearch.reportsscheduler.model.DeleteReportDefinitionRequest -// import com.amazon.opendistroforelasticsearch.reportsscheduler.model.GetReportDefinitionRequest -// import com.amazon.opendistroforelasticsearch.reportsscheduler.model.RestTag.REPORT_DEFINITION_ID_FIELD import com.amazon.opendistroforelasticsearch.reportsscheduler.model.RestTag.STATS_START_TIME import com.amazon.opendistroforelasticsearch.reportsscheduler.model.RestTag.STATS_END_TIME -// import com.amazon.opendistroforelasticsearch.reportsscheduler.model.UpdateReportDefinitionRequest -// import com.amazon.opendistroforelasticsearch.reportsscheduler.util.contentParserNextToken import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.Metrics import org.elasticsearch.client.node.NodeClient import org.elasticsearch.rest.BaseRestHandler From 22d9e07200b6bdd0da047c0a33e8b91d6247546f Mon Sep 17 00:00:00 2001 From: Abbas Hussain Date: Tue, 5 Jan 2021 13:55:59 -0800 Subject: [PATCH 20/25] Address comments --- .../metrics/BasicCounter.java | 15 ++++++- .../reportsscheduler/metrics/Counter.java | 8 ++++ .../reportsscheduler/metrics/GaugeMetric.java | 38 ------------------ .../reportsscheduler/metrics/Metric.java | 7 +++- .../metrics/MetricFactory.java | 17 ++++++++ .../reportsscheduler/metrics/MetricName.java | 28 ++++++++++++- .../reportsscheduler/metrics/Metrics.java | 3 +- .../metrics/NumericMetric.java | 7 +++- .../metrics/RollingCounter.java | 40 ++++++++++++------- .../action/PluginBaseAction.kt | 14 +++++++ .../reportsscheduler/model/RestTag.kt | 6 ++- .../resthandler/ReportStatsRestHandler.kt | 16 ++------ .../RestResponseToXContentListener.kt | 16 +++++++- 13 files changed, 138 insertions(+), 77 deletions(-) delete mode 100644 reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/GaugeMetric.java diff --git a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/BasicCounter.java b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/BasicCounter.java index a2a8f42d..db5de4c9 100644 --- a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/BasicCounter.java +++ b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/BasicCounter.java @@ -17,24 +17,37 @@ import java.util.concurrent.atomic.LongAdder; +/** + * Counter to hold accumulative value over time. + */ public class BasicCounter implements Counter { - private LongAdder count = new LongAdder(); + private final LongAdder count = new LongAdder(); + /** + * {@inheritDoc} + */ @Override public void increment() { count.increment(); } + /** + * {@inheritDoc} + */ @Override public void add(long n) { count.add(n); } + /** + * {@inheritDoc} + */ @Override public Long getValue() { return count.longValue(); } + /** Reset the count value to zero*/ @Override public void reset() { count.reset(); diff --git a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Counter.java b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Counter.java index 44940c52..3270f347 100644 --- a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Counter.java +++ b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Counter.java @@ -15,12 +15,20 @@ package com.amazon.opendistroforelasticsearch.reportsscheduler.metrics; +/** + * Defines a generic counter. + */ public interface Counter { + + /** Increments the count value by 1 unit*/ void increment(); + /** Increments the count value by n unit*/ void add(long n); + /** Retrieves the count value accumulated upto this call*/ T getValue(); + /** Resets the count value to initial value when Counter is created*/ void reset(); } diff --git a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/GaugeMetric.java b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/GaugeMetric.java deleted file mode 100644 index 5c3218c0..00000000 --- a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/GaugeMetric.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * or in the "license" file accompanying this file. This file 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.amazon.opendistroforelasticsearch.reportsscheduler.metrics; - -import java.util.function.Supplier; - -/** - * Gauge metric, an instant value like cpu usage, state and so on - */ -public class GaugeMetric extends Metric { - private Supplier loadValue; - - public GaugeMetric(String name, Supplier supplier) { - super(name); - this.loadValue = supplier; - } - - public String getName() { - return super.getName(); - } - - public T getValue() { - return loadValue.get(); - } -} diff --git a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Metric.java b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Metric.java index 9c4efb8b..32f1833a 100644 --- a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Metric.java +++ b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Metric.java @@ -15,11 +15,14 @@ package com.amazon.opendistroforelasticsearch.reportsscheduler.metrics; +import org.jetbrains.annotations.NotNull; + public abstract class Metric { - private String name; + @NotNull + private final String name; - public Metric(String name) { + public Metric(@NotNull String name) { this.name = name; } diff --git a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricFactory.java b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricFactory.java index 25cad9f3..2feff031 100644 --- a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricFactory.java +++ b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricFactory.java @@ -19,6 +19,8 @@ public class MetricFactory { public static Metric createMetric(MetricName name) { switch (name) { + + case REQUEST_TOTAL: case REPORT_DEFINITION_CREATE_TOTAL: case REPORT_DEFINITION_UPDATE_TOTAL: case REPORT_DEFINITION_INFO_TOTAL: @@ -32,6 +34,20 @@ public static Metric createMetric(MetricName name) { case DEFAULT: return new NumericMetric<>(name.getName(), new BasicCounter()); + case REQUEST_INTERVAL_COUNT: + case REQUEST_SUCCESS: + case REQUEST_USER_ERROR: + case REQUEST_SYSTEM_ERROR: + case REPORT_EXCEPTIONS_ES_STATUS_EXCEPTION: + case REPORT_EXCEPTIONS_ES_SECURITY_EXCEPTION: + case REPORT_EXCEPTIONS_VERSION_CONFLICT_ENGINE_EXCEPTION: + case REPORT_EXCEPTIONS_INDEX_NOT_FOUND_EXCEPTION: + case REPORT_EXCEPTIONS_INVALID_INDEX_NAME_EXCEPTION: + case REPORT_EXCEPTIONS_ILLEGAL_ARGUMENT_EXCEPTION: + case REPORT_EXCEPTIONS_ILLEGAL_STATE_EXCEPTION: + case REPORT_EXCEPTIONS_IO_EXCEPTION: + case REPORT_EXCEPTIONS_INTERNAL_SERVER_ERROR: + case REPORT_DEFINITION_CREATE_INTERVAL_COUNT: case REPORT_DEFINITION_CREATE_USER_ERROR: case REPORT_DEFINITION_CREATE_SYSTEM_ERROR: @@ -72,6 +88,7 @@ public static Metric createMetric(MetricName name) { case REPORT_FROM_DEFINITION_ID_USER_ERROR: case REPORT_FROM_DEFINITION_ID_SYSTEM_ERROR: + case REPORT_SECURITY_PERMISSION_ERROR: case REPORT_PERMISSION_USER_ERROR: return new NumericMetric<>(name.getName(), new RollingCounter()); default: diff --git a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricName.java b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricName.java index c11e3a11..46596b75 100644 --- a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricName.java +++ b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricName.java @@ -21,6 +21,29 @@ public enum MetricName { + REQUEST_TOTAL("request_total", true), + REQUEST_INTERVAL_COUNT("request_count", true), + REQUEST_SUCCESS("success_count", true), + REQUEST_USER_ERROR("failed_request_count_user_error", true), + REQUEST_SYSTEM_ERROR("failed_request_count_system_error", true), + + /** + * Exceptions from: + * @see com.amazon.opendistroforelasticsearch.reportsscheduler.action.PluginBaseAction + */ + REPORT_EXCEPTIONS_ES_STATUS_EXCEPTION("exception.es_status", true), + REPORT_EXCEPTIONS_ES_SECURITY_EXCEPTION("exception.es_security", true), + REPORT_EXCEPTIONS_VERSION_CONFLICT_ENGINE_EXCEPTION("exception.version_conflict_engine", true), + REPORT_EXCEPTIONS_INDEX_NOT_FOUND_EXCEPTION("exception.index_not_found", true), + REPORT_EXCEPTIONS_INVALID_INDEX_NAME_EXCEPTION("exception.invalid_index_name", true), + REPORT_EXCEPTIONS_ILLEGAL_ARGUMENT_EXCEPTION("exception.illegal_argument", true), + REPORT_EXCEPTIONS_ILLEGAL_STATE_EXCEPTION("exception.illegal_state", true), + REPORT_EXCEPTIONS_IO_EXCEPTION("exception.io", true), + REPORT_EXCEPTIONS_INTERNAL_SERVER_ERROR("exception.internal_server_error", true), + + /** + * Per REST endpoint metrics + */ REPORT_DEFINITION_CREATE_TOTAL("report_definition.create.total", true), REPORT_DEFINITION_CREATE_INTERVAL_COUNT("report_definition.create.count", true), REPORT_DEFINITION_CREATE_USER_ERROR("report_definition.create.user_error", true), @@ -71,12 +94,13 @@ public enum MetricName { REPORT_FROM_DEFINITION_ID_USER_ERROR("on_demand_from_definition.create.user_error", true), REPORT_FROM_DEFINITION_ID_SYSTEM_ERROR("on_demand_from_definition.create.system_error", true), + REPORT_SECURITY_PERMISSION_ERROR("es_security_permission_error", true), REPORT_PERMISSION_USER_ERROR("permission_user_error", true), DEFAULT("default", true); - private String name; - private boolean numerical; + private final String name; + private final boolean numerical; MetricName(String name, boolean numerical) { this.name = name; diff --git a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Metrics.java b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Metrics.java index 5ea96b80..a0893b56 100644 --- a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Metrics.java +++ b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Metrics.java @@ -88,8 +88,7 @@ public String collectToJSON() { } public String collectToFlattenedJSON() { - String metricsJson = JsonUnflattener.unflatten(collectToJSON()); - return metricsJson; + return JsonUnflattener.unflatten(collectToJSON()); } public void clear() { diff --git a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/NumericMetric.java b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/NumericMetric.java index 60c113d1..4abc6e97 100644 --- a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/NumericMetric.java +++ b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/NumericMetric.java @@ -15,10 +15,13 @@ package com.amazon.opendistroforelasticsearch.reportsscheduler.metrics; +import org.jetbrains.annotations.NotNull; + public class NumericMetric extends Metric { - private Counter counter; + @NotNull + private final Counter counter; - public NumericMetric(String name, Counter counter) { + public NumericMetric(@NotNull String name, @NotNull Counter counter) { super(name); this.counter = counter; } diff --git a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/RollingCounter.java b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/RollingCounter.java index 3b42b034..1a8c3024 100644 --- a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/RollingCounter.java +++ b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/RollingCounter.java @@ -17,7 +17,6 @@ import java.time.Clock; import java.util.concurrent.ConcurrentSkipListMap; -import java.util.concurrent.atomic.LongAdder; /** * Rolling counter. The count is refreshed every interval. In every interval the count is cumulative. @@ -30,8 +29,7 @@ public class RollingCounter implements Counter { private final long window; private final long interval; private final Clock clock; - private final ConcurrentSkipListMap time2CountWin; - private final LongAdder count; + private final ConcurrentSkipListMap timeToCountMap = new ConcurrentSkipListMap<>(); public RollingCounter() { this(METRICS_ROLLING_WINDOW_VALUE, METRICS_ROLLING_INTERVAL_VALUE); @@ -41,8 +39,6 @@ public RollingCounter(long window, long interval, Clock clock) { this.window = window; this.interval = interval; this.clock = clock; - time2CountWin = new ConcurrentSkipListMap<>(); - count = new LongAdder(); capacity = window / interval * 2; } @@ -50,37 +46,45 @@ public RollingCounter(long window, long interval) { this(window, interval, Clock.systemDefaultZone()); } + /** + * {@inheritDoc} + */ @Override public void increment() { add(1L); } + /** + * {@inheritDoc} + */ @Override public void add(long n) { trim(); - time2CountWin.compute(getKey(clock.millis()), (k, v) -> (v == null) ? n : v + n); + timeToCountMap.compute(getKey(clock.millis()), (k, v) -> (v == null) ? n : v + n); } + /** + * {@inheritDoc} + */ @Override public Long getValue() { return getValue(getPreKey(clock.millis())); } + /** + * {@inheritDoc} + */ public long getValue(long key) { - Long res = time2CountWin.get(key); + Long res = timeToCountMap.get(key); if (res == null) { return 0; } return res; } - public long getSum() { - return count.longValue(); - } - private void trim() { - if (time2CountWin.size() > capacity) { - time2CountWin.headMap(getKey(clock.millis() - window * 1000)).clear(); + if (timeToCountMap.size() > capacity) { + timeToCountMap.headMap(getKey(clock.millis() - window * 1000)).clear(); } } @@ -92,11 +96,17 @@ private long getPreKey(long millis) { return getKey(millis) - 1; } + /** + * Number of existing intervals + */ public int size() { - return time2CountWin.size(); + return timeToCountMap.size(); } + /** + * Remove all the items from counter + */ public void reset() { - time2CountWin.clear(); + timeToCountMap.clear(); } } diff --git a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/action/PluginBaseAction.kt b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/action/PluginBaseAction.kt index c24240fa..6fedfcf3 100644 --- a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/action/PluginBaseAction.kt +++ b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/action/PluginBaseAction.kt @@ -18,6 +18,8 @@ package com.amazon.opendistroforelasticsearch.reportsscheduler.action import com.amazon.opendistroforelasticsearch.commons.ConfigConstants.OPENDISTRO_SECURITY_USER_INFO_THREAD_CONTEXT import com.amazon.opendistroforelasticsearch.commons.authuser.User +import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.Metrics +import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.MetricName import com.amazon.opendistroforelasticsearch.reportsscheduler.ReportsSchedulerPlugin.Companion.LOG_PREFIX import com.amazon.opendistroforelasticsearch.reportsscheduler.util.logger import kotlinx.coroutines.CoroutineScope @@ -65,33 +67,45 @@ abstract class PluginBaseAction { - return setOf(STATS_START_TIME, STATS_END_TIME) + return setOf() } /** diff --git a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/RestResponseToXContentListener.kt b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/RestResponseToXContentListener.kt index 325faff8..07be8a32 100644 --- a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/RestResponseToXContentListener.kt +++ b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/RestResponseToXContentListener.kt @@ -16,6 +16,12 @@ package com.amazon.opendistroforelasticsearch.reportsscheduler.resthandler +import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.Metrics +import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.MetricName +import com.amazon.opendistroforelasticsearch.reportsscheduler.model.RestTag.SUCCESS_END_STATUS_CODE +import com.amazon.opendistroforelasticsearch.reportsscheduler.model.RestTag.SUCCESS_START_STATUS_CODE +import com.amazon.opendistroforelasticsearch.reportsscheduler.model.RestTag.CLIENT_END_STATUS_CODE +import com.amazon.opendistroforelasticsearch.reportsscheduler.model.RestTag.CLIENT_START_STATUS_CODE import com.amazon.opendistroforelasticsearch.reportsscheduler.model.BaseResponse import org.elasticsearch.rest.RestChannel import org.elasticsearch.rest.RestStatus @@ -31,6 +37,14 @@ internal class RestResponseToXContentListener(channel: * {@inheritDoc} */ override fun getStatus(response: Response): RestStatus { - return response.getStatus() + val restStatus = response.getStatus() + when (restStatus.status) { + in SUCCESS_START_STATUS_CODE..SUCCESS_END_STATUS_CODE -> Metrics.getInstance().getNumericalMetric(MetricName.REQUEST_SUCCESS).increment() + RestStatus.FORBIDDEN.status -> Metrics.getInstance().getNumericalMetric(MetricName.REPORT_SECURITY_PERMISSION_ERROR).increment() + in CLIENT_START_STATUS_CODE..CLIENT_END_STATUS_CODE -> Metrics.getInstance().getNumericalMetric(MetricName.REQUEST_USER_ERROR).increment() + else -> Metrics.getInstance().getNumericalMetric(MetricName.REQUEST_SYSTEM_ERROR).increment() + } + + return restStatus } } From 766ffe58ad0a23828c4d14ae12fa4e2a43f3cb26 Mon Sep 17 00:00:00 2001 From: Abbas Hussain Date: Tue, 5 Jan 2021 18:23:24 -0800 Subject: [PATCH 21/25] Address more comments --- .../reportsscheduler/metrics/MetricName.java | 8 -- .../reportsscheduler/metrics/MyMetrics.java | 132 ++++++++++++++++++ .../ReportsSchedulerPlugin.kt | 5 +- .../action/PluginBaseAction.kt | 26 ++-- .../action/ReportDefinitionActions.kt | 21 +-- .../action/ReportInstanceActions.kt | 25 ++-- .../index/ReportDefinitionsIndex.kt | 15 +- .../model/CreateReportDefinitionRequest.kt | 7 +- .../model/CreateReportDefinitionResponse.kt | 7 +- .../model/DeleteReportDefinitionRequest.kt | 7 +- .../model/DeleteReportDefinitionResponse.kt | 7 +- .../model/GetAllReportDefinitionsRequest.kt | 7 +- .../model/GetAllReportInstancesRequest.kt | 7 +- .../model/GetReportDefinitionRequest.kt | 7 +- .../model/GetReportDefinitionResponse.kt | 7 +- .../model/GetReportInstanceRequest.kt | 7 +- .../model/InContextReportCreateRequest.kt | 11 +- .../model/OnDemandReportCreateRequest.kt | 7 +- .../model/UpdateReportDefinitionRequest.kt | 9 +- .../model/UpdateReportDefinitionResponse.kt | 7 +- .../UpdateReportInstanceStatusRequest.kt | 9 +- .../resthandler/OnDemandReportRestHandler.kt | 13 +- .../ReportDefinitionListRestHandler.kt | 9 +- .../ReportDefinitionRestHandler.kt | 21 +-- .../ReportInstanceListRestHandler.kt | 9 +- .../resthandler/ReportInstanceRestHandler.kt | 13 +- .../resthandler/ReportStatsRestHandler.kt | 6 +- .../RestResponseToXContentListener.kt | 41 +++--- .../security/UserAccessManager.kt | 17 +-- 29 files changed, 310 insertions(+), 157 deletions(-) create mode 100644 reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MyMetrics.java diff --git a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricName.java b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricName.java index 46596b75..e2cfa620 100644 --- a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricName.java +++ b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricName.java @@ -15,10 +15,6 @@ package com.amazon.opendistroforelasticsearch.reportsscheduler.metrics; -import java.util.Arrays; -import java.util.List; -import java.util.stream.Collectors; - public enum MetricName { REQUEST_TOTAL("request_total", true), @@ -114,10 +110,6 @@ public String getName() { public boolean isNumerical() { return numerical; } - - public static List getNames() { - return Arrays.stream(MetricName.values()).map(v -> v.name).collect(Collectors.toList()); - } } diff --git a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MyMetrics.java b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MyMetrics.java new file mode 100644 index 00000000..6781c014 --- /dev/null +++ b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MyMetrics.java @@ -0,0 +1,132 @@ +/* + * Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). + * You may not use this file except in compliance with the License. + * A copy of the License is located at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * or in the "license" file accompanying this file. This file 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.amazon.opendistroforelasticsearch.reportsscheduler.metrics; + +import com.github.wnameless.json.unflattener.JsonUnflattener; +import org.json.JSONObject; + +public enum MyMetrics { + + REQUEST_TOTAL("request_total", new BasicCounter()), + REQUEST_INTERVAL_COUNT("request_count", new RollingCounter()), + REQUEST_SUCCESS("success_count", new RollingCounter()), + REQUEST_USER_ERROR("failed_request_count_user_error", new RollingCounter()), + REQUEST_SYSTEM_ERROR("failed_request_count_system_error", new RollingCounter()), + + /** + * Exceptions from: + * @see com.amazon.opendistroforelasticsearch.reportsscheduler.action.PluginBaseAction + */ + REPORT_EXCEPTIONS_ES_STATUS_EXCEPTION("exception.es_status", new RollingCounter()), + REPORT_EXCEPTIONS_ES_SECURITY_EXCEPTION("exception.es_security", new RollingCounter()), + REPORT_EXCEPTIONS_VERSION_CONFLICT_ENGINE_EXCEPTION("exception.version_conflict_engine", new RollingCounter()), + REPORT_EXCEPTIONS_INDEX_NOT_FOUND_EXCEPTION("exception.index_not_found", new RollingCounter()), + REPORT_EXCEPTIONS_INVALID_INDEX_NAME_EXCEPTION("exception.invalid_index_name", new RollingCounter()), + REPORT_EXCEPTIONS_ILLEGAL_ARGUMENT_EXCEPTION("exception.illegal_argument", new RollingCounter()), + REPORT_EXCEPTIONS_ILLEGAL_STATE_EXCEPTION("exception.illegal_state", new RollingCounter()), + REPORT_EXCEPTIONS_IO_EXCEPTION("exception.io", new RollingCounter()), + REPORT_EXCEPTIONS_INTERNAL_SERVER_ERROR("exception.internal_server_error", new RollingCounter()), + + /** + * Per REST endpoint metrics + */ + REPORT_DEFINITION_CREATE_TOTAL("report_definition.create.total", new BasicCounter()), + REPORT_DEFINITION_CREATE_INTERVAL_COUNT("report_definition.create.count", new RollingCounter()), + REPORT_DEFINITION_CREATE_USER_ERROR("report_definition.create.user_error", new RollingCounter()), + REPORT_DEFINITION_CREATE_SYSTEM_ERROR("report_definition.create.system_error", new RollingCounter()), + + REPORT_DEFINITION_UPDATE_TOTAL("report_definition.update.total", new BasicCounter()), + REPORT_DEFINITION_UPDATE_INTERVAL_COUNT("report_definition.update.count", new RollingCounter()), + REPORT_DEFINITION_UPDATE_USER_ERROR("report_definition.update.user_error", new RollingCounter()), + REPORT_DEFINITION_UPDATE_SYSTEM_ERROR("report_definition.update.system_error", new RollingCounter()), + + REPORT_DEFINITION_INFO_TOTAL("report_definition.info.total", new BasicCounter()), + REPORT_DEFINITION_INFO_INTERVAL_COUNT("report_definition.info.count", new RollingCounter()), + REPORT_DEFINITION_INFO_USER_ERROR("report_definition.info.user_error", new RollingCounter()), + REPORT_DEFINITION_INFO_SYSTEM_ERROR("report_definition.info.system_error", new RollingCounter()), + + REPORT_DEFINITION_DELETE_TOTAL("report_definition.delete.total", new BasicCounter()), + REPORT_DEFINITION_DELETE_INTERVAL_COUNT("report_definition.delete.count", new RollingCounter()), + REPORT_DEFINITION_DELETE_USER_ERROR("report_definition.delete.user_error", new RollingCounter()), + REPORT_DEFINITION_DELETE_SYSTEM_ERROR("report_definition.delete.system_error", new RollingCounter()), + + REPORT_DEFINITION_LIST_TOTAL("report_definition.list.total",new BasicCounter()), + REPORT_DEFINITION_LIST_INTERVAL_COUNT("report_definition.list.count", new RollingCounter()), + REPORT_DEFINITION_LIST_USER_ERROR("report_definition.list.user_error", new RollingCounter()), + REPORT_DEFINITION_LIST_SYSTEM_ERROR("report_definition.list.system_error", new RollingCounter()), + + REPORT_INSTANCE_UPDATE_TOTAL("report_instance.update.total", new BasicCounter()), + REPORT_INSTANCE_UPDATE_INTERVAL_COUNT("report_instance.update.count", new RollingCounter()), + REPORT_INSTANCE_UPDATE_USER_ERROR("report_instance.update.user_error", new RollingCounter()), + REPORT_INSTANCE_UPDATE_SYSTEM_ERROR("report_instance.update.system_error", new RollingCounter()), + + REPORT_INSTANCE_INFO_TOTAL("report_instance.info.total", new BasicCounter()), + REPORT_INSTANCE_INFO_INTERVAL_COUNT("report_instance.info.count", new RollingCounter()), + REPORT_INSTANCE_INFO_USER_ERROR("report_instance.info.user_error", new RollingCounter()), + REPORT_INSTANCE_INFO_SYSTEM_ERROR("report_instance.info.system_error", new RollingCounter()), + + REPORT_INSTANCE_LIST_TOTAL("report_instance.list.total", new BasicCounter()), + REPORT_INSTANCE_LIST_INTERVAL_COUNT("report_instance.list.count", new RollingCounter()), + REPORT_INSTANCE_LIST_USER_ERROR("report_instance.list.user_error", new RollingCounter()), + REPORT_INSTANCE_LIST_SYSTEM_ERROR("report_instance.list.system_error", new RollingCounter()), + + REPORT_FROM_DEFINITION_TOTAL("on_demand.create.total", new BasicCounter()), + REPORT_FROM_DEFINITION_INTERVAL_COUNT("on_demand.create.count", new RollingCounter()), + REPORT_FROM_DEFINITION_USER_ERROR("on_demand.create.user_error", new RollingCounter()), + REPORT_FROM_DEFINITION_SYSTEM_ERROR("on_demand.create.system_error", new RollingCounter()), + + REPORT_FROM_DEFINITION_ID_TOTAL("on_demand_from_definition.create.total", new BasicCounter()), + REPORT_FROM_DEFINITION_ID_INTERVAL_COUNT("on_demand_from_definition.create.count", new RollingCounter()), + REPORT_FROM_DEFINITION_ID_USER_ERROR("on_demand_from_definition.create.user_error", new RollingCounter()), + REPORT_FROM_DEFINITION_ID_SYSTEM_ERROR("on_demand_from_definition.create.system_error", new RollingCounter()), + + REPORT_SECURITY_PERMISSION_ERROR("es_security_permission_error", new RollingCounter()), + REPORT_PERMISSION_USER_ERROR("permission_user_error", new RollingCounter()); + + + private final String name; + private final Counter counter; + + MyMetrics(String name, Counter counter) { + this.name = name; + this.counter = counter; + } + + public String getName() { + return name; + } + + public Counter getCounter() { + return counter; + } + + private static final MyMetrics[] values = values(); + + //TODO: use json and remove org.json dependencies + public static String collectToJSON() { + JSONObject metricsJSONObject = new JSONObject(); + + for (MyMetrics metric: values) { + metricsJSONObject.put(metric.name, metric.counter.getValue()); + } + return metricsJSONObject.toString(); + } + + public static String collectToFlattenedJSON() { + return JsonUnflattener.unflatten(collectToJSON()); + } +} diff --git a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/ReportsSchedulerPlugin.kt b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/ReportsSchedulerPlugin.kt index 33082fd9..57cc98b9 100644 --- a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/ReportsSchedulerPlugin.kt +++ b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/ReportsSchedulerPlugin.kt @@ -32,7 +32,7 @@ import com.amazon.opendistroforelasticsearch.reportsscheduler.action.UpdateRepor import com.amazon.opendistroforelasticsearch.reportsscheduler.index.ReportDefinitionsIndex import com.amazon.opendistroforelasticsearch.reportsscheduler.index.ReportDefinitionsIndex.REPORT_DEFINITIONS_INDEX_NAME import com.amazon.opendistroforelasticsearch.reportsscheduler.index.ReportInstancesIndex -import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.Metrics +// import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.Metrics import com.amazon.opendistroforelasticsearch.reportsscheduler.resthandler.OnDemandReportRestHandler import com.amazon.opendistroforelasticsearch.reportsscheduler.resthandler.ReportDefinitionListRestHandler import com.amazon.opendistroforelasticsearch.reportsscheduler.resthandler.ReportDefinitionRestHandler @@ -150,9 +150,6 @@ class ReportsSchedulerPlugin : Plugin(), ActionPlugin, JobSchedulerExtension { indexNameExpressionResolver: IndexNameExpressionResolver, nodesInCluster: Supplier ): List { - - Metrics.getInstance().registerDefaultMetrics() - return listOf( ReportDefinitionRestHandler(), ReportDefinitionListRestHandler(), diff --git a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/action/PluginBaseAction.kt b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/action/PluginBaseAction.kt index 6fedfcf3..fab8cc78 100644 --- a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/action/PluginBaseAction.kt +++ b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/action/PluginBaseAction.kt @@ -18,9 +18,10 @@ package com.amazon.opendistroforelasticsearch.reportsscheduler.action import com.amazon.opendistroforelasticsearch.commons.ConfigConstants.OPENDISTRO_SECURITY_USER_INFO_THREAD_CONTEXT import com.amazon.opendistroforelasticsearch.commons.authuser.User -import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.Metrics -import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.MetricName +// import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.Metrics +// import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.MetricName import com.amazon.opendistroforelasticsearch.reportsscheduler.ReportsSchedulerPlugin.Companion.LOG_PREFIX +import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.MyMetrics import com.amazon.opendistroforelasticsearch.reportsscheduler.util.logger import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers @@ -67,45 +68,42 @@ abstract class PluginBaseAction RestChannelConsumer { - Metrics.getInstance().getNumericalMetric(MetricName.REPORT_FROM_DEFINITION_TOTAL).increment() - Metrics.getInstance().getNumericalMetric(MetricName.REPORT_FROM_DEFINITION_INTERVAL_COUNT).increment() + MyMetrics.REPORT_FROM_DEFINITION_TOTAL.counter.increment() + MyMetrics.REPORT_FROM_DEFINITION_INTERVAL_COUNT.counter.increment() client.execute(InContextReportCreateAction.ACTION_TYPE, InContextReportCreateRequest(request.contentParserNextToken()), RestResponseToXContentListener(it)) } POST -> RestChannelConsumer { - Metrics.getInstance().getNumericalMetric(MetricName.REPORT_FROM_DEFINITION_ID_TOTAL).increment() - Metrics.getInstance().getNumericalMetric(MetricName.REPORT_FROM_DEFINITION_ID_INTERVAL_COUNT).increment() + MyMetrics.REPORT_FROM_DEFINITION_ID_TOTAL.counter.increment() + MyMetrics.REPORT_FROM_DEFINITION_ID_INTERVAL_COUNT.counter.increment() client.execute(OnDemandReportCreateAction.ACTION_TYPE, OnDemandReportCreateRequest.parse(request.contentParserNextToken(), request.param(REPORT_DEFINITION_ID_FIELD)), RestResponseToXContentListener(it)) diff --git a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportDefinitionListRestHandler.kt b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportDefinitionListRestHandler.kt index 88dba442..7eff08da 100644 --- a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportDefinitionListRestHandler.kt +++ b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportDefinitionListRestHandler.kt @@ -18,8 +18,9 @@ package com.amazon.opendistroforelasticsearch.reportsscheduler.resthandler import com.amazon.opendistroforelasticsearch.reportsscheduler.ReportsSchedulerPlugin.Companion.BASE_REPORTS_URI import com.amazon.opendistroforelasticsearch.reportsscheduler.action.GetAllReportDefinitionsAction import com.amazon.opendistroforelasticsearch.reportsscheduler.action.ReportDefinitionActions -import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.Metrics -import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.MetricName +// import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.Metrics +// import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.MetricName +import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.MyMetrics import com.amazon.opendistroforelasticsearch.reportsscheduler.model.GetAllReportDefinitionsRequest import com.amazon.opendistroforelasticsearch.reportsscheduler.model.RestTag.FROM_INDEX_FIELD import com.amazon.opendistroforelasticsearch.reportsscheduler.model.RestTag.MAX_ITEMS_FIELD @@ -74,8 +75,8 @@ internal class ReportDefinitionListRestHandler : BaseRestHandler() { val maxItems = request.param(MAX_ITEMS_FIELD)?.toIntOrNull() ?: PluginSettings.defaultItemsQueryCount return when (request.method()) { GET -> RestChannelConsumer { - Metrics.getInstance().getNumericalMetric(MetricName.REPORT_DEFINITION_LIST_TOTAL).increment() - Metrics.getInstance().getNumericalMetric(MetricName.REPORT_DEFINITION_LIST_INTERVAL_COUNT).increment() + MyMetrics.REPORT_DEFINITION_LIST_TOTAL.counter.increment() + MyMetrics.REPORT_DEFINITION_LIST_INTERVAL_COUNT.counter.increment() client.execute(GetAllReportDefinitionsAction.ACTION_TYPE, GetAllReportDefinitionsRequest(from, maxItems), RestResponseToXContentListener(it)) diff --git a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportDefinitionRestHandler.kt b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportDefinitionRestHandler.kt index c43808ff..78ed2e43 100644 --- a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportDefinitionRestHandler.kt +++ b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportDefinitionRestHandler.kt @@ -21,8 +21,9 @@ import com.amazon.opendistroforelasticsearch.reportsscheduler.action.DeleteRepor import com.amazon.opendistroforelasticsearch.reportsscheduler.action.GetReportDefinitionAction import com.amazon.opendistroforelasticsearch.reportsscheduler.action.ReportDefinitionActions import com.amazon.opendistroforelasticsearch.reportsscheduler.action.UpdateReportDefinitionAction -import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.Metrics -import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.MetricName +// import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.Metrics +// import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.MetricName +import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.MyMetrics import com.amazon.opendistroforelasticsearch.reportsscheduler.model.CreateReportDefinitionRequest import com.amazon.opendistroforelasticsearch.reportsscheduler.model.DeleteReportDefinitionRequest import com.amazon.opendistroforelasticsearch.reportsscheduler.model.GetReportDefinitionRequest @@ -107,30 +108,30 @@ internal class ReportDefinitionRestHandler : BaseRestHandler() { override fun prepareRequest(request: RestRequest, client: NodeClient): RestChannelConsumer { return when (request.method()) { POST -> RestChannelConsumer { - Metrics.getInstance().getNumericalMetric(MetricName.REPORT_DEFINITION_CREATE_TOTAL).increment() - Metrics.getInstance().getNumericalMetric(MetricName.REPORT_DEFINITION_CREATE_INTERVAL_COUNT).increment() + MyMetrics.REPORT_DEFINITION_CREATE_TOTAL.counter.increment() + MyMetrics.REPORT_DEFINITION_CREATE_INTERVAL_COUNT.counter.increment() client.execute(CreateReportDefinitionAction.ACTION_TYPE, CreateReportDefinitionRequest(request.contentParserNextToken()), RestResponseToXContentListener(it)) } PUT -> RestChannelConsumer { - Metrics.getInstance().getNumericalMetric(MetricName.REPORT_DEFINITION_UPDATE_TOTAL).increment() - Metrics.getInstance().getNumericalMetric(MetricName.REPORT_DEFINITION_UPDATE_INTERVAL_COUNT).increment() + MyMetrics.REPORT_DEFINITION_UPDATE_TOTAL.counter.increment() + MyMetrics.REPORT_DEFINITION_UPDATE_INTERVAL_COUNT.counter.increment() client.execute( UpdateReportDefinitionAction.ACTION_TYPE, UpdateReportDefinitionRequest(request.contentParserNextToken(), request.param(REPORT_DEFINITION_ID_FIELD)), RestResponseToXContentListener(it)) } GET -> RestChannelConsumer { - Metrics.getInstance().getNumericalMetric(MetricName.REPORT_DEFINITION_INFO_TOTAL).increment() - Metrics.getInstance().getNumericalMetric(MetricName.REPORT_DEFINITION_INFO_INTERVAL_COUNT).increment() + MyMetrics.REPORT_DEFINITION_INFO_TOTAL.counter.increment() + MyMetrics.REPORT_DEFINITION_INFO_INTERVAL_COUNT.counter.increment() client.execute(GetReportDefinitionAction.ACTION_TYPE, GetReportDefinitionRequest(request.param(REPORT_DEFINITION_ID_FIELD)), RestResponseToXContentListener(it)) } DELETE -> RestChannelConsumer { - Metrics.getInstance().getNumericalMetric(MetricName.REPORT_DEFINITION_DELETE_TOTAL).increment() - Metrics.getInstance().getNumericalMetric(MetricName.REPORT_DEFINITION_DELETE_INTERVAL_COUNT).increment() + MyMetrics.REPORT_DEFINITION_DELETE_TOTAL.counter.increment() + MyMetrics.REPORT_DEFINITION_DELETE_INTERVAL_COUNT.counter.increment() client.execute(DeleteReportDefinitionAction.ACTION_TYPE, DeleteReportDefinitionRequest(request.param(REPORT_DEFINITION_ID_FIELD)), RestResponseToXContentListener(it)) diff --git a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportInstanceListRestHandler.kt b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportInstanceListRestHandler.kt index 0b12b606..cb613a0c 100644 --- a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportInstanceListRestHandler.kt +++ b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportInstanceListRestHandler.kt @@ -18,8 +18,9 @@ package com.amazon.opendistroforelasticsearch.reportsscheduler.resthandler import com.amazon.opendistroforelasticsearch.reportsscheduler.ReportsSchedulerPlugin.Companion.BASE_REPORTS_URI import com.amazon.opendistroforelasticsearch.reportsscheduler.action.GetAllReportInstancesAction import com.amazon.opendistroforelasticsearch.reportsscheduler.action.ReportInstanceActions -import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.Metrics -import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.MetricName +// import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.Metrics +// import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.MetricName +import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.MyMetrics import com.amazon.opendistroforelasticsearch.reportsscheduler.model.GetAllReportInstancesRequest import com.amazon.opendistroforelasticsearch.reportsscheduler.model.RestTag.FROM_INDEX_FIELD import com.amazon.opendistroforelasticsearch.reportsscheduler.model.RestTag.MAX_ITEMS_FIELD @@ -80,8 +81,8 @@ internal class ReportInstanceListRestHandler : BaseRestHandler() { val maxItems = request.param(MAX_ITEMS_FIELD)?.toIntOrNull() ?: PluginSettings.defaultItemsQueryCount return when (request.method()) { GET -> RestChannelConsumer { - Metrics.getInstance().getNumericalMetric(MetricName.REPORT_INSTANCE_LIST_TOTAL).increment() - Metrics.getInstance().getNumericalMetric(MetricName.REPORT_INSTANCE_LIST_INTERVAL_COUNT).increment() + MyMetrics.REPORT_INSTANCE_LIST_TOTAL.counter.increment() + MyMetrics.REPORT_INSTANCE_LIST_INTERVAL_COUNT.counter.increment() client.execute(GetAllReportInstancesAction.ACTION_TYPE, GetAllReportInstancesRequest(from, maxItems), RestResponseToXContentListener(it)) diff --git a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportInstanceRestHandler.kt b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportInstanceRestHandler.kt index 80638237..93967860 100644 --- a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportInstanceRestHandler.kt +++ b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportInstanceRestHandler.kt @@ -19,8 +19,9 @@ import com.amazon.opendistroforelasticsearch.reportsscheduler.ReportsSchedulerPl import com.amazon.opendistroforelasticsearch.reportsscheduler.action.GetReportInstanceAction import com.amazon.opendistroforelasticsearch.reportsscheduler.action.ReportInstanceActions import com.amazon.opendistroforelasticsearch.reportsscheduler.action.UpdateReportInstanceStatusAction -import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.Metrics -import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.MetricName +// import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.Metrics +// import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.MetricName +import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.MyMetrics import com.amazon.opendistroforelasticsearch.reportsscheduler.model.GetReportInstanceRequest import com.amazon.opendistroforelasticsearch.reportsscheduler.model.RestTag.REPORT_INSTANCE_ID_FIELD import com.amazon.opendistroforelasticsearch.reportsscheduler.model.UpdateReportInstanceStatusRequest @@ -88,15 +89,15 @@ internal class ReportInstanceRestHandler : BaseRestHandler() { val reportInstanceId = request.param(REPORT_INSTANCE_ID_FIELD) ?: throw IllegalArgumentException("Must specify id") return when (request.method()) { POST -> RestChannelConsumer { - Metrics.getInstance().getNumericalMetric(MetricName.REPORT_INSTANCE_UPDATE_TOTAL).increment() - Metrics.getInstance().getNumericalMetric(MetricName.REPORT_INSTANCE_UPDATE_INTERVAL_COUNT).increment() + MyMetrics.REPORT_INSTANCE_UPDATE_TOTAL.counter.increment() + MyMetrics.REPORT_INSTANCE_UPDATE_INTERVAL_COUNT.counter.increment() client.execute(UpdateReportInstanceStatusAction.ACTION_TYPE, UpdateReportInstanceStatusRequest.parse(request.contentParserNextToken(), reportInstanceId), RestResponseToXContentListener(it)) } GET -> RestChannelConsumer { - Metrics.getInstance().getNumericalMetric(MetricName.REPORT_INSTANCE_INFO_TOTAL).increment() - Metrics.getInstance().getNumericalMetric(MetricName.REPORT_INSTANCE_INFO_INTERVAL_COUNT).increment() + MyMetrics.REPORT_INSTANCE_INFO_TOTAL.counter.increment() + MyMetrics.REPORT_INSTANCE_INFO_INTERVAL_COUNT.counter.increment() client.execute(GetReportInstanceAction.ACTION_TYPE, GetReportInstanceRequest(reportInstanceId), RestResponseToXContentListener(it)) diff --git a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportStatsRestHandler.kt b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportStatsRestHandler.kt index 7318441c..dda3e9e8 100644 --- a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportStatsRestHandler.kt +++ b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportStatsRestHandler.kt @@ -16,7 +16,7 @@ package com.amazon.opendistroforelasticsearch.reportsscheduler.resthandler import com.amazon.opendistroforelasticsearch.reportsscheduler.ReportsSchedulerPlugin.Companion.BASE_REPORTS_URI -import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.Metrics +import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.MyMetrics import org.elasticsearch.client.node.NodeClient import org.elasticsearch.rest.BaseRestHandler import org.elasticsearch.rest.BaseRestHandler.RestChannelConsumer @@ -68,8 +68,10 @@ internal class ReportStatsRestHandler : BaseRestHandler() { */ override fun prepareRequest(request: RestRequest, client: NodeClient): RestChannelConsumer { return when (request.method()) { + // TODO: Wrap this API in TransportAction GET -> RestChannelConsumer { - it.sendResponse(BytesRestResponse(RestStatus.OK, Metrics.getInstance().collectToFlattenedJSON())) + // it.sendResponse(BytesRestResponse(RestStatus.OK, Metrics.getInstance().collectToFlattenedJSON())) + it.sendResponse(BytesRestResponse(RestStatus.OK, MyMetrics.collectToFlattenedJSON())) } else -> RestChannelConsumer { it.sendResponse(BytesRestResponse(RestStatus.METHOD_NOT_ALLOWED, "${request.method()} is not allowed")) diff --git a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/RestResponseToXContentListener.kt b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/RestResponseToXContentListener.kt index 07be8a32..d7f856d3 100644 --- a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/RestResponseToXContentListener.kt +++ b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/RestResponseToXContentListener.kt @@ -16,14 +16,14 @@ package com.amazon.opendistroforelasticsearch.reportsscheduler.resthandler -import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.Metrics -import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.MetricName -import com.amazon.opendistroforelasticsearch.reportsscheduler.model.RestTag.SUCCESS_END_STATUS_CODE -import com.amazon.opendistroforelasticsearch.reportsscheduler.model.RestTag.SUCCESS_START_STATUS_CODE -import com.amazon.opendistroforelasticsearch.reportsscheduler.model.RestTag.CLIENT_END_STATUS_CODE -import com.amazon.opendistroforelasticsearch.reportsscheduler.model.RestTag.CLIENT_START_STATUS_CODE +// import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.MetricName +// import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.Metrics +import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.MyMetrics import com.amazon.opendistroforelasticsearch.reportsscheduler.model.BaseResponse +import org.elasticsearch.common.xcontent.XContentBuilder +import org.elasticsearch.rest.BytesRestResponse import org.elasticsearch.rest.RestChannel +import org.elasticsearch.rest.RestResponse import org.elasticsearch.rest.RestStatus import org.elasticsearch.rest.action.RestToXContentListener @@ -32,19 +32,28 @@ import org.elasticsearch.rest.action.RestToXContentListener * {@link ToXContent} and automatically builds an XContent based response * (wrapping the toXContent in startObject/endObject). */ -internal class RestResponseToXContentListener(channel: RestChannel) : RestToXContentListener(channel) { +internal class RestResponseToXContentListener(channel: RestChannel) : RestToXContentListener( + channel +) { + override fun buildResponse(response: Response, builder: XContentBuilder?): RestResponse? { + super.buildResponse(response, builder) + + MyMetrics.REQUEST_TOTAL.counter.increment() + MyMetrics.REQUEST_INTERVAL_COUNT.counter.increment() + + when (response.getStatus()) { + in RestStatus.OK..RestStatus.MULTI_STATUS -> MyMetrics.REQUEST_SUCCESS.counter.increment() + RestStatus.FORBIDDEN -> MyMetrics.REPORT_SECURITY_PERMISSION_ERROR.counter.increment() + in RestStatus.UNAUTHORIZED..RestStatus.TOO_MANY_REQUESTS -> MyMetrics.REQUEST_USER_ERROR.counter.increment() + else -> MyMetrics.REQUEST_SYSTEM_ERROR.counter.increment() + } + return BytesRestResponse(getStatus(response), builder) + } + /** * {@inheritDoc} */ override fun getStatus(response: Response): RestStatus { - val restStatus = response.getStatus() - when (restStatus.status) { - in SUCCESS_START_STATUS_CODE..SUCCESS_END_STATUS_CODE -> Metrics.getInstance().getNumericalMetric(MetricName.REQUEST_SUCCESS).increment() - RestStatus.FORBIDDEN.status -> Metrics.getInstance().getNumericalMetric(MetricName.REPORT_SECURITY_PERMISSION_ERROR).increment() - in CLIENT_START_STATUS_CODE..CLIENT_END_STATUS_CODE -> Metrics.getInstance().getNumericalMetric(MetricName.REQUEST_USER_ERROR).increment() - else -> Metrics.getInstance().getNumericalMetric(MetricName.REQUEST_SYSTEM_ERROR).increment() - } - - return restStatus + return response.getStatus() } } diff --git a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/security/UserAccessManager.kt b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/security/UserAccessManager.kt index 40f7f4ea..acec2fbe 100644 --- a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/security/UserAccessManager.kt +++ b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/security/UserAccessManager.kt @@ -17,8 +17,9 @@ package com.amazon.opendistroforelasticsearch.reportsscheduler.security import com.amazon.opendistroforelasticsearch.commons.authuser.User -import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.Metrics -import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.MetricName +// import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.Metrics +// import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.MetricName +import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.MyMetrics import com.amazon.opendistroforelasticsearch.reportsscheduler.settings.PluginSettings import com.amazon.opendistroforelasticsearch.reportsscheduler.settings.PluginSettings.FilterBy import org.elasticsearch.ElasticsearchStatusException @@ -50,7 +51,7 @@ internal object UserAccessManager { */ fun validateUser(user: User?) { if (isUserPrivateTenant(user) && user?.name == null) { - Metrics.getInstance().getNumericalMetric(MetricName.REPORT_PERMISSION_USER_ERROR).increment() + MyMetrics.REPORT_PERMISSION_USER_ERROR.counter.increment() throw ElasticsearchStatusException("User name not provided for private tenant access", RestStatus.FORBIDDEN) } @@ -60,25 +61,25 @@ internal object UserAccessManager { FilterBy.User -> { // User name must be present user?.name ?: run { - Metrics.getInstance().getNumericalMetric(MetricName.REPORT_PERMISSION_USER_ERROR).increment() + MyMetrics.REPORT_PERMISSION_USER_ERROR.counter.increment() throw ElasticsearchStatusException("Filter-by enabled with security disabled", RestStatus.FORBIDDEN) } } FilterBy.Roles -> { // backend roles must be present if (user == null || user.roles.isNullOrEmpty()) { - Metrics.getInstance().getNumericalMetric(MetricName.REPORT_PERMISSION_USER_ERROR).increment() + MyMetrics.REPORT_PERMISSION_USER_ERROR.counter.increment() throw ElasticsearchStatusException("User doesn't have roles configured. Contact administrator.", RestStatus.FORBIDDEN) } else if (user.roles.stream().filter { !PluginSettings.ignoredRoles.contains(it) }.count() == 0L) { - Metrics.getInstance().getNumericalMetric(MetricName.REPORT_PERMISSION_USER_ERROR).increment() + MyMetrics.REPORT_PERMISSION_USER_ERROR.counter.increment() throw ElasticsearchStatusException("No distinguishing roles configured. Contact administrator.", RestStatus.FORBIDDEN) } } FilterBy.BackendRoles -> { // backend roles must be present if (user?.backendRoles.isNullOrEmpty()) { - Metrics.getInstance().getNumericalMetric(MetricName.REPORT_PERMISSION_USER_ERROR).increment() + MyMetrics.REPORT_PERMISSION_USER_ERROR.counter.increment() throw ElasticsearchStatusException("User doesn't have backend roles configured. Contact administrator.", RestStatus.FORBIDDEN) } @@ -92,7 +93,7 @@ internal object UserAccessManager { fun validatePollingUser(user: User?) { if (user != null) { // Check only if security is enabled if (user.name != KIBANA_SERVER_USER) { - Metrics.getInstance().getNumericalMetric(MetricName.REPORT_PERMISSION_USER_ERROR).increment() + MyMetrics.REPORT_PERMISSION_USER_ERROR.counter.increment() throw ElasticsearchStatusException("Permission denied", RestStatus.FORBIDDEN) } } From 6f47ecbb75b6ad1b68679787c6ed160a230a22ed Mon Sep 17 00:00:00 2001 From: Abbas Hussain Date: Tue, 5 Jan 2021 19:44:08 -0800 Subject: [PATCH 22/25] Address comments --- .../reportsscheduler/metrics/Metric.java | 34 ---- .../metrics/MetricFactory.java | 98 ---------- .../reportsscheduler/metrics/MetricName.java | 115 ------------ .../reportsscheduler/metrics/Metrics.java | 177 +++++++++++------- .../reportsscheduler/metrics/MyMetrics.java | 132 ------------- .../metrics/NumericMetric.java | 52 ----- .../ReportsSchedulerPlugin.kt | 1 - .../action/PluginBaseAction.kt | 22 +-- .../action/ReportDefinitionActions.kt | 20 +- .../action/ReportInstanceActions.kt | 24 ++- .../index/ReportDefinitionsIndex.kt | 14 +- .../model/CreateReportDefinitionRequest.kt | 6 +- .../model/CreateReportDefinitionResponse.kt | 6 +- .../model/DeleteReportDefinitionRequest.kt | 6 +- .../model/DeleteReportDefinitionResponse.kt | 6 +- .../model/GetAllReportDefinitionsRequest.kt | 6 +- .../model/GetAllReportInstancesRequest.kt | 6 +- .../model/GetReportDefinitionRequest.kt | 6 +- .../model/GetReportDefinitionResponse.kt | 6 +- .../model/GetReportInstanceRequest.kt | 6 +- .../model/InContextReportCreateRequest.kt | 10 +- .../model/OnDemandReportCreateRequest.kt | 6 +- .../reportsscheduler/model/RestTag.kt | 4 - .../model/UpdateReportDefinitionRequest.kt | 8 +- .../model/UpdateReportDefinitionResponse.kt | 6 +- .../UpdateReportInstanceStatusRequest.kt | 8 +- .../resthandler/OnDemandReportRestHandler.kt | 17 +- .../resthandler/PluginBaseHandler.kt | 36 ++++ .../ReportDefinitionListRestHandler.kt | 13 +- .../ReportDefinitionRestHandler.kt | 25 ++- .../ReportInstanceListRestHandler.kt | 13 +- .../ReportInstancePollRestHandler.kt | 5 +- .../resthandler/ReportInstanceRestHandler.kt | 17 +- .../resthandler/ReportStatsRestHandler.kt | 8 +- .../RestResponseToXContentListener.kt | 16 +- .../security/UserAccessManager.kt | 16 +- .../reportsscheduler/metrics/MetricsTest.java | 78 -------- .../metrics/NumericMetricTest.java | 43 ----- 38 files changed, 264 insertions(+), 808 deletions(-) delete mode 100644 reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Metric.java delete mode 100644 reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricFactory.java delete mode 100644 reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricName.java delete mode 100644 reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MyMetrics.java delete mode 100644 reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/NumericMetric.java create mode 100644 reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/PluginBaseHandler.kt delete mode 100644 reports-scheduler/src/test/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricsTest.java delete mode 100644 reports-scheduler/src/test/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/NumericMetricTest.java diff --git a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Metric.java b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Metric.java deleted file mode 100644 index 32f1833a..00000000 --- a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Metric.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * or in the "license" file accompanying this file. This file 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.amazon.opendistroforelasticsearch.reportsscheduler.metrics; - -import org.jetbrains.annotations.NotNull; - -public abstract class Metric { - - @NotNull - private final String name; - - public Metric(@NotNull String name) { - this.name = name; - } - - public String getName() { - return name; - } - - public abstract T getValue(); -} diff --git a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricFactory.java b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricFactory.java deleted file mode 100644 index 2feff031..00000000 --- a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricFactory.java +++ /dev/null @@ -1,98 +0,0 @@ -/* - * Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * or in the "license" file accompanying this file. This file 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.amazon.opendistroforelasticsearch.reportsscheduler.metrics; - -public class MetricFactory { - public static Metric createMetric(MetricName name) { - - switch (name) { - - case REQUEST_TOTAL: - case REPORT_DEFINITION_CREATE_TOTAL: - case REPORT_DEFINITION_UPDATE_TOTAL: - case REPORT_DEFINITION_INFO_TOTAL: - case REPORT_DEFINITION_DELETE_TOTAL: - case REPORT_DEFINITION_LIST_TOTAL: - case REPORT_INSTANCE_UPDATE_TOTAL: - case REPORT_INSTANCE_INFO_TOTAL: - case REPORT_INSTANCE_LIST_TOTAL: - case REPORT_FROM_DEFINITION_TOTAL: - case REPORT_FROM_DEFINITION_ID_TOTAL: - case DEFAULT: - return new NumericMetric<>(name.getName(), new BasicCounter()); - - case REQUEST_INTERVAL_COUNT: - case REQUEST_SUCCESS: - case REQUEST_USER_ERROR: - case REQUEST_SYSTEM_ERROR: - case REPORT_EXCEPTIONS_ES_STATUS_EXCEPTION: - case REPORT_EXCEPTIONS_ES_SECURITY_EXCEPTION: - case REPORT_EXCEPTIONS_VERSION_CONFLICT_ENGINE_EXCEPTION: - case REPORT_EXCEPTIONS_INDEX_NOT_FOUND_EXCEPTION: - case REPORT_EXCEPTIONS_INVALID_INDEX_NAME_EXCEPTION: - case REPORT_EXCEPTIONS_ILLEGAL_ARGUMENT_EXCEPTION: - case REPORT_EXCEPTIONS_ILLEGAL_STATE_EXCEPTION: - case REPORT_EXCEPTIONS_IO_EXCEPTION: - case REPORT_EXCEPTIONS_INTERNAL_SERVER_ERROR: - - case REPORT_DEFINITION_CREATE_INTERVAL_COUNT: - case REPORT_DEFINITION_CREATE_USER_ERROR: - case REPORT_DEFINITION_CREATE_SYSTEM_ERROR: - - case REPORT_DEFINITION_UPDATE_INTERVAL_COUNT: - case REPORT_DEFINITION_UPDATE_USER_ERROR: - case REPORT_DEFINITION_UPDATE_SYSTEM_ERROR: - - case REPORT_DEFINITION_INFO_INTERVAL_COUNT: - case REPORT_DEFINITION_INFO_USER_ERROR: - case REPORT_DEFINITION_INFO_SYSTEM_ERROR: - - case REPORT_DEFINITION_DELETE_INTERVAL_COUNT: - case REPORT_DEFINITION_DELETE_USER_ERROR: - case REPORT_DEFINITION_DELETE_SYSTEM_ERROR: - - case REPORT_DEFINITION_LIST_INTERVAL_COUNT: - case REPORT_DEFINITION_LIST_USER_ERROR: - case REPORT_DEFINITION_LIST_SYSTEM_ERROR: - - case REPORT_INSTANCE_UPDATE_INTERVAL_COUNT: - case REPORT_INSTANCE_UPDATE_USER_ERROR: - case REPORT_INSTANCE_UPDATE_SYSTEM_ERROR: - - case REPORT_INSTANCE_INFO_INTERVAL_COUNT: - case REPORT_INSTANCE_INFO_USER_ERROR: - case REPORT_INSTANCE_INFO_SYSTEM_ERROR: - - case REPORT_INSTANCE_LIST_INTERVAL_COUNT: - case REPORT_INSTANCE_LIST_USER_ERROR: - case REPORT_INSTANCE_LIST_SYSTEM_ERROR: - - case REPORT_FROM_DEFINITION_INTERVAL_COUNT: - case REPORT_FROM_DEFINITION_USER_ERROR: - case REPORT_FROM_DEFINITION_SYSTEM_ERROR: - - case REPORT_FROM_DEFINITION_ID_INTERVAL_COUNT: - case REPORT_FROM_DEFINITION_ID_USER_ERROR: - case REPORT_FROM_DEFINITION_ID_SYSTEM_ERROR: - - case REPORT_SECURITY_PERMISSION_ERROR: - case REPORT_PERMISSION_USER_ERROR: - return new NumericMetric<>(name.getName(), new RollingCounter()); - default: - return new NumericMetric<>(name.getName(), new BasicCounter()); - } - } -} diff --git a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricName.java b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricName.java deleted file mode 100644 index e2cfa620..00000000 --- a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricName.java +++ /dev/null @@ -1,115 +0,0 @@ -/* - * Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * or in the "license" file accompanying this file. This file 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.amazon.opendistroforelasticsearch.reportsscheduler.metrics; - -public enum MetricName { - - REQUEST_TOTAL("request_total", true), - REQUEST_INTERVAL_COUNT("request_count", true), - REQUEST_SUCCESS("success_count", true), - REQUEST_USER_ERROR("failed_request_count_user_error", true), - REQUEST_SYSTEM_ERROR("failed_request_count_system_error", true), - - /** - * Exceptions from: - * @see com.amazon.opendistroforelasticsearch.reportsscheduler.action.PluginBaseAction - */ - REPORT_EXCEPTIONS_ES_STATUS_EXCEPTION("exception.es_status", true), - REPORT_EXCEPTIONS_ES_SECURITY_EXCEPTION("exception.es_security", true), - REPORT_EXCEPTIONS_VERSION_CONFLICT_ENGINE_EXCEPTION("exception.version_conflict_engine", true), - REPORT_EXCEPTIONS_INDEX_NOT_FOUND_EXCEPTION("exception.index_not_found", true), - REPORT_EXCEPTIONS_INVALID_INDEX_NAME_EXCEPTION("exception.invalid_index_name", true), - REPORT_EXCEPTIONS_ILLEGAL_ARGUMENT_EXCEPTION("exception.illegal_argument", true), - REPORT_EXCEPTIONS_ILLEGAL_STATE_EXCEPTION("exception.illegal_state", true), - REPORT_EXCEPTIONS_IO_EXCEPTION("exception.io", true), - REPORT_EXCEPTIONS_INTERNAL_SERVER_ERROR("exception.internal_server_error", true), - - /** - * Per REST endpoint metrics - */ - REPORT_DEFINITION_CREATE_TOTAL("report_definition.create.total", true), - REPORT_DEFINITION_CREATE_INTERVAL_COUNT("report_definition.create.count", true), - REPORT_DEFINITION_CREATE_USER_ERROR("report_definition.create.user_error", true), - REPORT_DEFINITION_CREATE_SYSTEM_ERROR("report_definition.create.system_error", true), - - REPORT_DEFINITION_UPDATE_TOTAL("report_definition.update.total", true), - REPORT_DEFINITION_UPDATE_INTERVAL_COUNT("report_definition.update.count", true), - REPORT_DEFINITION_UPDATE_USER_ERROR("report_definition.update.user_error", true), - REPORT_DEFINITION_UPDATE_SYSTEM_ERROR("report_definition.update.system_error", true), - - REPORT_DEFINITION_INFO_TOTAL("report_definition.info.total", true), - REPORT_DEFINITION_INFO_INTERVAL_COUNT("report_definition.info.count", true), - REPORT_DEFINITION_INFO_USER_ERROR("report_definition.info.user_error", true), - REPORT_DEFINITION_INFO_SYSTEM_ERROR("report_definition.info.system_error", true), - - REPORT_DEFINITION_DELETE_TOTAL("report_definition.delete.total", true), - REPORT_DEFINITION_DELETE_INTERVAL_COUNT("report_definition.delete.count", true), - REPORT_DEFINITION_DELETE_USER_ERROR("report_definition.delete.user_error", true), - REPORT_DEFINITION_DELETE_SYSTEM_ERROR("report_definition.delete.system_error", true), - - REPORT_DEFINITION_LIST_TOTAL("report_definition.list.total", true), - REPORT_DEFINITION_LIST_INTERVAL_COUNT("report_definition.list.count", true), - REPORT_DEFINITION_LIST_USER_ERROR("report_definition.list.user_error", true), - REPORT_DEFINITION_LIST_SYSTEM_ERROR("report_definition.list.system_error", true), - - REPORT_INSTANCE_UPDATE_TOTAL("report_instance.update.total", true), - REPORT_INSTANCE_UPDATE_INTERVAL_COUNT("report_instance.update.count", true), - REPORT_INSTANCE_UPDATE_USER_ERROR("report_instance.update.user_error", true), - REPORT_INSTANCE_UPDATE_SYSTEM_ERROR("report_instance.update.system_error", true), - - REPORT_INSTANCE_INFO_TOTAL("report_instance.info.total", true), - REPORT_INSTANCE_INFO_INTERVAL_COUNT("report_instance.info.count", true), - REPORT_INSTANCE_INFO_USER_ERROR("report_instance.info.user_error", true), - REPORT_INSTANCE_INFO_SYSTEM_ERROR("report_instance.info.system_error", true), - - REPORT_INSTANCE_LIST_TOTAL("report_instance.list.total", true), - REPORT_INSTANCE_LIST_INTERVAL_COUNT("report_instance.list.count", true), - REPORT_INSTANCE_LIST_USER_ERROR("report_instance.list.user_error", true), - REPORT_INSTANCE_LIST_SYSTEM_ERROR("report_instance.list.system_error", true), - - REPORT_FROM_DEFINITION_TOTAL("on_demand.create.total", true), - REPORT_FROM_DEFINITION_INTERVAL_COUNT("on_demand.create.count", true), - REPORT_FROM_DEFINITION_USER_ERROR("on_demand.create.user_error", true), - REPORT_FROM_DEFINITION_SYSTEM_ERROR("on_demand.create.system_error", true), - - REPORT_FROM_DEFINITION_ID_TOTAL("on_demand_from_definition.create.total", true), - REPORT_FROM_DEFINITION_ID_INTERVAL_COUNT("on_demand_from_definition.create.count", true), - REPORT_FROM_DEFINITION_ID_USER_ERROR("on_demand_from_definition.create.user_error", true), - REPORT_FROM_DEFINITION_ID_SYSTEM_ERROR("on_demand_from_definition.create.system_error", true), - - REPORT_SECURITY_PERMISSION_ERROR("es_security_permission_error", true), - REPORT_PERMISSION_USER_ERROR("permission_user_error", true), - - DEFAULT("default", true); - - private final String name; - private final boolean numerical; - - MetricName(String name, boolean numerical) { - this.name = name; - this.numerical = numerical; - } - - public String getName() { - return name; - } - - public boolean isNumerical() { - return numerical; - } -} - - diff --git a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Metrics.java b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Metrics.java index a0893b56..1ae0489f 100644 --- a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Metrics.java +++ b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Metrics.java @@ -1,97 +1,130 @@ /* - * Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at + * Licensed under the Apache License, Version 2.0 (the "License"). + * You may not use this file except in compliance with the License. + * A copy of the License is located at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 + * + * or in the "license" file accompanying this file. This file 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. * - * or in the "license" file accompanying this file. This file 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.amazon.opendistroforelasticsearch.reportsscheduler.metrics; -import org.json.JSONObject; -import java.time.Clock; -import java.util.ArrayList; -import java.util.List; -import java.util.concurrent.ConcurrentHashMap; - import com.github.wnameless.json.unflattener.JsonUnflattener; +import org.json.JSONObject; -public class Metrics { - - private static Metrics metrics = new Metrics(); - private ConcurrentHashMap> registeredMetricsByName = new ConcurrentHashMap<>(); - private final Clock clock = Clock.systemDefaultZone(); - - private Metrics() { - } - - public void registerDefaultMetrics() { - for (MetricName metricName : MetricName.values()) { - registerMetric(MetricFactory.createMetric(metricName)); - } - } - - public void registerMetric(Metric metric) { - registeredMetricsByName.put(metric.getName(), metric); - } - - public void unregisterMetric(String name) { - if (name == null) { - return; - } - - registeredMetricsByName.remove(name); - } - - public Metric getMetric(String name) { - if (name == null) { - return null; - } - - return registeredMetricsByName.get(name); +public enum Metrics { + + REQUEST_TOTAL("request_total", new BasicCounter()), + REQUEST_INTERVAL_COUNT("request_count", new RollingCounter()), + REQUEST_SUCCESS("success_count", new RollingCounter()), + REQUEST_USER_ERROR("failed_request_count_user_error", new RollingCounter()), + REQUEST_SYSTEM_ERROR("failed_request_count_system_error", new RollingCounter()), + + /** + * Exceptions from: + * @see com.amazon.opendistroforelasticsearch.reportsscheduler.action.PluginBaseAction + */ + REPORT_EXCEPTIONS_ES_STATUS_EXCEPTION("exception.es_status", new RollingCounter()), + REPORT_EXCEPTIONS_ES_SECURITY_EXCEPTION("exception.es_security", new RollingCounter()), + REPORT_EXCEPTIONS_VERSION_CONFLICT_ENGINE_EXCEPTION("exception.version_conflict_engine", new RollingCounter()), + REPORT_EXCEPTIONS_INDEX_NOT_FOUND_EXCEPTION("exception.index_not_found", new RollingCounter()), + REPORT_EXCEPTIONS_INVALID_INDEX_NAME_EXCEPTION("exception.invalid_index_name", new RollingCounter()), + REPORT_EXCEPTIONS_ILLEGAL_ARGUMENT_EXCEPTION("exception.illegal_argument", new RollingCounter()), + REPORT_EXCEPTIONS_ILLEGAL_STATE_EXCEPTION("exception.illegal_state", new RollingCounter()), + REPORT_EXCEPTIONS_IO_EXCEPTION("exception.io", new RollingCounter()), + REPORT_EXCEPTIONS_INTERNAL_SERVER_ERROR("exception.internal_server_error", new RollingCounter()), + + /** + * Per REST endpoint metrics + */ + REPORT_DEFINITION_CREATE_TOTAL("report_definition.create.total", new BasicCounter()), + REPORT_DEFINITION_CREATE_INTERVAL_COUNT("report_definition.create.count", new RollingCounter()), + REPORT_DEFINITION_CREATE_USER_ERROR("report_definition.create.user_error", new RollingCounter()), + REPORT_DEFINITION_CREATE_SYSTEM_ERROR("report_definition.create.system_error", new RollingCounter()), + + REPORT_DEFINITION_UPDATE_TOTAL("report_definition.update.total", new BasicCounter()), + REPORT_DEFINITION_UPDATE_INTERVAL_COUNT("report_definition.update.count", new RollingCounter()), + REPORT_DEFINITION_UPDATE_USER_ERROR("report_definition.update.user_error", new RollingCounter()), + REPORT_DEFINITION_UPDATE_SYSTEM_ERROR("report_definition.update.system_error", new RollingCounter()), + + REPORT_DEFINITION_INFO_TOTAL("report_definition.info.total", new BasicCounter()), + REPORT_DEFINITION_INFO_INTERVAL_COUNT("report_definition.info.count", new RollingCounter()), + REPORT_DEFINITION_INFO_USER_ERROR("report_definition.info.user_error", new RollingCounter()), + REPORT_DEFINITION_INFO_SYSTEM_ERROR("report_definition.info.system_error", new RollingCounter()), + + REPORT_DEFINITION_DELETE_TOTAL("report_definition.delete.total", new BasicCounter()), + REPORT_DEFINITION_DELETE_INTERVAL_COUNT("report_definition.delete.count", new RollingCounter()), + REPORT_DEFINITION_DELETE_USER_ERROR("report_definition.delete.user_error", new RollingCounter()), + REPORT_DEFINITION_DELETE_SYSTEM_ERROR("report_definition.delete.system_error", new RollingCounter()), + + REPORT_DEFINITION_LIST_TOTAL("report_definition.list.total",new BasicCounter()), + REPORT_DEFINITION_LIST_INTERVAL_COUNT("report_definition.list.count", new RollingCounter()), + REPORT_DEFINITION_LIST_USER_ERROR("report_definition.list.user_error", new RollingCounter()), + REPORT_DEFINITION_LIST_SYSTEM_ERROR("report_definition.list.system_error", new RollingCounter()), + + REPORT_INSTANCE_UPDATE_TOTAL("report_instance.update.total", new BasicCounter()), + REPORT_INSTANCE_UPDATE_INTERVAL_COUNT("report_instance.update.count", new RollingCounter()), + REPORT_INSTANCE_UPDATE_USER_ERROR("report_instance.update.user_error", new RollingCounter()), + REPORT_INSTANCE_UPDATE_SYSTEM_ERROR("report_instance.update.system_error", new RollingCounter()), + + REPORT_INSTANCE_INFO_TOTAL("report_instance.info.total", new BasicCounter()), + REPORT_INSTANCE_INFO_INTERVAL_COUNT("report_instance.info.count", new RollingCounter()), + REPORT_INSTANCE_INFO_USER_ERROR("report_instance.info.user_error", new RollingCounter()), + REPORT_INSTANCE_INFO_SYSTEM_ERROR("report_instance.info.system_error", new RollingCounter()), + + REPORT_INSTANCE_LIST_TOTAL("report_instance.list.total", new BasicCounter()), + REPORT_INSTANCE_LIST_INTERVAL_COUNT("report_instance.list.count", new RollingCounter()), + REPORT_INSTANCE_LIST_USER_ERROR("report_instance.list.user_error", new RollingCounter()), + REPORT_INSTANCE_LIST_SYSTEM_ERROR("report_instance.list.system_error", new RollingCounter()), + + REPORT_FROM_DEFINITION_TOTAL("on_demand.create.total", new BasicCounter()), + REPORT_FROM_DEFINITION_INTERVAL_COUNT("on_demand.create.count", new RollingCounter()), + REPORT_FROM_DEFINITION_USER_ERROR("on_demand.create.user_error", new RollingCounter()), + REPORT_FROM_DEFINITION_SYSTEM_ERROR("on_demand.create.system_error", new RollingCounter()), + + REPORT_FROM_DEFINITION_ID_TOTAL("on_demand_from_definition.create.total", new BasicCounter()), + REPORT_FROM_DEFINITION_ID_INTERVAL_COUNT("on_demand_from_definition.create.count", new RollingCounter()), + REPORT_FROM_DEFINITION_ID_USER_ERROR("on_demand_from_definition.create.user_error", new RollingCounter()), + REPORT_FROM_DEFINITION_ID_SYSTEM_ERROR("on_demand_from_definition.create.system_error", new RollingCounter()), + + REPORT_SECURITY_PERMISSION_ERROR("es_security_permission_error", new RollingCounter()), + REPORT_PERMISSION_USER_ERROR("permission_user_error", new RollingCounter()); + + private final String name; + private final Counter counter; + + Metrics(String name, Counter counter) { + this.name = name; + this.counter = counter; } - public NumericMetric getNumericalMetric(MetricName metricName) { - String name = metricName.getName(); - if (!metricName.isNumerical()) { - name = MetricName.DEFAULT.getName(); - } - - return (NumericMetric) registeredMetricsByName.get(name); + public String getName() { + return name; } - public List> getAllMetrics() { - return new ArrayList<>(registeredMetricsByName.values()); + public Counter getCounter() { + return counter; } - public static Metrics getInstance() { - return metrics; - } + private static final Metrics[] values = values(); - public String collectToJSON() { + //TODO: see if we can use gson and remove org.json dependencies + public static String collectToJSON() { JSONObject metricsJSONObject = new JSONObject(); - - for (Metric metric : registeredMetricsByName.values()) { - if (metric.getName().equals("default")) { - continue; - } - metricsJSONObject.put(metric.getName(), metric.getValue()); + for (Metrics metric: values) { + metricsJSONObject.put(metric.name, metric.counter.getValue()); } return metricsJSONObject.toString(); } - public String collectToFlattenedJSON() { + public static String collectToFlattenedJSON() { return JsonUnflattener.unflatten(collectToJSON()); } - - public void clear() { - registeredMetricsByName.clear(); - } } diff --git a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MyMetrics.java b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MyMetrics.java deleted file mode 100644 index 6781c014..00000000 --- a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MyMetrics.java +++ /dev/null @@ -1,132 +0,0 @@ -/* - * Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * or in the "license" file accompanying this file. This file 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.amazon.opendistroforelasticsearch.reportsscheduler.metrics; - -import com.github.wnameless.json.unflattener.JsonUnflattener; -import org.json.JSONObject; - -public enum MyMetrics { - - REQUEST_TOTAL("request_total", new BasicCounter()), - REQUEST_INTERVAL_COUNT("request_count", new RollingCounter()), - REQUEST_SUCCESS("success_count", new RollingCounter()), - REQUEST_USER_ERROR("failed_request_count_user_error", new RollingCounter()), - REQUEST_SYSTEM_ERROR("failed_request_count_system_error", new RollingCounter()), - - /** - * Exceptions from: - * @see com.amazon.opendistroforelasticsearch.reportsscheduler.action.PluginBaseAction - */ - REPORT_EXCEPTIONS_ES_STATUS_EXCEPTION("exception.es_status", new RollingCounter()), - REPORT_EXCEPTIONS_ES_SECURITY_EXCEPTION("exception.es_security", new RollingCounter()), - REPORT_EXCEPTIONS_VERSION_CONFLICT_ENGINE_EXCEPTION("exception.version_conflict_engine", new RollingCounter()), - REPORT_EXCEPTIONS_INDEX_NOT_FOUND_EXCEPTION("exception.index_not_found", new RollingCounter()), - REPORT_EXCEPTIONS_INVALID_INDEX_NAME_EXCEPTION("exception.invalid_index_name", new RollingCounter()), - REPORT_EXCEPTIONS_ILLEGAL_ARGUMENT_EXCEPTION("exception.illegal_argument", new RollingCounter()), - REPORT_EXCEPTIONS_ILLEGAL_STATE_EXCEPTION("exception.illegal_state", new RollingCounter()), - REPORT_EXCEPTIONS_IO_EXCEPTION("exception.io", new RollingCounter()), - REPORT_EXCEPTIONS_INTERNAL_SERVER_ERROR("exception.internal_server_error", new RollingCounter()), - - /** - * Per REST endpoint metrics - */ - REPORT_DEFINITION_CREATE_TOTAL("report_definition.create.total", new BasicCounter()), - REPORT_DEFINITION_CREATE_INTERVAL_COUNT("report_definition.create.count", new RollingCounter()), - REPORT_DEFINITION_CREATE_USER_ERROR("report_definition.create.user_error", new RollingCounter()), - REPORT_DEFINITION_CREATE_SYSTEM_ERROR("report_definition.create.system_error", new RollingCounter()), - - REPORT_DEFINITION_UPDATE_TOTAL("report_definition.update.total", new BasicCounter()), - REPORT_DEFINITION_UPDATE_INTERVAL_COUNT("report_definition.update.count", new RollingCounter()), - REPORT_DEFINITION_UPDATE_USER_ERROR("report_definition.update.user_error", new RollingCounter()), - REPORT_DEFINITION_UPDATE_SYSTEM_ERROR("report_definition.update.system_error", new RollingCounter()), - - REPORT_DEFINITION_INFO_TOTAL("report_definition.info.total", new BasicCounter()), - REPORT_DEFINITION_INFO_INTERVAL_COUNT("report_definition.info.count", new RollingCounter()), - REPORT_DEFINITION_INFO_USER_ERROR("report_definition.info.user_error", new RollingCounter()), - REPORT_DEFINITION_INFO_SYSTEM_ERROR("report_definition.info.system_error", new RollingCounter()), - - REPORT_DEFINITION_DELETE_TOTAL("report_definition.delete.total", new BasicCounter()), - REPORT_DEFINITION_DELETE_INTERVAL_COUNT("report_definition.delete.count", new RollingCounter()), - REPORT_DEFINITION_DELETE_USER_ERROR("report_definition.delete.user_error", new RollingCounter()), - REPORT_DEFINITION_DELETE_SYSTEM_ERROR("report_definition.delete.system_error", new RollingCounter()), - - REPORT_DEFINITION_LIST_TOTAL("report_definition.list.total",new BasicCounter()), - REPORT_DEFINITION_LIST_INTERVAL_COUNT("report_definition.list.count", new RollingCounter()), - REPORT_DEFINITION_LIST_USER_ERROR("report_definition.list.user_error", new RollingCounter()), - REPORT_DEFINITION_LIST_SYSTEM_ERROR("report_definition.list.system_error", new RollingCounter()), - - REPORT_INSTANCE_UPDATE_TOTAL("report_instance.update.total", new BasicCounter()), - REPORT_INSTANCE_UPDATE_INTERVAL_COUNT("report_instance.update.count", new RollingCounter()), - REPORT_INSTANCE_UPDATE_USER_ERROR("report_instance.update.user_error", new RollingCounter()), - REPORT_INSTANCE_UPDATE_SYSTEM_ERROR("report_instance.update.system_error", new RollingCounter()), - - REPORT_INSTANCE_INFO_TOTAL("report_instance.info.total", new BasicCounter()), - REPORT_INSTANCE_INFO_INTERVAL_COUNT("report_instance.info.count", new RollingCounter()), - REPORT_INSTANCE_INFO_USER_ERROR("report_instance.info.user_error", new RollingCounter()), - REPORT_INSTANCE_INFO_SYSTEM_ERROR("report_instance.info.system_error", new RollingCounter()), - - REPORT_INSTANCE_LIST_TOTAL("report_instance.list.total", new BasicCounter()), - REPORT_INSTANCE_LIST_INTERVAL_COUNT("report_instance.list.count", new RollingCounter()), - REPORT_INSTANCE_LIST_USER_ERROR("report_instance.list.user_error", new RollingCounter()), - REPORT_INSTANCE_LIST_SYSTEM_ERROR("report_instance.list.system_error", new RollingCounter()), - - REPORT_FROM_DEFINITION_TOTAL("on_demand.create.total", new BasicCounter()), - REPORT_FROM_DEFINITION_INTERVAL_COUNT("on_demand.create.count", new RollingCounter()), - REPORT_FROM_DEFINITION_USER_ERROR("on_demand.create.user_error", new RollingCounter()), - REPORT_FROM_DEFINITION_SYSTEM_ERROR("on_demand.create.system_error", new RollingCounter()), - - REPORT_FROM_DEFINITION_ID_TOTAL("on_demand_from_definition.create.total", new BasicCounter()), - REPORT_FROM_DEFINITION_ID_INTERVAL_COUNT("on_demand_from_definition.create.count", new RollingCounter()), - REPORT_FROM_DEFINITION_ID_USER_ERROR("on_demand_from_definition.create.user_error", new RollingCounter()), - REPORT_FROM_DEFINITION_ID_SYSTEM_ERROR("on_demand_from_definition.create.system_error", new RollingCounter()), - - REPORT_SECURITY_PERMISSION_ERROR("es_security_permission_error", new RollingCounter()), - REPORT_PERMISSION_USER_ERROR("permission_user_error", new RollingCounter()); - - - private final String name; - private final Counter counter; - - MyMetrics(String name, Counter counter) { - this.name = name; - this.counter = counter; - } - - public String getName() { - return name; - } - - public Counter getCounter() { - return counter; - } - - private static final MyMetrics[] values = values(); - - //TODO: use json and remove org.json dependencies - public static String collectToJSON() { - JSONObject metricsJSONObject = new JSONObject(); - - for (MyMetrics metric: values) { - metricsJSONObject.put(metric.name, metric.counter.getValue()); - } - return metricsJSONObject.toString(); - } - - public static String collectToFlattenedJSON() { - return JsonUnflattener.unflatten(collectToJSON()); - } -} diff --git a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/NumericMetric.java b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/NumericMetric.java deleted file mode 100644 index 4abc6e97..00000000 --- a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/NumericMetric.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * or in the "license" file accompanying this file. This file 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.amazon.opendistroforelasticsearch.reportsscheduler.metrics; - -import org.jetbrains.annotations.NotNull; - -public class NumericMetric extends Metric { - @NotNull - private final Counter counter; - - public NumericMetric(@NotNull String name, @NotNull Counter counter) { - super(name); - this.counter = counter; - } - - public String getName() { - return super.getName(); - } - - public Counter getCounter() { - return counter; - } - - public void increment() { - counter.increment(); - } - - public void increment(long n) { - counter.add(n); - } - - public T getValue() { - return counter.getValue(); - } - - public void clear() { - counter.reset(); - } -} diff --git a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/ReportsSchedulerPlugin.kt b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/ReportsSchedulerPlugin.kt index 57cc98b9..bd33c9fd 100644 --- a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/ReportsSchedulerPlugin.kt +++ b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/ReportsSchedulerPlugin.kt @@ -32,7 +32,6 @@ import com.amazon.opendistroforelasticsearch.reportsscheduler.action.UpdateRepor import com.amazon.opendistroforelasticsearch.reportsscheduler.index.ReportDefinitionsIndex import com.amazon.opendistroforelasticsearch.reportsscheduler.index.ReportDefinitionsIndex.REPORT_DEFINITIONS_INDEX_NAME import com.amazon.opendistroforelasticsearch.reportsscheduler.index.ReportInstancesIndex -// import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.Metrics import com.amazon.opendistroforelasticsearch.reportsscheduler.resthandler.OnDemandReportRestHandler import com.amazon.opendistroforelasticsearch.reportsscheduler.resthandler.ReportDefinitionListRestHandler import com.amazon.opendistroforelasticsearch.reportsscheduler.resthandler.ReportDefinitionRestHandler diff --git a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/action/PluginBaseAction.kt b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/action/PluginBaseAction.kt index fab8cc78..2beab1f8 100644 --- a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/action/PluginBaseAction.kt +++ b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/action/PluginBaseAction.kt @@ -18,10 +18,8 @@ package com.amazon.opendistroforelasticsearch.reportsscheduler.action import com.amazon.opendistroforelasticsearch.commons.ConfigConstants.OPENDISTRO_SECURITY_USER_INFO_THREAD_CONTEXT import com.amazon.opendistroforelasticsearch.commons.authuser.User -// import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.Metrics -// import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.MetricName import com.amazon.opendistroforelasticsearch.reportsscheduler.ReportsSchedulerPlugin.Companion.LOG_PREFIX -import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.MyMetrics +import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.Metrics import com.amazon.opendistroforelasticsearch.reportsscheduler.util.logger import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers @@ -70,40 +68,40 @@ abstract class PluginBaseAction RestChannelConsumer { - MyMetrics.REPORT_FROM_DEFINITION_TOTAL.counter.increment() - MyMetrics.REPORT_FROM_DEFINITION_INTERVAL_COUNT.counter.increment() + Metrics.REPORT_FROM_DEFINITION_TOTAL.counter.increment() + Metrics.REPORT_FROM_DEFINITION_INTERVAL_COUNT.counter.increment() client.execute(InContextReportCreateAction.ACTION_TYPE, InContextReportCreateRequest(request.contentParserNextToken()), RestResponseToXContentListener(it)) } POST -> RestChannelConsumer { - MyMetrics.REPORT_FROM_DEFINITION_ID_TOTAL.counter.increment() - MyMetrics.REPORT_FROM_DEFINITION_ID_INTERVAL_COUNT.counter.increment() + Metrics.REPORT_FROM_DEFINITION_ID_TOTAL.counter.increment() + Metrics.REPORT_FROM_DEFINITION_ID_INTERVAL_COUNT.counter.increment() client.execute(OnDemandReportCreateAction.ACTION_TYPE, OnDemandReportCreateRequest.parse(request.contentParserNextToken(), request.param(REPORT_DEFINITION_ID_FIELD)), RestResponseToXContentListener(it)) diff --git a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/PluginBaseHandler.kt b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/PluginBaseHandler.kt new file mode 100644 index 00000000..0dc6c920 --- /dev/null +++ b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/PluginBaseHandler.kt @@ -0,0 +1,36 @@ +/* + * Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). + * You may not use this file except in compliance with the License. + * A copy of the License is located at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * or in the "license" file accompanying this file. This file 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.amazon.opendistroforelasticsearch.reportsscheduler.resthandler + +import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.Metrics +import org.elasticsearch.client.node.NodeClient +import org.elasticsearch.rest.BaseRestHandler +import org.elasticsearch.rest.RestRequest + +abstract class PluginBaseHandler : BaseRestHandler() { + + /** + * {@inheritDoc} + */ + override fun prepareRequest(request: RestRequest, client: NodeClient): RestChannelConsumer { + Metrics.REQUEST_TOTAL.counter.increment() + Metrics.REQUEST_INTERVAL_COUNT.counter.increment() + return executeRequest(request, client) + } + + protected abstract fun executeRequest(request: RestRequest, client: NodeClient): RestChannelConsumer +} diff --git a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportDefinitionListRestHandler.kt b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportDefinitionListRestHandler.kt index 7eff08da..60cb6ee6 100644 --- a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportDefinitionListRestHandler.kt +++ b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportDefinitionListRestHandler.kt @@ -18,16 +18,13 @@ package com.amazon.opendistroforelasticsearch.reportsscheduler.resthandler import com.amazon.opendistroforelasticsearch.reportsscheduler.ReportsSchedulerPlugin.Companion.BASE_REPORTS_URI import com.amazon.opendistroforelasticsearch.reportsscheduler.action.GetAllReportDefinitionsAction import com.amazon.opendistroforelasticsearch.reportsscheduler.action.ReportDefinitionActions -// import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.Metrics -// import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.MetricName -import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.MyMetrics +import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.Metrics import com.amazon.opendistroforelasticsearch.reportsscheduler.model.GetAllReportDefinitionsRequest import com.amazon.opendistroforelasticsearch.reportsscheduler.model.RestTag.FROM_INDEX_FIELD import com.amazon.opendistroforelasticsearch.reportsscheduler.model.RestTag.MAX_ITEMS_FIELD import com.amazon.opendistroforelasticsearch.reportsscheduler.settings.PluginSettings import org.elasticsearch.client.node.NodeClient -import org.elasticsearch.rest.BaseRestHandler import org.elasticsearch.rest.BaseRestHandler.RestChannelConsumer import org.elasticsearch.rest.BytesRestResponse import org.elasticsearch.rest.RestHandler.Route @@ -39,7 +36,7 @@ import org.elasticsearch.rest.RestStatus * Rest handler for getting list of report definitions. * This handler uses [ReportDefinitionActions]. */ -internal class ReportDefinitionListRestHandler : BaseRestHandler() { +internal class ReportDefinitionListRestHandler : PluginBaseHandler() { companion object { private const val REPORT_DEFINITION_LIST_ACTION = "report_definition_list_actions" private const val LIST_REPORT_DEFINITIONS_URL = "$BASE_REPORTS_URI/definitions" @@ -70,13 +67,13 @@ internal class ReportDefinitionListRestHandler : BaseRestHandler() { /** * {@inheritDoc} */ - override fun prepareRequest(request: RestRequest, client: NodeClient): RestChannelConsumer { + override fun executeRequest(request: RestRequest, client: NodeClient): RestChannelConsumer { val from = request.param(FROM_INDEX_FIELD)?.toIntOrNull() ?: 0 val maxItems = request.param(MAX_ITEMS_FIELD)?.toIntOrNull() ?: PluginSettings.defaultItemsQueryCount return when (request.method()) { GET -> RestChannelConsumer { - MyMetrics.REPORT_DEFINITION_LIST_TOTAL.counter.increment() - MyMetrics.REPORT_DEFINITION_LIST_INTERVAL_COUNT.counter.increment() + Metrics.REPORT_DEFINITION_LIST_TOTAL.counter.increment() + Metrics.REPORT_DEFINITION_LIST_INTERVAL_COUNT.counter.increment() client.execute(GetAllReportDefinitionsAction.ACTION_TYPE, GetAllReportDefinitionsRequest(from, maxItems), RestResponseToXContentListener(it)) diff --git a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportDefinitionRestHandler.kt b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportDefinitionRestHandler.kt index 78ed2e43..3821757f 100644 --- a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportDefinitionRestHandler.kt +++ b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportDefinitionRestHandler.kt @@ -21,9 +21,7 @@ import com.amazon.opendistroforelasticsearch.reportsscheduler.action.DeleteRepor import com.amazon.opendistroforelasticsearch.reportsscheduler.action.GetReportDefinitionAction import com.amazon.opendistroforelasticsearch.reportsscheduler.action.ReportDefinitionActions import com.amazon.opendistroforelasticsearch.reportsscheduler.action.UpdateReportDefinitionAction -// import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.Metrics -// import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.MetricName -import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.MyMetrics +import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.Metrics import com.amazon.opendistroforelasticsearch.reportsscheduler.model.CreateReportDefinitionRequest import com.amazon.opendistroforelasticsearch.reportsscheduler.model.DeleteReportDefinitionRequest import com.amazon.opendistroforelasticsearch.reportsscheduler.model.GetReportDefinitionRequest @@ -31,7 +29,6 @@ import com.amazon.opendistroforelasticsearch.reportsscheduler.model.RestTag.REPO import com.amazon.opendistroforelasticsearch.reportsscheduler.model.UpdateReportDefinitionRequest import com.amazon.opendistroforelasticsearch.reportsscheduler.util.contentParserNextToken import org.elasticsearch.client.node.NodeClient -import org.elasticsearch.rest.BaseRestHandler import org.elasticsearch.rest.BaseRestHandler.RestChannelConsumer import org.elasticsearch.rest.BytesRestResponse import org.elasticsearch.rest.RestHandler.Route @@ -46,7 +43,7 @@ import org.elasticsearch.rest.RestStatus * Rest handler for report definitions lifecycle management. * This handler uses [ReportDefinitionActions]. */ -internal class ReportDefinitionRestHandler : BaseRestHandler() { +internal class ReportDefinitionRestHandler : PluginBaseHandler() { companion object { private const val REPORT_DEFINITION_ACTION = "report_definition_actions" private const val REPORT_DEFINITION_URL = "$BASE_REPORTS_URI/definition" @@ -105,33 +102,33 @@ internal class ReportDefinitionRestHandler : BaseRestHandler() { /** * {@inheritDoc} */ - override fun prepareRequest(request: RestRequest, client: NodeClient): RestChannelConsumer { + override fun executeRequest(request: RestRequest, client: NodeClient): RestChannelConsumer { return when (request.method()) { POST -> RestChannelConsumer { - MyMetrics.REPORT_DEFINITION_CREATE_TOTAL.counter.increment() - MyMetrics.REPORT_DEFINITION_CREATE_INTERVAL_COUNT.counter.increment() + Metrics.REPORT_DEFINITION_CREATE_TOTAL.counter.increment() + Metrics.REPORT_DEFINITION_CREATE_INTERVAL_COUNT.counter.increment() client.execute(CreateReportDefinitionAction.ACTION_TYPE, CreateReportDefinitionRequest(request.contentParserNextToken()), RestResponseToXContentListener(it)) } PUT -> RestChannelConsumer { - MyMetrics.REPORT_DEFINITION_UPDATE_TOTAL.counter.increment() - MyMetrics.REPORT_DEFINITION_UPDATE_INTERVAL_COUNT.counter.increment() + Metrics.REPORT_DEFINITION_UPDATE_TOTAL.counter.increment() + Metrics.REPORT_DEFINITION_UPDATE_INTERVAL_COUNT.counter.increment() client.execute( UpdateReportDefinitionAction.ACTION_TYPE, UpdateReportDefinitionRequest(request.contentParserNextToken(), request.param(REPORT_DEFINITION_ID_FIELD)), RestResponseToXContentListener(it)) } GET -> RestChannelConsumer { - MyMetrics.REPORT_DEFINITION_INFO_TOTAL.counter.increment() - MyMetrics.REPORT_DEFINITION_INFO_INTERVAL_COUNT.counter.increment() + Metrics.REPORT_DEFINITION_INFO_TOTAL.counter.increment() + Metrics.REPORT_DEFINITION_INFO_INTERVAL_COUNT.counter.increment() client.execute(GetReportDefinitionAction.ACTION_TYPE, GetReportDefinitionRequest(request.param(REPORT_DEFINITION_ID_FIELD)), RestResponseToXContentListener(it)) } DELETE -> RestChannelConsumer { - MyMetrics.REPORT_DEFINITION_DELETE_TOTAL.counter.increment() - MyMetrics.REPORT_DEFINITION_DELETE_INTERVAL_COUNT.counter.increment() + Metrics.REPORT_DEFINITION_DELETE_TOTAL.counter.increment() + Metrics.REPORT_DEFINITION_DELETE_INTERVAL_COUNT.counter.increment() client.execute(DeleteReportDefinitionAction.ACTION_TYPE, DeleteReportDefinitionRequest(request.param(REPORT_DEFINITION_ID_FIELD)), RestResponseToXContentListener(it)) diff --git a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportInstanceListRestHandler.kt b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportInstanceListRestHandler.kt index cb613a0c..e82f137a 100644 --- a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportInstanceListRestHandler.kt +++ b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportInstanceListRestHandler.kt @@ -18,15 +18,12 @@ package com.amazon.opendistroforelasticsearch.reportsscheduler.resthandler import com.amazon.opendistroforelasticsearch.reportsscheduler.ReportsSchedulerPlugin.Companion.BASE_REPORTS_URI import com.amazon.opendistroforelasticsearch.reportsscheduler.action.GetAllReportInstancesAction import com.amazon.opendistroforelasticsearch.reportsscheduler.action.ReportInstanceActions -// import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.Metrics -// import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.MetricName -import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.MyMetrics +import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.Metrics import com.amazon.opendistroforelasticsearch.reportsscheduler.model.GetAllReportInstancesRequest import com.amazon.opendistroforelasticsearch.reportsscheduler.model.RestTag.FROM_INDEX_FIELD import com.amazon.opendistroforelasticsearch.reportsscheduler.model.RestTag.MAX_ITEMS_FIELD import com.amazon.opendistroforelasticsearch.reportsscheduler.settings.PluginSettings import org.elasticsearch.client.node.NodeClient -import org.elasticsearch.rest.BaseRestHandler import org.elasticsearch.rest.BaseRestHandler.RestChannelConsumer import org.elasticsearch.rest.BytesRestResponse import org.elasticsearch.rest.RestHandler.Route @@ -38,7 +35,7 @@ import org.elasticsearch.rest.RestStatus * Rest handler for getting list of report instances. * This handler uses [ReportInstanceActions]. */ -internal class ReportInstanceListRestHandler : BaseRestHandler() { +internal class ReportInstanceListRestHandler : PluginBaseHandler() { companion object { private const val REPORT_INSTANCE_LIST_ACTION = "report_instance_list_actions" private const val LIST_REPORT_INSTANCES_URL = "$BASE_REPORTS_URI/instances" @@ -76,13 +73,13 @@ internal class ReportInstanceListRestHandler : BaseRestHandler() { /** * {@inheritDoc} */ - override fun prepareRequest(request: RestRequest, client: NodeClient): RestChannelConsumer { + override fun executeRequest(request: RestRequest, client: NodeClient): RestChannelConsumer { val from = request.param(FROM_INDEX_FIELD)?.toIntOrNull() ?: 0 val maxItems = request.param(MAX_ITEMS_FIELD)?.toIntOrNull() ?: PluginSettings.defaultItemsQueryCount return when (request.method()) { GET -> RestChannelConsumer { - MyMetrics.REPORT_INSTANCE_LIST_TOTAL.counter.increment() - MyMetrics.REPORT_INSTANCE_LIST_INTERVAL_COUNT.counter.increment() + Metrics.REPORT_INSTANCE_LIST_TOTAL.counter.increment() + Metrics.REPORT_INSTANCE_LIST_INTERVAL_COUNT.counter.increment() client.execute(GetAllReportInstancesAction.ACTION_TYPE, GetAllReportInstancesRequest(from, maxItems), RestResponseToXContentListener(it)) diff --git a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportInstancePollRestHandler.kt b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportInstancePollRestHandler.kt index 73ddc15c..3669da4e 100644 --- a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportInstancePollRestHandler.kt +++ b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportInstancePollRestHandler.kt @@ -20,7 +20,6 @@ import com.amazon.opendistroforelasticsearch.reportsscheduler.action.PollReportI import com.amazon.opendistroforelasticsearch.reportsscheduler.action.ReportInstanceActions import com.amazon.opendistroforelasticsearch.reportsscheduler.model.PollReportInstanceRequest import org.elasticsearch.client.node.NodeClient -import org.elasticsearch.rest.BaseRestHandler import org.elasticsearch.rest.BaseRestHandler.RestChannelConsumer import org.elasticsearch.rest.BytesRestResponse import org.elasticsearch.rest.RestHandler.Route @@ -32,7 +31,7 @@ import org.elasticsearch.rest.RestStatus * Rest handler for getting list of report instances. * This handler uses [ReportInstanceActions]. */ -internal class ReportInstancePollRestHandler : BaseRestHandler() { +internal class ReportInstancePollRestHandler : PluginBaseHandler() { companion object { private const val REPORT_INSTANCE_POLL_ACTION = "report_instance_poll_actions" private const val POLL_REPORT_INSTANCE_URL = "$BASE_REPORTS_URI/poll_instance" @@ -70,7 +69,7 @@ internal class ReportInstancePollRestHandler : BaseRestHandler() { /** * {@inheritDoc} */ - override fun prepareRequest(request: RestRequest, client: NodeClient): RestChannelConsumer { + override fun executeRequest(request: RestRequest, client: NodeClient): RestChannelConsumer { return when (request.method()) { GET -> RestChannelConsumer { client.execute(PollReportInstanceAction.ACTION_TYPE, diff --git a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportInstanceRestHandler.kt b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportInstanceRestHandler.kt index 93967860..2eebb10e 100644 --- a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportInstanceRestHandler.kt +++ b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportInstanceRestHandler.kt @@ -19,15 +19,12 @@ import com.amazon.opendistroforelasticsearch.reportsscheduler.ReportsSchedulerPl import com.amazon.opendistroforelasticsearch.reportsscheduler.action.GetReportInstanceAction import com.amazon.opendistroforelasticsearch.reportsscheduler.action.ReportInstanceActions import com.amazon.opendistroforelasticsearch.reportsscheduler.action.UpdateReportInstanceStatusAction -// import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.Metrics -// import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.MetricName -import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.MyMetrics +import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.Metrics import com.amazon.opendistroforelasticsearch.reportsscheduler.model.GetReportInstanceRequest import com.amazon.opendistroforelasticsearch.reportsscheduler.model.RestTag.REPORT_INSTANCE_ID_FIELD import com.amazon.opendistroforelasticsearch.reportsscheduler.model.UpdateReportInstanceStatusRequest import com.amazon.opendistroforelasticsearch.reportsscheduler.util.contentParserNextToken import org.elasticsearch.client.node.NodeClient -import org.elasticsearch.rest.BaseRestHandler import org.elasticsearch.rest.BaseRestHandler.RestChannelConsumer import org.elasticsearch.rest.BytesRestResponse import org.elasticsearch.rest.RestHandler.Route @@ -40,7 +37,7 @@ import org.elasticsearch.rest.RestStatus * Rest handler for report instances lifecycle management. * This handler uses [ReportInstanceActions]. */ -internal class ReportInstanceRestHandler : BaseRestHandler() { +internal class ReportInstanceRestHandler : PluginBaseHandler() { companion object { private const val REPORT_INSTANCE_LIST_ACTION = "report_instance_actions" private const val REPORT_INSTANCE_URL = "$BASE_REPORTS_URI/instance" @@ -85,19 +82,19 @@ internal class ReportInstanceRestHandler : BaseRestHandler() { /** * {@inheritDoc} */ - override fun prepareRequest(request: RestRequest, client: NodeClient): RestChannelConsumer { + override fun executeRequest(request: RestRequest, client: NodeClient): RestChannelConsumer { val reportInstanceId = request.param(REPORT_INSTANCE_ID_FIELD) ?: throw IllegalArgumentException("Must specify id") return when (request.method()) { POST -> RestChannelConsumer { - MyMetrics.REPORT_INSTANCE_UPDATE_TOTAL.counter.increment() - MyMetrics.REPORT_INSTANCE_UPDATE_INTERVAL_COUNT.counter.increment() + Metrics.REPORT_INSTANCE_UPDATE_TOTAL.counter.increment() + Metrics.REPORT_INSTANCE_UPDATE_INTERVAL_COUNT.counter.increment() client.execute(UpdateReportInstanceStatusAction.ACTION_TYPE, UpdateReportInstanceStatusRequest.parse(request.contentParserNextToken(), reportInstanceId), RestResponseToXContentListener(it)) } GET -> RestChannelConsumer { - MyMetrics.REPORT_INSTANCE_INFO_TOTAL.counter.increment() - MyMetrics.REPORT_INSTANCE_INFO_INTERVAL_COUNT.counter.increment() + Metrics.REPORT_INSTANCE_INFO_TOTAL.counter.increment() + Metrics.REPORT_INSTANCE_INFO_INTERVAL_COUNT.counter.increment() client.execute(GetReportInstanceAction.ACTION_TYPE, GetReportInstanceRequest(reportInstanceId), RestResponseToXContentListener(it)) diff --git a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportStatsRestHandler.kt b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportStatsRestHandler.kt index dda3e9e8..91e2b834 100644 --- a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportStatsRestHandler.kt +++ b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/ReportStatsRestHandler.kt @@ -16,7 +16,7 @@ package com.amazon.opendistroforelasticsearch.reportsscheduler.resthandler import com.amazon.opendistroforelasticsearch.reportsscheduler.ReportsSchedulerPlugin.Companion.BASE_REPORTS_URI -import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.MyMetrics +import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.Metrics import org.elasticsearch.client.node.NodeClient import org.elasticsearch.rest.BaseRestHandler import org.elasticsearch.rest.BaseRestHandler.RestChannelConsumer @@ -50,7 +50,7 @@ internal class ReportStatsRestHandler : BaseRestHandler() { /** * Get reporting backend stats * Request URL: GET REPORT_STATS_URL - * Response body derived from: Ref [com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.MetricName] + * Response body derived from: Ref [com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.Metrics] */ Route(GET, "$REPORT_STATS_URL") ) @@ -68,10 +68,10 @@ internal class ReportStatsRestHandler : BaseRestHandler() { */ override fun prepareRequest(request: RestRequest, client: NodeClient): RestChannelConsumer { return when (request.method()) { - // TODO: Wrap this API in TransportAction + // TODO: Wrap this into TransportAction GET -> RestChannelConsumer { // it.sendResponse(BytesRestResponse(RestStatus.OK, Metrics.getInstance().collectToFlattenedJSON())) - it.sendResponse(BytesRestResponse(RestStatus.OK, MyMetrics.collectToFlattenedJSON())) + it.sendResponse(BytesRestResponse(RestStatus.OK, Metrics.collectToFlattenedJSON())) } else -> RestChannelConsumer { it.sendResponse(BytesRestResponse(RestStatus.METHOD_NOT_ALLOWED, "${request.method()} is not allowed")) diff --git a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/RestResponseToXContentListener.kt b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/RestResponseToXContentListener.kt index d7f856d3..c13c275d 100644 --- a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/RestResponseToXContentListener.kt +++ b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/resthandler/RestResponseToXContentListener.kt @@ -16,9 +16,7 @@ package com.amazon.opendistroforelasticsearch.reportsscheduler.resthandler -// import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.MetricName -// import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.Metrics -import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.MyMetrics +import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.Metrics import com.amazon.opendistroforelasticsearch.reportsscheduler.model.BaseResponse import org.elasticsearch.common.xcontent.XContentBuilder import org.elasticsearch.rest.BytesRestResponse @@ -38,14 +36,14 @@ internal class RestResponseToXContentListener(channel: override fun buildResponse(response: Response, builder: XContentBuilder?): RestResponse? { super.buildResponse(response, builder) - MyMetrics.REQUEST_TOTAL.counter.increment() - MyMetrics.REQUEST_INTERVAL_COUNT.counter.increment() + Metrics.REQUEST_TOTAL.counter.increment() + Metrics.REQUEST_INTERVAL_COUNT.counter.increment() when (response.getStatus()) { - in RestStatus.OK..RestStatus.MULTI_STATUS -> MyMetrics.REQUEST_SUCCESS.counter.increment() - RestStatus.FORBIDDEN -> MyMetrics.REPORT_SECURITY_PERMISSION_ERROR.counter.increment() - in RestStatus.UNAUTHORIZED..RestStatus.TOO_MANY_REQUESTS -> MyMetrics.REQUEST_USER_ERROR.counter.increment() - else -> MyMetrics.REQUEST_SYSTEM_ERROR.counter.increment() + in RestStatus.OK..RestStatus.MULTI_STATUS -> Metrics.REQUEST_SUCCESS.counter.increment() + RestStatus.FORBIDDEN -> Metrics.REPORT_SECURITY_PERMISSION_ERROR.counter.increment() + in RestStatus.UNAUTHORIZED..RestStatus.TOO_MANY_REQUESTS -> Metrics.REQUEST_USER_ERROR.counter.increment() + else -> Metrics.REQUEST_SYSTEM_ERROR.counter.increment() } return BytesRestResponse(getStatus(response), builder) } diff --git a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/security/UserAccessManager.kt b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/security/UserAccessManager.kt index acec2fbe..c8053d81 100644 --- a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/security/UserAccessManager.kt +++ b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/security/UserAccessManager.kt @@ -17,9 +17,7 @@ package com.amazon.opendistroforelasticsearch.reportsscheduler.security import com.amazon.opendistroforelasticsearch.commons.authuser.User -// import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.Metrics -// import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.MetricName -import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.MyMetrics +import com.amazon.opendistroforelasticsearch.reportsscheduler.metrics.Metrics import com.amazon.opendistroforelasticsearch.reportsscheduler.settings.PluginSettings import com.amazon.opendistroforelasticsearch.reportsscheduler.settings.PluginSettings.FilterBy import org.elasticsearch.ElasticsearchStatusException @@ -51,7 +49,7 @@ internal object UserAccessManager { */ fun validateUser(user: User?) { if (isUserPrivateTenant(user) && user?.name == null) { - MyMetrics.REPORT_PERMISSION_USER_ERROR.counter.increment() + Metrics.REPORT_PERMISSION_USER_ERROR.counter.increment() throw ElasticsearchStatusException("User name not provided for private tenant access", RestStatus.FORBIDDEN) } @@ -61,25 +59,25 @@ internal object UserAccessManager { FilterBy.User -> { // User name must be present user?.name ?: run { - MyMetrics.REPORT_PERMISSION_USER_ERROR.counter.increment() + Metrics.REPORT_PERMISSION_USER_ERROR.counter.increment() throw ElasticsearchStatusException("Filter-by enabled with security disabled", RestStatus.FORBIDDEN) } } FilterBy.Roles -> { // backend roles must be present if (user == null || user.roles.isNullOrEmpty()) { - MyMetrics.REPORT_PERMISSION_USER_ERROR.counter.increment() + Metrics.REPORT_PERMISSION_USER_ERROR.counter.increment() throw ElasticsearchStatusException("User doesn't have roles configured. Contact administrator.", RestStatus.FORBIDDEN) } else if (user.roles.stream().filter { !PluginSettings.ignoredRoles.contains(it) }.count() == 0L) { - MyMetrics.REPORT_PERMISSION_USER_ERROR.counter.increment() + Metrics.REPORT_PERMISSION_USER_ERROR.counter.increment() throw ElasticsearchStatusException("No distinguishing roles configured. Contact administrator.", RestStatus.FORBIDDEN) } } FilterBy.BackendRoles -> { // backend roles must be present if (user?.backendRoles.isNullOrEmpty()) { - MyMetrics.REPORT_PERMISSION_USER_ERROR.counter.increment() + Metrics.REPORT_PERMISSION_USER_ERROR.counter.increment() throw ElasticsearchStatusException("User doesn't have backend roles configured. Contact administrator.", RestStatus.FORBIDDEN) } @@ -93,7 +91,7 @@ internal object UserAccessManager { fun validatePollingUser(user: User?) { if (user != null) { // Check only if security is enabled if (user.name != KIBANA_SERVER_USER) { - MyMetrics.REPORT_PERMISSION_USER_ERROR.counter.increment() + Metrics.REPORT_PERMISSION_USER_ERROR.counter.increment() throw ElasticsearchStatusException("Permission denied", RestStatus.FORBIDDEN) } } diff --git a/reports-scheduler/src/test/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricsTest.java b/reports-scheduler/src/test/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricsTest.java deleted file mode 100644 index 58888a35..00000000 --- a/reports-scheduler/src/test/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/MetricsTest.java +++ /dev/null @@ -1,78 +0,0 @@ -/* - * Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * or in the "license" file accompanying this file. This file 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.amazon.opendistroforelasticsearch.reportsscheduler.metrics; - -import org.json.JSONObject; -import org.junit.Test; - -import java.util.List; - -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.notNullValue; -public class MetricsTest { - @Test - public void registerMetric() { - Metrics.getInstance().clear(); - Metrics.getInstance().registerMetric(new NumericMetric("test", new BasicCounter())); - - assertThat(Metrics.getInstance().getAllMetrics().size(), equalTo(1)); - } - - @Test - public void unRegisterMetric() { - Metrics.getInstance().clear(); - Metrics.getInstance().registerMetric(new NumericMetric("test1", new BasicCounter())); - Metrics.getInstance().registerMetric(new NumericMetric("test2", new BasicCounter())); - assertThat(Metrics.getInstance().getAllMetrics().size(), equalTo(2)); - - Metrics.getInstance().unregisterMetric("test2"); - assertThat(Metrics.getInstance().getAllMetrics().size(), equalTo(1)); - } - - @Test - public void getMetric() { - Metrics.getInstance().clear(); - Metrics.getInstance().registerMetric(new NumericMetric("test1", new BasicCounter())); - Metric metric = Metrics.getInstance().getMetric("test1"); - - assertThat(metric, notNullValue()); - } - - - @Test - public void getAllMetric() { - Metrics.getInstance().clear(); - Metrics.getInstance().registerMetric(new NumericMetric("test1", new BasicCounter())); - Metrics.getInstance().registerMetric(new NumericMetric("test2", new BasicCounter())); - List> list = Metrics.getInstance().getAllMetrics(); - - assertThat(list.size(), equalTo(2)); - } - - @Test - public void collectToJSON() { - Metrics.getInstance().clear(); - Metrics.getInstance().registerMetric(new NumericMetric("test1", new BasicCounter())); - Metrics.getInstance().registerMetric(new NumericMetric("test2", new BasicCounter())); - String res = Metrics.getInstance().collectToJSON(); - JSONObject jsonObject = new JSONObject(res); - - assertThat(jsonObject.getLong("test1"), equalTo(0L)); - assertThat(jsonObject.getInt("test2"), equalTo(0)); - } -} diff --git a/reports-scheduler/src/test/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/NumericMetricTest.java b/reports-scheduler/src/test/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/NumericMetricTest.java deleted file mode 100644 index bff37b5e..00000000 --- a/reports-scheduler/src/test/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/NumericMetricTest.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * or in the "license" file accompanying this file. This file 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.amazon.opendistroforelasticsearch.reportsscheduler.metrics; - -import org.junit.Test; - -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.equalTo; - -public class NumericMetricTest { - - @Test - public void increment() { - NumericMetric metric = new NumericMetric<>("test", new BasicCounter()); - for (int i=0; i<5; ++i) { - metric.increment(); - } - - assertThat(metric.getValue(), equalTo(5L)); - } - - @Test - public void add() { - NumericMetric metric = new NumericMetric<>("test", new BasicCounter()); - metric.increment(5); - - assertThat(metric.getValue(), equalTo(5L)); - } -} From 2da9ca54710d3e5358b55a86c7f969f7e91685b6 Mon Sep 17 00:00:00 2001 From: Abbas Hussain Date: Tue, 5 Jan 2021 20:55:02 -0800 Subject: [PATCH 23/25] Address comments --- .../reportsscheduler/metrics/Metrics.java | 72 +++++++++++++++---- .../action/ReportDefinitionActions.kt | 4 +- .../action/ReportInstanceActions.kt | 6 +- .../model/DeleteReportDefinitionRequest.kt | 2 +- .../model/GetAllReportDefinitionsRequest.kt | 2 +- .../model/GetAllReportInstancesRequest.kt | 2 +- .../model/GetReportDefinitionRequest.kt | 2 +- .../model/InContextReportCreateRequest.kt | 6 +- .../model/OnDemandReportCreateRequest.kt | 2 +- .../model/UpdateReportDefinitionRequest.kt | 4 +- .../model/UpdateReportDefinitionResponse.kt | 2 +- .../UpdateReportInstanceStatusRequest.kt | 4 +- 12 files changed, 78 insertions(+), 30 deletions(-) diff --git a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Metrics.java b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Metrics.java index 1ae0489f..46924035 100644 --- a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Metrics.java +++ b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Metrics.java @@ -41,59 +41,108 @@ public enum Metrics { REPORT_EXCEPTIONS_IO_EXCEPTION("exception.io", new RollingCounter()), REPORT_EXCEPTIONS_INTERNAL_SERVER_ERROR("exception.internal_server_error", new RollingCounter()), - /** - * Per REST endpoint metrics - */ + // ==== Per REST endpoint metrics ==== // + + // POST _opendistro/_reports/definition REPORT_DEFINITION_CREATE_TOTAL("report_definition.create.total", new BasicCounter()), REPORT_DEFINITION_CREATE_INTERVAL_COUNT("report_definition.create.count", new RollingCounter()), REPORT_DEFINITION_CREATE_USER_ERROR("report_definition.create.user_error", new RollingCounter()), REPORT_DEFINITION_CREATE_SYSTEM_ERROR("report_definition.create.system_error", new RollingCounter()), + + // PUT _opendistro/_reports/definition/{reportDefinitionId} REPORT_DEFINITION_UPDATE_TOTAL("report_definition.update.total", new BasicCounter()), REPORT_DEFINITION_UPDATE_INTERVAL_COUNT("report_definition.update.count", new RollingCounter()), - REPORT_DEFINITION_UPDATE_USER_ERROR("report_definition.update.user_error", new RollingCounter()), + REPORT_DEFINITION_UPDATE_USER_ERROR_MISSING_REPORT_DEF_DETAILS( + "report_definition.update.user_error.missing_report_def_details", new RollingCounter()), + REPORT_DEFINITION_UPDATE_USER_ERROR_INVALID_REPORT_DEF_ID( + "report_definition.update.user_error.invalid_report_def_id", new RollingCounter()), + REPORT_DEFINITION_UPDATE_USER_ERROR_INVALID_REPORT_DEF( + "report_definition.update.user_error.invalid_report_definition", new RollingCounter()), REPORT_DEFINITION_UPDATE_SYSTEM_ERROR("report_definition.update.system_error", new RollingCounter()), + + // GET _opendistro/_reports/definition/{reportDefinitionId} REPORT_DEFINITION_INFO_TOTAL("report_definition.info.total", new BasicCounter()), REPORT_DEFINITION_INFO_INTERVAL_COUNT("report_definition.info.count", new RollingCounter()), - REPORT_DEFINITION_INFO_USER_ERROR("report_definition.info.user_error", new RollingCounter()), + REPORT_DEFINITION_INFO_USER_ERROR_MISSING_REPORT_DEF_DETAILS( + "report_definition.info.user_error.missing_report_def_details", new RollingCounter()), + REPORT_DEFINITION_INFO_USER_ERROR_INVALID_REPORT_DEF_ID( + "report_definition.info.user_error.invalid_report_def_id", new RollingCounter()), REPORT_DEFINITION_INFO_SYSTEM_ERROR("report_definition.info.system_error", new RollingCounter()), + + // DELETE _opendistro/_reports/definition/{reportDefinitionId} REPORT_DEFINITION_DELETE_TOTAL("report_definition.delete.total", new BasicCounter()), REPORT_DEFINITION_DELETE_INTERVAL_COUNT("report_definition.delete.count", new RollingCounter()), - REPORT_DEFINITION_DELETE_USER_ERROR("report_definition.delete.user_error", new RollingCounter()), + REPORT_DEFINITION_DELETE_USER_ERROR_MISSING_REPORT_DEF_DETAILS( + "report_definition.delete.user_error.missing_report_def_details", new RollingCounter()), + REPORT_DEFINITION_DELETE_USER_ERROR_INVALID_REPORT_DEF_ID( + "report_definition.delete.user_error.invalid_report_def_id", new RollingCounter()), REPORT_DEFINITION_DELETE_SYSTEM_ERROR("report_definition.delete.system_error", new RollingCounter()), + + // GET _opendistro/_reports/definitions/[?[fromIndex=0]&[maxItems=100]] REPORT_DEFINITION_LIST_TOTAL("report_definition.list.total",new BasicCounter()), REPORT_DEFINITION_LIST_INTERVAL_COUNT("report_definition.list.count", new RollingCounter()), - REPORT_DEFINITION_LIST_USER_ERROR("report_definition.list.user_error", new RollingCounter()), + REPORT_DEFINITION_LIST_USER_ERROR_INVALID_FROM_INDEX( + "report_definition.list.user_error.invalid_from_index", new RollingCounter()), REPORT_DEFINITION_LIST_SYSTEM_ERROR("report_definition.list.system_error", new RollingCounter()), + + // POST _opendistro/_reports/instance/{reportInstanceId} REPORT_INSTANCE_UPDATE_TOTAL("report_instance.update.total", new BasicCounter()), REPORT_INSTANCE_UPDATE_INTERVAL_COUNT("report_instance.update.count", new RollingCounter()), - REPORT_INSTANCE_UPDATE_USER_ERROR("report_instance.update.user_error", new RollingCounter()), + REPORT_INSTANCE_UPDATE_USER_ERROR_MISSING_REPORT_INSTANCE( + "report_instance.update.user_error.missing_report_instance", new RollingCounter()), + REPORT_INSTANCE_UPDATE_USER_ERROR_INVALID_STATUS( + "report_instance.update.user_error.invalid_status", new RollingCounter()), + REPORT_INSTANCE_UPDATE_USER_ERROR_INVALID_REPORT_ID( + "report_instance.update.user_error.invalid_report_id", new RollingCounter()), REPORT_INSTANCE_UPDATE_SYSTEM_ERROR("report_instance.update.system_error", new RollingCounter()), + + // GET _opendistro/_reports/instance/{reportInstanceId} REPORT_INSTANCE_INFO_TOTAL("report_instance.info.total", new BasicCounter()), REPORT_INSTANCE_INFO_INTERVAL_COUNT("report_instance.info.count", new RollingCounter()), REPORT_INSTANCE_INFO_USER_ERROR("report_instance.info.user_error", new RollingCounter()), + REPORT_INSTANCE_INFO_USER_ERROR_MISSING_REPORT_INSTANCE( + "report_instance.info.user_error.missing_report_instance", + new RollingCounter() + ), + REPORT_INSTANCE_INFO_USER_ERROR_INVALID_REPORT_ID( + "report_instance.info.user_error.invalid_report_id", new RollingCounter()), REPORT_INSTANCE_INFO_SYSTEM_ERROR("report_instance.info.system_error", new RollingCounter()), + + // GET _opendistro/_reports/instances REPORT_INSTANCE_LIST_TOTAL("report_instance.list.total", new BasicCounter()), REPORT_INSTANCE_LIST_INTERVAL_COUNT("report_instance.list.count", new RollingCounter()), - REPORT_INSTANCE_LIST_USER_ERROR("report_instance.list.user_error", new RollingCounter()), + REPORT_INSTANCE_LIST_USER_ERROR_INVALID_FROM_INDEX( + "report_instance.list.user_error.invalid_from_index", new RollingCounter()), REPORT_INSTANCE_LIST_SYSTEM_ERROR("report_instance.list.system_error", new RollingCounter()), + + // PUT _opendistro/_reports/on_demand REPORT_FROM_DEFINITION_TOTAL("on_demand.create.total", new BasicCounter()), REPORT_FROM_DEFINITION_INTERVAL_COUNT("on_demand.create.count", new RollingCounter()), - REPORT_FROM_DEFINITION_USER_ERROR("on_demand.create.user_error", new RollingCounter()), + REPORT_FROM_DEFINITION_USER_ERROR_INVALID_BEGIN_TIME( + "on_demand.create.user_error.invalid_begin_time", new RollingCounter()), + REPORT_FROM_DEFINITION_USER_ERROR_INVALID_END_TIME( + "on_demand.create.user_error.invalid_end_time", new RollingCounter()), + REPORT_FROM_DEFINITION_USER_ERROR_INVALID_STATUS( + "on_demand.create.user_error.invalid_status", new RollingCounter()), REPORT_FROM_DEFINITION_SYSTEM_ERROR("on_demand.create.system_error", new RollingCounter()), + + // POST _opendistro/_reports/on_demand/{reportDefinitionId} REPORT_FROM_DEFINITION_ID_TOTAL("on_demand_from_definition.create.total", new BasicCounter()), REPORT_FROM_DEFINITION_ID_INTERVAL_COUNT("on_demand_from_definition.create.count", new RollingCounter()), - REPORT_FROM_DEFINITION_ID_USER_ERROR("on_demand_from_definition.create.user_error", new RollingCounter()), + REPORT_FROM_DEFINITION_ID_USER_ERROR_INVALID_REPORT_DEF_ID( + "on_demand_from_definition.create.user_error.invalid_report_def_id", new RollingCounter()), REPORT_FROM_DEFINITION_ID_SYSTEM_ERROR("on_demand_from_definition.create.system_error", new RollingCounter()), + REPORT_SECURITY_PERMISSION_ERROR("es_security_permission_error", new RollingCounter()), REPORT_PERMISSION_USER_ERROR("permission_user_error", new RollingCounter()); @@ -115,7 +164,6 @@ public Counter getCounter() { private static final Metrics[] values = values(); - //TODO: see if we can use gson and remove org.json dependencies public static String collectToJSON() { JSONObject metricsJSONObject = new JSONObject(); for (Metrics metric: values) { diff --git a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/action/ReportDefinitionActions.kt b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/action/ReportDefinitionActions.kt index 4f315c1c..a0f5bf49 100644 --- a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/action/ReportDefinitionActions.kt +++ b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/action/ReportDefinitionActions.kt @@ -76,7 +76,7 @@ internal object ReportDefinitionActions { val currentReportDefinitionDetails = ReportDefinitionsIndex.getReportDefinition(request.reportDefinitionId) currentReportDefinitionDetails ?: run { - Metrics.REPORT_DEFINITION_UPDATE_USER_ERROR.counter.increment() + Metrics.REPORT_DEFINITION_UPDATE_USER_ERROR_MISSING_REPORT_DEF_DETAILS.counter.increment() throw ElasticsearchStatusException("Report Definition ${request.reportDefinitionId} not found", RestStatus.NOT_FOUND) } @@ -132,7 +132,7 @@ internal object ReportDefinitionActions { val reportDefinitionDetails = ReportDefinitionsIndex.getReportDefinition(request.reportDefinitionId) reportDefinitionDetails ?: run { - Metrics.REPORT_DEFINITION_DELETE_USER_ERROR.counter.increment() + Metrics.REPORT_DEFINITION_DELETE_USER_ERROR_MISSING_REPORT_DEF_DETAILS.counter.increment() throw ElasticsearchStatusException("Report Definition ${request.reportDefinitionId} not found", RestStatus.NOT_FOUND) } diff --git a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/action/ReportInstanceActions.kt b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/action/ReportInstanceActions.kt index df76151b..57e2a240 100644 --- a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/action/ReportInstanceActions.kt +++ b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/action/ReportInstanceActions.kt @@ -89,7 +89,7 @@ internal object ReportInstanceActions { val reportDefinitionDetails = ReportDefinitionsIndex.getReportDefinition(request.reportDefinitionId) reportDefinitionDetails ?: run { - Metrics.REPORT_DEFINITION_INFO_USER_ERROR.counter.increment() + Metrics.REPORT_DEFINITION_INFO_USER_ERROR_MISSING_REPORT_DEF_DETAILS.counter.increment() throw ElasticsearchStatusException("Report Definition ${request.reportDefinitionId} not found", RestStatus.NOT_FOUND) } @@ -129,7 +129,7 @@ internal object ReportInstanceActions { val currentReportInstance = ReportInstancesIndex.getReportInstance(request.reportInstanceId) currentReportInstance ?: run { - Metrics.REPORT_INSTANCE_UPDATE_USER_ERROR.counter.increment() + Metrics.REPORT_INSTANCE_UPDATE_USER_ERROR_MISSING_REPORT_INSTANCE.counter.increment() throw ElasticsearchStatusException("Report Instance ${request.reportInstanceId} not found", RestStatus.NOT_FOUND) } if (!UserAccessManager.doesUserHasAccess(user, currentReportInstance.tenant, currentReportInstance.access)) { @@ -137,7 +137,7 @@ internal object ReportInstanceActions { throw ElasticsearchStatusException("Permission denied for Report Definition ${request.reportInstanceId}", RestStatus.FORBIDDEN) } if (request.status == Status.Scheduled) { // Don't allow changing status to Scheduled - Metrics.REPORT_INSTANCE_UPDATE_USER_ERROR.counter.increment() + Metrics.REPORT_INSTANCE_UPDATE_USER_ERROR_INVALID_STATUS.counter.increment() throw ElasticsearchStatusException("Status cannot be updated to ${Status.Scheduled}", RestStatus.BAD_REQUEST) } val currentTime = Instant.now() diff --git a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/DeleteReportDefinitionRequest.kt b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/DeleteReportDefinitionRequest.kt index 36fb8894..790c25b7 100644 --- a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/DeleteReportDefinitionRequest.kt +++ b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/DeleteReportDefinitionRequest.kt @@ -76,7 +76,7 @@ internal class DeleteReportDefinitionRequest( } } reportDefinitionId ?: run { - Metrics.REPORT_DEFINITION_DELETE_USER_ERROR.counter.increment() + Metrics.REPORT_DEFINITION_DELETE_USER_ERROR_INVALID_REPORT_DEF_ID.counter.increment() throw IllegalArgumentException("$REPORT_DEFINITION_ID_FIELD field absent") } return DeleteReportDefinitionRequest(reportDefinitionId) diff --git a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/GetAllReportDefinitionsRequest.kt b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/GetAllReportDefinitionsRequest.kt index 1d63239e..9a7a8b0b 100644 --- a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/GetAllReportDefinitionsRequest.kt +++ b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/GetAllReportDefinitionsRequest.kt @@ -101,7 +101,7 @@ internal class GetAllReportDefinitionsRequest( return if (fromIndex < 0) { val exception = ActionRequestValidationException() exception.addValidationError("fromIndex should be greater than 0") - Metrics.REPORT_DEFINITION_LIST_USER_ERROR.counter.increment() + Metrics.REPORT_DEFINITION_LIST_USER_ERROR_INVALID_FROM_INDEX.counter.increment() exception } else { null diff --git a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/GetAllReportInstancesRequest.kt b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/GetAllReportInstancesRequest.kt index 0f910b31..a7051847 100644 --- a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/GetAllReportInstancesRequest.kt +++ b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/GetAllReportInstancesRequest.kt @@ -101,7 +101,7 @@ internal data class GetAllReportInstancesRequest( return if (fromIndex < 0) { val exception = ActionRequestValidationException() exception.addValidationError("fromIndex should be greater than 0") - Metrics.REPORT_INSTANCE_LIST_USER_ERROR.counter.increment() + Metrics.REPORT_INSTANCE_LIST_USER_ERROR_INVALID_FROM_INDEX.counter.increment() exception } else { null diff --git a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/GetReportDefinitionRequest.kt b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/GetReportDefinitionRequest.kt index ade5369b..d2f190b7 100644 --- a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/GetReportDefinitionRequest.kt +++ b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/GetReportDefinitionRequest.kt @@ -76,7 +76,7 @@ internal class GetReportDefinitionRequest( } } reportDefinitionId ?: run { - Metrics.REPORT_DEFINITION_INFO_USER_ERROR.counter.increment() + Metrics.REPORT_DEFINITION_INFO_USER_ERROR_INVALID_REPORT_DEF_ID.counter.increment() throw IllegalArgumentException("$REPORT_DEFINITION_ID_FIELD field absent") } return GetReportDefinitionRequest(reportDefinitionId) diff --git a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/InContextReportCreateRequest.kt b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/InContextReportCreateRequest.kt index c7c64dee..0a9f1123 100644 --- a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/InContextReportCreateRequest.kt +++ b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/InContextReportCreateRequest.kt @@ -119,15 +119,15 @@ internal class InContextReportCreateRequest : ActionRequest, ToXContentObject { } } beginTime ?: run { - Metrics.REPORT_FROM_DEFINITION_USER_ERROR.counter.increment() + Metrics.REPORT_FROM_DEFINITION_USER_ERROR_INVALID_BEGIN_TIME.counter.increment() throw IllegalArgumentException("$BEGIN_TIME_FIELD field absent") } endTime ?: run { - Metrics.REPORT_FROM_DEFINITION_USER_ERROR.counter.increment() + Metrics.REPORT_FROM_DEFINITION_USER_ERROR_INVALID_END_TIME.counter.increment() throw IllegalArgumentException("$END_TIME_FIELD field absent") } status ?: run { - Metrics.REPORT_FROM_DEFINITION_USER_ERROR.counter.increment() + Metrics.REPORT_FROM_DEFINITION_USER_ERROR_INVALID_STATUS.counter.increment() throw IllegalArgumentException("$STATUS_FIELD field absent") } this.beginTime = beginTime diff --git a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/OnDemandReportCreateRequest.kt b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/OnDemandReportCreateRequest.kt index d06ea631..9bd4e38e 100644 --- a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/OnDemandReportCreateRequest.kt +++ b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/OnDemandReportCreateRequest.kt @@ -76,7 +76,7 @@ internal class OnDemandReportCreateRequest( } } reportDefinitionId ?: run { - Metrics.REPORT_FROM_DEFINITION_ID_USER_ERROR.counter.increment() + Metrics.REPORT_FROM_DEFINITION_ID_USER_ERROR_INVALID_REPORT_DEF_ID.counter.increment() throw IllegalArgumentException("$REPORT_DEFINITION_ID_FIELD field absent") } diff --git a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/UpdateReportDefinitionRequest.kt b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/UpdateReportDefinitionRequest.kt index 00dba820..8dc480bf 100644 --- a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/UpdateReportDefinitionRequest.kt +++ b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/UpdateReportDefinitionRequest.kt @@ -85,11 +85,11 @@ internal class UpdateReportDefinitionRequest : ActionRequest, ToXContentObject { } } reportDefinitionId ?: run { - Metrics.REPORT_DEFINITION_UPDATE_USER_ERROR.counter.increment() + Metrics.REPORT_DEFINITION_UPDATE_USER_ERROR_INVALID_REPORT_DEF_ID.counter.increment() throw IllegalArgumentException("$REPORT_DEFINITION_ID_FIELD field absent") } reportDefinition ?: run { - Metrics.REPORT_DEFINITION_UPDATE_USER_ERROR.counter.increment() + Metrics.REPORT_DEFINITION_UPDATE_USER_ERROR_INVALID_REPORT_DEF.counter.increment() throw IllegalArgumentException("$REPORT_DEFINITION_FIELD field absent") } this.reportDefinitionId = reportDefinitionId diff --git a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/UpdateReportDefinitionResponse.kt b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/UpdateReportDefinitionResponse.kt index 023699ce..92180842 100644 --- a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/UpdateReportDefinitionResponse.kt +++ b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/UpdateReportDefinitionResponse.kt @@ -70,7 +70,7 @@ internal class UpdateReportDefinitionResponse( } } reportDefinitionId ?: run { - Metrics.REPORT_DEFINITION_UPDATE_USER_ERROR.counter.increment() + Metrics.REPORT_DEFINITION_UPDATE_SYSTEM_ERROR.counter.increment() throw IllegalArgumentException("$REPORT_DEFINITION_ID_FIELD field absent") } return UpdateReportDefinitionResponse(reportDefinitionId) diff --git a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/UpdateReportInstanceStatusRequest.kt b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/UpdateReportInstanceStatusRequest.kt index 67c97f92..f9b5aa1f 100644 --- a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/UpdateReportInstanceStatusRequest.kt +++ b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/UpdateReportInstanceStatusRequest.kt @@ -88,11 +88,11 @@ internal class UpdateReportInstanceStatusRequest( } } reportInstanceId ?: run { - Metrics.REPORT_INSTANCE_UPDATE_USER_ERROR.counter.increment() + Metrics.REPORT_INSTANCE_UPDATE_USER_ERROR_INVALID_REPORT_ID.counter.increment() throw IllegalArgumentException("$REPORT_INSTANCE_ID_FIELD field absent") } status ?: run { - Metrics.REPORT_INSTANCE_UPDATE_USER_ERROR.counter.increment() + Metrics.REPORT_INSTANCE_UPDATE_USER_ERROR_INVALID_STATUS.counter.increment() throw IllegalArgumentException("$STATUS_FIELD field absent") } return UpdateReportInstanceStatusRequest(reportInstanceId, status, statusText) From 083f7f503b5d2c94126048f2c550b98fe29db266 Mon Sep 17 00:00:00 2001 From: Abbas Hussain Date: Tue, 5 Jan 2021 21:18:44 -0800 Subject: [PATCH 24/25] Fix metric name --- .../reportsscheduler/metrics/Metrics.java | 1 - .../reportsscheduler/action/ReportInstanceActions.kt | 2 +- .../reportsscheduler/model/GetReportInstanceRequest.kt | 2 +- 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Metrics.java b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Metrics.java index 46924035..fa933845 100644 --- a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Metrics.java +++ b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Metrics.java @@ -105,7 +105,6 @@ public enum Metrics { // GET _opendistro/_reports/instance/{reportInstanceId} REPORT_INSTANCE_INFO_TOTAL("report_instance.info.total", new BasicCounter()), REPORT_INSTANCE_INFO_INTERVAL_COUNT("report_instance.info.count", new RollingCounter()), - REPORT_INSTANCE_INFO_USER_ERROR("report_instance.info.user_error", new RollingCounter()), REPORT_INSTANCE_INFO_USER_ERROR_MISSING_REPORT_INSTANCE( "report_instance.info.user_error.missing_report_instance", new RollingCounter() diff --git a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/action/ReportInstanceActions.kt b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/action/ReportInstanceActions.kt index 57e2a240..1cfbe340 100644 --- a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/action/ReportInstanceActions.kt +++ b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/action/ReportInstanceActions.kt @@ -162,7 +162,7 @@ internal object ReportInstanceActions { val reportInstance = ReportInstancesIndex.getReportInstance(request.reportInstanceId) reportInstance ?: run { - Metrics.REPORT_INSTANCE_INFO_USER_ERROR.counter.increment() + Metrics.REPORT_INSTANCE_INFO_USER_ERROR_MISSING_REPORT_INSTANCE.counter.increment() throw ElasticsearchStatusException("Report Instance ${request.reportInstanceId} not found", RestStatus.NOT_FOUND) } diff --git a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/GetReportInstanceRequest.kt b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/GetReportInstanceRequest.kt index 5e7d5a37..846c63e8 100644 --- a/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/GetReportInstanceRequest.kt +++ b/reports-scheduler/src/main/kotlin/com/amazon/opendistroforelasticsearch/reportsscheduler/model/GetReportInstanceRequest.kt @@ -75,7 +75,7 @@ internal class GetReportInstanceRequest( } } reportInstanceId ?: run { - Metrics.REPORT_INSTANCE_INFO_USER_ERROR.counter.increment() + Metrics.REPORT_INSTANCE_INFO_USER_ERROR_INVALID_REPORT_ID.counter.increment() throw IllegalArgumentException("$REPORT_INSTANCE_ID_FIELD field absent") } From b68ad64f22e82a8ddf44d5a717c79c7be632ec51 Mon Sep 17 00:00:00 2001 From: Abbas Hussain Date: Wed, 6 Jan 2021 14:02:18 -0800 Subject: [PATCH 25/25] Add javadoc to Metrics enum --- .../reportsscheduler/metrics/Metrics.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Metrics.java b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Metrics.java index fa933845..246a0075 100644 --- a/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Metrics.java +++ b/reports-scheduler/src/main/java/com/amazon/opendistroforelasticsearch/reportsscheduler/metrics/Metrics.java @@ -19,6 +19,9 @@ import com.github.wnameless.json.unflattener.JsonUnflattener; import org.json.JSONObject; +/** + * Enum to hold all the metrics that need to be logged into _opendistro/_reports/l_ocal/stats API + */ public enum Metrics { REQUEST_TOTAL("request_total", new BasicCounter()), @@ -163,6 +166,9 @@ public Counter getCounter() { private static final Metrics[] values = values(); + /** + * Converts the enum metric values to JSON string + */ public static String collectToJSON() { JSONObject metricsJSONObject = new JSONObject(); for (Metrics metric: values) { @@ -171,6 +177,20 @@ public static String collectToJSON() { return metricsJSONObject.toString(); } + /** + * Unflattens the JSON to nested JSON for easy readability and parsing + * The metric name is unflattened in the output JSON on the period '.' delimiter + * + * For ex: { "a.b.c_d" : 2 } becomes + *{ + * "a" : { + * "b" : { + * "c_d" : 2 + * } + * } + * } + */ + public static String collectToFlattenedJSON() { return JsonUnflattener.unflatten(collectToJSON()); }