Skip to content

Commit ab74705

Browse files
committed
add first changes
Signed-off-by: Anthony Leong <aj.leong623@gmail.com>
1 parent 0cc83a9 commit ab74705

File tree

5 files changed

+469
-0
lines changed

5 files changed

+469
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55

66
## [Unreleased 3.x]
77
### Added
8+
- Add search API tracker ([#18601](https://github.com/opensearch-project/OpenSearch/pull/18601))
89
- Expand fetch phase profiling to support inner hits and top hits aggregation phases ([##18936](https://github.com/opensearch-project/OpenSearch/pull/18936))
910
- Add temporal routing processors for time-based document routing ([#18920](https://github.com/opensearch-project/OpenSearch/issues/18920))
1011
- The dynamic mapping parameter supports false_allow_templates ([#19065](https://github.com/opensearch-project/OpenSearch/pull/19065))

libs/core/src/main/java/org/opensearch/core/rest/RestStatus.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,17 @@ public int getStatusFamilyCode() {
536536
return status / 100;
537537
}
538538

539+
public String getErrorType() {
540+
int family = getStatusFamilyCode();
541+
if (family <= 3) {
542+
return "success";
543+
} else if (family == 4) {
544+
return "user_error";
545+
} else {
546+
return "system_failure";
547+
}
548+
}
549+
539550
public static RestStatus readFrom(StreamInput in) throws IOException {
540551
return RestStatus.valueOf(in.readString());
541552
}
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
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 item level rest category class codes during indexing
50+
*
51+
* @opensearch.api
52+
*/
53+
@PublicApi(since = "1.0.0")
54+
public class DocStatusStats implements Writeable, ToXContentFragment {
55+
final LongAdder[] docStatusCounter;
56+
57+
public DocStatusStats() {
58+
docStatusCounter = new LongAdder[5];
59+
for (int i = 0; i < docStatusCounter.length; ++i) {
60+
docStatusCounter[i] = new LongAdder();
61+
}
62+
}
63+
64+
public DocStatusStats(StreamInput in) throws IOException {
65+
docStatusCounter = in.readArray(i -> {
66+
LongAdder adder = new LongAdder();
67+
adder.add(i.readLong());
68+
return adder;
69+
70+
}, LongAdder[]::new);
71+
72+
assert docStatusCounter.length == 5 : "Length of incoming array should be 5! Got " + docStatusCounter.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+
docStatusCounter[status.getStatusFamilyCode() - 1].add(delta);
92+
}
93+
94+
/**
95+
* Accumulate stats from the passed Object
96+
*
97+
* @param stats Instance storing {@link DocStatusStats}
98+
*/
99+
public void add(final DocStatusStats stats) {
100+
if (null == stats) {
101+
return;
102+
}
103+
104+
for (int i = 0; i < docStatusCounter.length; ++i) {
105+
docStatusCounter[i].add(stats.docStatusCounter[i].longValue());
106+
}
107+
}
108+
109+
public LongAdder[] getDocStatusCounter() {
110+
return docStatusCounter;
111+
}
112+
113+
@Override
114+
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
115+
builder.startObject(Fields.DOC_STATUS);
116+
117+
Map<String, Long> errorTypeCounts = new HashMap<>();
118+
119+
for (int i = 0; i < docStatusCounter.length; ++i) {
120+
long count = docStatusCounter[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()), docStatusCounter);
142+
}
143+
144+
/**
145+
* For this function, I just want to retrieve a point in time snapshot of the DocStatusStats.
146+
*/
147+
public DocStatusStats getSnapshot() {
148+
DocStatusStats curSnapshot = new DocStatusStats();
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 DOC_STATUS = "doc_status";
160+
}
161+
}
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
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

Comments
 (0)