3535
3636@ InterfaceAudience .Private
3737public class PrometheusHadoopServlet extends HttpServlet {
38-
3938 private static final Pattern SPLIT_PATTERN =
4039 Pattern .compile ("(?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=([A-Z][a-z]))|\\ W|(_)+" );
4140
4241 @ Override
4342 protected void doGet (HttpServletRequest req , HttpServletResponse resp ) throws IOException {
44- writeMetrics (resp .getWriter (), "true" .equals (req .getParameter ("description" )));
43+ writeMetrics (resp .getWriter (), "true" .equals (req .getParameter ("description" )),
44+ req .getParameter ("qry" ));
4545 }
4646
4747 static String toPrometheusName (String metricRecordName , String metricName ) {
@@ -57,31 +57,35 @@ static String toPrometheusName(String metricRecordName, String metricName) {
5757 */
5858 @ RestrictedApi (explanation = "Should only be called in tests or self" , link = "" ,
5959 allowedOnPath = ".*/src/test/.*|.*/PrometheusHadoopServlet\\ .java" )
60- void writeMetrics (Writer writer , boolean desc ) throws IOException {
60+ void writeMetrics (Writer writer , boolean descriptionEnabled , String queryParam )
61+ throws IOException {
6162 Collection <MetricsRecord > metricRecords = MetricsExportHelper .export ();
6263 for (MetricsRecord metricsRecord : metricRecords ) {
6364 for (AbstractMetric metrics : metricsRecord .metrics ()) {
6465 if (metrics .type () == MetricType .COUNTER || metrics .type () == MetricType .GAUGE ) {
6566
6667 String key = toPrometheusName (metricsRecord .name (), metrics .name ());
6768
68- if (desc ) {
69- String description = metrics .description ();
70- if (!description .isEmpty ()) writer .append ("# HELP " ).append (description ).append ('\n' );
71- }
69+ if (queryParam == null || key .contains (queryParam )) {
70+
71+ if (descriptionEnabled ) {
72+ String description = metrics .description ();
73+ if (!description .isEmpty ()) writer .append ("# HELP " ).append (description ).append ('\n' );
74+ }
7275
73- writer .append ("# TYPE " ).append (key ).append (" " )
74- .append (metrics .type ().toString ().toLowerCase ()).append ('\n' ).append (key ).append ("{" );
76+ writer .append ("# TYPE " ).append (key ).append (" " )
77+ .append (metrics .type ().toString ().toLowerCase ()).append ('\n' ).append (key ).append ("{" );
7578
76- /* add tags */
77- String sep = "" ;
78- for (MetricsTag tag : metricsRecord .tags ()) {
79- String tagName = tag .name ().toLowerCase ();
80- writer .append (sep ).append (tagName ).append ("=\" " ).append (tag .value ()).append ("\" " );
81- sep = "," ;
79+ /* add tags */
80+ String sep = "" ;
81+ for (MetricsTag tag : metricsRecord .tags ()) {
82+ String tagName = tag .name ().toLowerCase ();
83+ writer .append (sep ).append (tagName ).append ("=\" " ).append (tag .value ()).append ("\" " );
84+ sep = "," ;
85+ }
86+ writer .append ("} " );
87+ writer .append (metrics .value ().toString ()).append ('\n' );
8288 }
83- writer .append ("} " );
84- writer .append (metrics .value ().toString ()).append ('\n' );
8589 }
8690 }
8791 }
0 commit comments