Skip to content

Commit 5204a3e

Browse files
committed
add minimalist exporter for Prometheus
1 parent aca5496 commit 5204a3e

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package app.softnetwork.elastic.client.monitoring
2+
3+
import app.softnetwork.elastic.client.metrics.MetricsElasticClient
4+
5+
trait PrometheusExporter { self: MetricsElasticClient =>
6+
7+
def exportPrometheusMetrics: String = {
8+
val aggregated = getAggregatedMetrics
9+
val sb = new StringBuilder()
10+
11+
// Global metrics
12+
sb.append(s"elasticsearch_operations_total ${aggregated.totalOperations}\n")
13+
sb.append(s"elasticsearch_operations_success ${aggregated.successCount}\n")
14+
sb.append(s"elasticsearch_operations_failure ${aggregated.failureCount}\n")
15+
sb.append(s"elasticsearch_operations_duration_ms ${aggregated.totalDuration}\n")
16+
17+
// Per-operation metrics
18+
aggregated.operationMetrics.foreach { case (op, m) =>
19+
sb.append(s"""elasticsearch_operation_total{operation="$op"} ${m.totalOperations}""" + "\n")
20+
sb.append(s"""elasticsearch_operation_duration_ms{operation="$op"} ${m.totalDuration}""" + "\n")
21+
sb.append(s"""elasticsearch_operation_success_rate{operation="$op"} ${m.successRate}""" + "\n")
22+
}
23+
24+
// Per-index metrics
25+
aggregated.indexMetrics.foreach { case (idx, m) =>
26+
sb.append(s"""elasticsearch_index_operations{index="$idx"} ${m.totalOperations}""" + "\n")
27+
sb.append(s"""elasticsearch_index_duration_ms{index="$idx"} ${m.totalDuration}""" + "\n")
28+
}
29+
30+
sb.toString()
31+
}
32+
}
33+

0 commit comments

Comments
 (0)