001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.juneau.rest.server.metrics.micrometer; 018 019import org.apache.juneau.http.response.*; 020import org.apache.juneau.rest.server.*; 021import org.apache.juneau.rest.server.servlet.*; 022 023/** 024 * Child-resource flavor of the {@code /metrics} endpoint. 025 * 026 * <p> 027 * Mounts as a routed child via {@link Rest#children() @Rest(children=MetricsResource.class)} under the 028 * subtree {@code /metrics} and delegates to a shared {@link MetricsManager} worker — the same logic 029 * the {@link MetricsMixin mixin} flavor uses, so the two forms cannot drift. Degrades to HTTP 501 when no 030 * scrapeable registry bean is present. 031 * 032 * <h5 class='section'>See Also:</h5><ul> 033 * <li class='jc'>{@link MetricsMixin} 034 * <li class='jc'>{@link MetricsManager} 035 * </ul> 036 * 037 * @since 10.0.0 038 */ 039@Rest(path="/metrics") 040public class MetricsResource extends BasicRestResource { 041 042 private final MetricsManager manager = new MetricsManager(); 043 044 /** 045 * [GET /] - Prometheus scrape of the consumer-provided MeterRegistry. 046 * 047 * @param req The HTTP request. 048 * @return The Prometheus exposition text. 049 * @throws NotImplemented If no scrapeable {@link io.micrometer.core.instrument.MeterRegistry} bean is registered. 050 */ 051 @RestGet( 052 path="/*", 053 summary="Metrics scrape", 054 description="Renders a Prometheus text scrape from a consumer-provided Micrometer MeterRegistry bean." 055 ) 056 public String getMetrics(RestRequest req) { 057 // A routed child's bean store inherits from its parent, so the consumer's MeterRegistry bean is 058 // reachable via this context's bean store. 059 var registry = manager.resolveRegistry(req.getContext()); 060 var scrape = manager.scrape(registry); 061 if (scrape == null) 062 throw new NotImplemented("No scrapeable MeterRegistry is registered (a PrometheusMeterRegistry bean is required)."); 063 return scrape; 064 } 065}