|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +/* |
| 10 | + * Licensed to Elasticsearch under one or more contributor |
| 11 | + * license agreements. See the NOTICE file distributed with |
| 12 | + * this work for additional information regarding copyright |
| 13 | + * ownership. Elasticsearch licenses this file to you under |
| 14 | + * the Apache License, Version 2.0 (the "License"); you may |
| 15 | + * not use this file except in compliance with the License. |
| 16 | + * You may obtain a copy of the License at |
| 17 | + * |
| 18 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 19 | + * |
| 20 | + * Unless required by applicable law or agreed to in writing, |
| 21 | + * software distributed under the License is distributed on an |
| 22 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 23 | + * KIND, either express or implied. See the License for the |
| 24 | + * specific language governing permissions and limitations |
| 25 | + * under the License. |
| 26 | + */ |
| 27 | + |
| 28 | +/* |
| 29 | + * Modifications Copyright OpenSearch Contributors. See |
| 30 | + * GitHub history for details. |
| 31 | + */ |
| 32 | + |
| 33 | +package org.opensearch.action.admin.indices.stats; |
| 34 | + |
| 35 | +import org.opensearch.common.annotation.PublicApi; |
| 36 | +import org.opensearch.core.common.io.stream.StreamInput; |
| 37 | +import org.opensearch.core.common.io.stream.StreamOutput; |
| 38 | +import org.opensearch.core.common.io.stream.Writeable; |
| 39 | +import org.opensearch.core.rest.RestStatus; |
| 40 | +import org.opensearch.core.xcontent.ToXContentFragment; |
| 41 | +import org.opensearch.core.xcontent.XContentBuilder; |
| 42 | + |
| 43 | +import java.io.IOException; |
| 44 | +import java.util.HashMap; |
| 45 | +import java.util.Map; |
| 46 | +import java.util.concurrent.atomic.LongAdder; |
| 47 | + |
| 48 | +/** |
| 49 | + * Tracks rest category class codes from search requests |
| 50 | + * |
| 51 | + * @opensearch.api |
| 52 | + */ |
| 53 | +@PublicApi(since = "1.0.0") |
| 54 | +public class SearchResponseStatusStats implements Writeable, ToXContentFragment { |
| 55 | + final LongAdder[] searchResponseStatusCounter; |
| 56 | + |
| 57 | + public SearchResponseStatusStats() { |
| 58 | + searchResponseStatusCounter = new LongAdder[5]; |
| 59 | + for (int i = 0; i < searchResponseStatusCounter.length; i++) { |
| 60 | + searchResponseStatusCounter[i] = new LongAdder(); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + public SearchResponseStatusStats(StreamInput in) throws IOException { |
| 65 | + searchResponseStatusCounter = in.readArray(i -> { |
| 66 | + LongAdder adder = new LongAdder(); |
| 67 | + adder.add(i.readLong()); |
| 68 | + return adder; |
| 69 | + |
| 70 | + }, LongAdder[]::new); |
| 71 | + |
| 72 | + assert searchResponseStatusCounter.length == 5 : "Length of incoming array should be 5! Got " + searchResponseStatusCounter.length; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Increment counter for status |
| 77 | + * |
| 78 | + * @param status {@link RestStatus} |
| 79 | + */ |
| 80 | + public void inc(final RestStatus status) { |
| 81 | + add(status, 1L); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Increment counter for status by count |
| 86 | + * |
| 87 | + * @param status {@link RestStatus} |
| 88 | + * @param delta The value to add |
| 89 | + */ |
| 90 | + public void add(final RestStatus status, final long delta) { |
| 91 | + searchResponseStatusCounter[status.getStatusFamilyCode() - 1].add(delta); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Accumulate stats from the passed Object |
| 96 | + * |
| 97 | + * @param stats Instance storing {@link SearchResponseStatusStats} |
| 98 | + */ |
| 99 | + public void add(final SearchResponseStatusStats stats) { |
| 100 | + if (null == stats) { |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + for (int i = 0; i < searchResponseStatusCounter.length; ++i) { |
| 105 | + searchResponseStatusCounter[i].add(stats.searchResponseStatusCounter[i].longValue()); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + public LongAdder[] getSearchResponseStatusCounter() { |
| 110 | + return searchResponseStatusCounter; |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { |
| 115 | + builder.startObject(Fields.SEARCH_RESPONSE_STATUS); |
| 116 | + |
| 117 | + Map<String, Long> errorTypeCounts = new HashMap<>(); |
| 118 | + |
| 119 | + for (int i = 0; i < searchResponseStatusCounter.length; ++i) { |
| 120 | + long count = searchResponseStatusCounter[i].longValue(); |
| 121 | + |
| 122 | + if (count > 0) { |
| 123 | + RestStatus familyStatus = RestStatus.fromCode((i + 1) * 100); |
| 124 | + String errorType = familyStatus.getErrorType(); |
| 125 | + errorTypeCounts.put(errorType, errorTypeCounts.getOrDefault(errorType, (long) 0) + count); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + String successType = RestStatus.ACCEPTED.getErrorType(); |
| 130 | + String userFailureType = RestStatus.BAD_REQUEST.getErrorType(); |
| 131 | + String systemErrorType = RestStatus.INTERNAL_SERVER_ERROR.getErrorType(); |
| 132 | + builder.field(successType, errorTypeCounts.getOrDefault(successType, (long) 0)); |
| 133 | + builder.field(userFailureType, errorTypeCounts.getOrDefault(userFailureType, (long) 0)); |
| 134 | + builder.field(systemErrorType, errorTypeCounts.getOrDefault(systemErrorType, (long) 0)); |
| 135 | + |
| 136 | + return builder.endObject(); |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + public void writeTo(StreamOutput out) throws IOException { |
| 141 | + out.writeArray((o, v) -> o.writeLong(v.longValue()), searchResponseStatusCounter); |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * For this function, I just want to retrieve a point in time snapshot of the SearchResponseStatusStats. |
| 146 | + */ |
| 147 | + public SearchResponseStatusStats getSnapshot() { |
| 148 | + SearchResponseStatusStats curSnapshot = new SearchResponseStatusStats(); |
| 149 | + curSnapshot.add(this); |
| 150 | + return curSnapshot; |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Fields for parsing and toXContent |
| 155 | + * |
| 156 | + * @opensearch.internal |
| 157 | + */ |
| 158 | + static final class Fields { |
| 159 | + static final String SEARCH_RESPONSE_STATUS = "search_response_status"; |
| 160 | + } |
| 161 | +} |
0 commit comments