1616import org .elasticsearch .telemetry .metric .MeterRegistry ;
1717import org .elasticsearch .xpack .core .inference .action .BaseInferenceActionRequest ;
1818
19+ import java .util .HashMap ;
1920import java .util .Map ;
2021import java .util .Objects ;
21- import java .util .stream .Collectors ;
22- import java .util .stream .Stream ;
23-
24- import static java .util .Map .entry ;
2522
2623public record InferenceStats (LongCounter requestCount , LongHistogram inferenceDuration ) {
2724
@@ -45,43 +42,35 @@ public static InferenceStats create(MeterRegistry meterRegistry) {
4542 );
4643 }
4744
48- private static Map <String , Object > toMap (Stream <Map .Entry <String , Object >> stream ) {
49- return stream .collect (Collectors .toMap (Map .Entry ::getKey , Map .Entry ::getValue ));
50- }
51-
5245 public static Map <String , Object > modelAttributes (Model model ) {
53- var stream = Stream .<Map .Entry <String , Object >>builder ()
54- .add (entry ("service" , model .getConfigurations ().getService ()))
55- .add (entry ("task_type" , model .getTaskType ().toString ()));
56- if (model .getServiceSettings ().modelId () != null ) {
57- stream .add (entry ("model_id" , model .getServiceSettings ().modelId ()));
46+ var modelAttributesMap = new HashMap <String , Object >();
47+ modelAttributesMap .put ("service" , model .getConfigurations ().getService ());
48+ modelAttributesMap .put ("task_type" , model .getTaskType ().toString ());
49+
50+ if (Objects .nonNull (model .getServiceSettings ().modelId ())) {
51+ modelAttributesMap .put ("model_id" , model .getServiceSettings ().modelId ());
5852 }
59- return toMap (stream .build ());
53+
54+ return modelAttributesMap ;
6055 }
6156
6257 public static Map <String , Object > routingAttributes (BaseInferenceActionRequest request , String nodeIdHandlingRequest ) {
6358 return Map .of ("rerouted" , request .hasBeenRerouted (), "node_id" , nodeIdHandlingRequest );
6459 }
6560
6661 public static Map <String , Object > modelAttributes (UnparsedModel model ) {
67- var unknownModelAttributes = Stream .<Map .Entry <String , Object >>builder ()
68- .add (entry ("service" , model .service ()))
69- .add (entry ("task_type" , model .taskType ().toString ()))
70- .build ();
71-
72- return toMap (unknownModelAttributes );
62+ return Map .of ("service" , model .service (), "task_type" , model .taskType ().toString ());
7363 }
7464
75- public static Map <String , Object > responseAttributes (@ Nullable Throwable t ) {
76- var stream = switch (t ) {
77- case null -> Stream .<Map .Entry <String , Object >>of (entry ("status_code" , 200 ));
78- case ElasticsearchStatusException ese -> Stream .<Map .Entry <String , Object >>builder ()
79- .add (entry ("status_code" , ese .status ().getStatus ()))
80- .add (entry ("error.type" , String .valueOf (ese .status ().getStatus ())))
81- .build ();
82- default -> Stream .<Map .Entry <String , Object >>of (entry ("error.type" , t .getClass ().getSimpleName ()));
83- };
65+ public static Map <String , Object > responseAttributes (@ Nullable Throwable throwable ) {
66+ if (Objects .isNull (throwable )) {
67+ return Map .of ("status_code" , 200 );
68+ }
69+
70+ if (throwable instanceof ElasticsearchStatusException ese ) {
71+ return Map .of ("status_code" , ese .status ().getStatus (), "error.type" , String .valueOf (ese .status ().getStatus ()));
72+ }
8473
85- return toMap ( stream );
74+ return Map . of ( "error.type" , throwable . getClass (). getSimpleName () );
8675 }
8776}
0 commit comments