15
15
import java .util .List ;
16
16
import java .util .Map ;
17
17
import java .util .Objects ;
18
+ import java .util .UUID ;
18
19
import org .apache .logging .log4j .LogManager ;
19
20
import org .apache .logging .log4j .Logger ;
20
21
import org .opensearch .Version ;
@@ -41,6 +42,8 @@ public class SearchQueryRecord implements ToXContentObject, Writeable {
41
42
private final long timestamp ;
42
43
private final Map <MetricType , Measurement > measurements ;
43
44
private final Map <Attribute , Object > attributes ;
45
+ private final String id ;
46
+
44
47
/**
45
48
* Timestamp
46
49
*/
@@ -93,11 +96,15 @@ public class SearchQueryRecord implements ToXContentObject, Writeable {
93
96
* Grouping type of the query record (none, similarity)
94
97
*/
95
98
public static final String GROUP_BY = "group_by" ;
99
+ /**
100
+ * UUID
101
+ */
102
+ public static final String ID = "id" ;
96
103
97
104
/**
98
105
* Query Group hashcode or query hashcode representing a unique identifier for the query/group
99
106
*/
100
- public static final String ID = "id " ;
107
+ public static final String QUERY_GROUP_HASHCODE = "query_group_hashcode " ;
101
108
102
109
public static final String MEASUREMENTS = "measurements" ;
103
110
private String groupingId ;
@@ -111,6 +118,7 @@ public class SearchQueryRecord implements ToXContentObject, Writeable {
111
118
*/
112
119
public SearchQueryRecord (final StreamInput in ) throws IOException , ClassCastException {
113
120
this .timestamp = in .readLong ();
121
+ this .id = in .readString ();
114
122
if (in .getVersion ().onOrAfter (Version .V_2_17_0 )) {
115
123
measurements = new LinkedHashMap <>();
116
124
in .readOrderedMap (MetricType ::readFromStream , Measurement ::readFromStream )
@@ -137,12 +145,30 @@ public SearchQueryRecord(final StreamInput in) throws IOException, ClassCastExce
137
145
* @param attributes A list of Attributes associated with this query
138
146
*/
139
147
public SearchQueryRecord (final long timestamp , Map <MetricType , Measurement > measurements , final Map <Attribute , Object > attributes ) {
148
+ this (timestamp , measurements , attributes , UUID .randomUUID ().toString ());
149
+ }
150
+
151
+ /**
152
+ * Constructor of SearchQueryRecord
153
+ *
154
+ * @param timestamp The timestamp of the query.
155
+ * @param measurements A list of Measurement associated with this query
156
+ * @param attributes A list of Attributes associated with this query
157
+ * @param id unique id for a query
158
+ */
159
+ public SearchQueryRecord (
160
+ final long timestamp ,
161
+ Map <MetricType , Measurement > measurements ,
162
+ final Map <Attribute , Object > attributes ,
163
+ String id
164
+ ) {
140
165
if (measurements == null ) {
141
166
throw new IllegalArgumentException ("Measurements cannot be null" );
142
167
}
143
168
this .measurements = measurements ;
144
169
this .attributes = attributes ;
145
170
this .timestamp = timestamp ;
171
+ this .id = id ;
146
172
}
147
173
148
174
/**
@@ -156,6 +182,7 @@ public static SearchQueryRecord fromXContent(XContentParser parser) throws IOExc
156
182
long timestamp = 0L ;
157
183
Map <MetricType , Measurement > measurements = new HashMap <>();
158
184
Map <Attribute , Object > attributes = new HashMap <>();
185
+ String id = null ;
159
186
160
187
parser .nextToken ();
161
188
XContentParserUtils .ensureExpectedToken (XContentParser .Token .START_OBJECT , parser .currentToken (), parser );
@@ -167,6 +194,9 @@ public static SearchQueryRecord fromXContent(XContentParser parser) throws IOExc
167
194
case TIMESTAMP :
168
195
timestamp = parser .longValue ();
169
196
break ;
197
+ case ID :
198
+ id = parser .text ();
199
+ break ;
170
200
case LATENCY :
171
201
case CPU :
172
202
case MEMORY :
@@ -179,8 +209,8 @@ public static SearchQueryRecord fromXContent(XContentParser parser) throws IOExc
179
209
case GROUP_BY :
180
210
attributes .put (Attribute .GROUP_BY , parser .text ());
181
211
break ;
182
- case ID :
183
- attributes .put (Attribute .ID , parser .text ());
212
+ case QUERY_GROUP_HASHCODE :
213
+ attributes .put (Attribute .QUERY_GROUP_HASHCODE , parser .text ());
184
214
break ;
185
215
case SOURCE :
186
216
XContentParserUtils .ensureExpectedToken (XContentParser .Token .START_OBJECT , parser .currentToken (), parser );
@@ -264,7 +294,7 @@ public static SearchQueryRecord fromXContent(XContentParser parser) throws IOExc
264
294
log .error ("Error when parsing through search hit" , e );
265
295
}
266
296
}
267
- return new SearchQueryRecord (timestamp , measurements , attributes );
297
+ return new SearchQueryRecord (timestamp , measurements , attributes , id );
268
298
}
269
299
270
300
/**
@@ -337,6 +367,9 @@ public void addAttribute(final Attribute attribute, final Object value) {
337
367
public XContentBuilder toXContent (final XContentBuilder builder , final Params params ) throws IOException {
338
368
builder .startObject ();
339
369
builder .field ("timestamp" , timestamp );
370
+ if (id != null ) {
371
+ builder .field ("id" , id );
372
+ }
340
373
for (Map .Entry <Attribute , Object > entry : attributes .entrySet ()) {
341
374
builder .field (entry .getKey ().toString (), entry .getValue ());
342
375
}
@@ -358,6 +391,7 @@ public XContentBuilder toXContent(final XContentBuilder builder, final Params pa
358
391
@ Override
359
392
public void writeTo (final StreamOutput out ) throws IOException {
360
393
out .writeLong (timestamp );
394
+ out .writeString (id );
361
395
if (out .getVersion ().onOrAfter (Version .V_2_17_0 )) {
362
396
out .writeMap (
363
397
measurements ,
0 commit comments