2323import org .apache .jackrabbit .oak .stats .MeterStats ;
2424import org .apache .jackrabbit .oak .stats .StatisticsProvider ;
2525import org .apache .jackrabbit .oak .stats .TimerStats ;
26+ import org .jetbrains .annotations .NotNull ;
2627
27- import java .util .Collections ;
2828import java .util .Map ;
2929import java .util .concurrent .TimeUnit ;
3030import java .util .function .BiFunction ;
3434 */
3535public class ElasticMetricHandler {
3636
37+ private static final String ENABLED = "ELASTIC_ENABLED" ;
38+
3739 private static final String QUERY_RATE = "ELASTIC_QUERY_RATE" ;
3840 private static final String QUERY_INTERNAL_RATE = "ELASTIC_QUERY_INTERNAL_RATE" ;
3941
@@ -59,6 +61,15 @@ public ElasticMetricHandler(StatisticsProvider sp) {
5961 timer = statsProviderUtil .getTimerStats ();
6062 }
6163
64+ /**
65+ * Marks the Elastic metric as enabled or disabled.
66+ *
67+ * @param enabled a boolean indicating whether the Elastic metric is enabled (true) or disabled (false)
68+ */
69+ public void markEnabled (boolean enabled ) {
70+ meter .apply (ENABLED , Map .of ()).mark (enabled ? 1 : 0 );
71+ }
72+
6273 /**
6374 * Tracks a new query using two metrics:
6475 * <ul>
@@ -69,8 +80,8 @@ public ElasticMetricHandler(StatisticsProvider sp) {
6980 * @param index the index passed as metric label
7081 * @param isRootQuery if {@code false} only {@code QUERY_INTERNAL_RATE} gets incremented
7182 */
72- public void markQuery (String index , boolean isRootQuery ) {
73- Map <String , String > labels = Collections . singletonMap ("index" , index );
83+ public void markQuery (@ NotNull String index , boolean isRootQuery ) {
84+ Map <String , String > labels = Map . of ("index" , index );
7485 if (isRootQuery ) {
7586 meter .apply (QUERY_RATE , labels ).mark ();
7687 }
@@ -87,8 +98,8 @@ public void markQuery(String index, boolean isRootQuery) {
8798 * @param timedOut Elastic could time out while returning partial results. When {@code true} these
8899 * occurrences get tracked
89100 */
90- public void measureQuery (String index , int hits , long serverTimeMs , long totalTimeMs , boolean timedOut ) {
91- Map <String , String > labels = Collections . singletonMap ("index" , index );
101+ public void measureQuery (@ NotNull String index , int hits , long serverTimeMs , long totalTimeMs , boolean timedOut ) {
102+ Map <String , String > labels = Map . of ("index" , index );
92103 histogram .apply (QUERY_HITS , labels ).update (hits );
93104 timer .apply (QUERY_SERVER_TIME , labels ).update (serverTimeMs , TimeUnit .MILLISECONDS );
94105 timer .apply (QUERY_TOTAL_TIME , labels ).update (totalTimeMs , TimeUnit .MILLISECONDS );
@@ -103,8 +114,8 @@ public void measureQuery(String index, int hits, long serverTimeMs, long totalTi
103114 * @param index the index passed as metric label
104115 * @param totalTimeMs the total execution time
105116 */
106- public void measureFailedQuery (String index , long totalTimeMs ) {
107- Map <String , String > labels = Collections . singletonMap ("index" , index );
117+ public void measureFailedQuery (@ NotNull String index , long totalTimeMs ) {
118+ Map <String , String > labels = Map . of ("index" , index );
108119 meter .apply (QUERY_FAILED_RATE , labels ).mark ();
109120 timer .apply (QUERY_TOTAL_TIME , labels ).update (totalTimeMs , TimeUnit .MILLISECONDS );
110121 }
@@ -115,8 +126,8 @@ public void measureFailedQuery(String index, long totalTimeMs) {
115126 * @param index the index passed as metric label
116127 * @param numDocs the current number of documents. Only top level documents are tracked
117128 */
118- public void markDocuments (String index , long numDocs ) {
119- Map <String , String > labels = Collections . singletonMap ("index" , index );
129+ public void markDocuments (@ NotNull String index , long numDocs ) {
130+ Map <String , String > labels = Map . of ("index" , index );
120131 histogram .apply (INDEX_DOCUMENTS , labels ).update (numDocs );
121132 }
122133
@@ -127,8 +138,8 @@ public void markDocuments(String index, long numDocs) {
127138 * @param primarySize the primary shards size
128139 * @param storeSize the total size in bytes. The value includes potential replicas
129140 */
130- public void markSize (String index , long primarySize , long storeSize ) {
131- Map <String , String > labels = Collections . singletonMap ("index" , index );
141+ public void markSize (@ NotNull String index , long primarySize , long storeSize ) {
142+ Map <String , String > labels = Map . of ("index" , index );
132143 histogram .apply (INDEX_SIZE , labels ).update (primarySize );
133144 histogram .apply (INDEX_WITH_REPLICAS_SIZE , labels ).update (storeSize );
134145 }
0 commit comments