-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #407 from nscuro/metrics
Expose Prometheus metrics with Micrometer
- Loading branch information
Showing
11 changed files
with
229 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
alpine-common/src/main/java/alpine/common/metrics/Metrics.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* This file is part of Alpine. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* Copyright (c) Steve Springett. All Rights Reserved. | ||
*/ | ||
package alpine.common.metrics; | ||
|
||
import alpine.Config; | ||
import io.micrometer.core.instrument.binder.jvm.ExecutorServiceMetrics; | ||
import io.micrometer.prometheus.PrometheusConfig; | ||
import io.micrometer.prometheus.PrometheusMeterRegistry; | ||
|
||
import java.util.concurrent.ExecutorService; | ||
|
||
/** | ||
* @since 2.1.0 | ||
*/ | ||
public final class Metrics { | ||
|
||
private static final PrometheusMeterRegistry registry = new PrometheusMeterRegistry(PrometheusConfig.DEFAULT); | ||
|
||
public static PrometheusMeterRegistry getRegistry() { | ||
return registry; | ||
} | ||
|
||
public static void registerExecutorService(final ExecutorService executorService, final String name) { | ||
if (Config.getInstance().getPropertyAsBoolean(Config.AlpineKey.METRICS_ENABLED)) { | ||
new ExecutorServiceMetrics(executorService, name, null).bindTo(registry); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
alpine-server/src/main/java/alpine/server/metrics/MetricsInitializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* This file is part of Alpine. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* Copyright (c) Steve Springett. All Rights Reserved. | ||
*/ | ||
package alpine.server.metrics; | ||
|
||
import alpine.Config; | ||
import alpine.common.logging.Logger; | ||
import alpine.common.metrics.Metrics; | ||
import io.micrometer.core.instrument.binder.jvm.ClassLoaderMetrics; | ||
import io.micrometer.core.instrument.binder.jvm.JvmGcMetrics; | ||
import io.micrometer.core.instrument.binder.jvm.JvmInfoMetrics; | ||
import io.micrometer.core.instrument.binder.jvm.JvmMemoryMetrics; | ||
import io.micrometer.core.instrument.binder.jvm.JvmThreadMetrics; | ||
import io.micrometer.core.instrument.binder.system.DiskSpaceMetrics; | ||
import io.micrometer.core.instrument.binder.system.ProcessorMetrics; | ||
import io.micrometer.core.instrument.binder.system.UptimeMetrics; | ||
|
||
import javax.servlet.ServletContextEvent; | ||
import javax.servlet.ServletContextListener; | ||
|
||
/** | ||
* @since 2.1.0 | ||
*/ | ||
public class MetricsInitializer implements ServletContextListener { | ||
|
||
private static final Logger LOGGER = Logger.getLogger(MetricsInitializer.class); | ||
|
||
@Override | ||
public void contextInitialized(final ServletContextEvent event) { | ||
if (Config.getInstance().getPropertyAsBoolean(Config.AlpineKey.METRICS_ENABLED)) { | ||
LOGGER.info("Registering system metrics"); | ||
new ClassLoaderMetrics().bindTo(Metrics.getRegistry()); | ||
new DiskSpaceMetrics(Config.getInstance().getDataDirectorty()).bindTo(Metrics.getRegistry()); | ||
new JvmGcMetrics().bindTo(Metrics.getRegistry()); | ||
new JvmInfoMetrics().bindTo(Metrics.getRegistry()); | ||
new JvmMemoryMetrics().bindTo(Metrics.getRegistry()); | ||
new JvmThreadMetrics().bindTo(Metrics.getRegistry()); | ||
new ProcessorMetrics().bindTo(Metrics.getRegistry()); | ||
new UptimeMetrics().bindTo(Metrics.getRegistry()); | ||
} | ||
} | ||
|
||
} |
51 changes: 51 additions & 0 deletions
51
alpine-server/src/main/java/alpine/server/servlets/MetricsServlet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* This file is part of Alpine. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* Copyright (c) Steve Springett. All Rights Reserved. | ||
*/ | ||
package alpine.server.servlets; | ||
|
||
import alpine.Config; | ||
import alpine.common.metrics.Metrics; | ||
|
||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServlet; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
|
||
/** | ||
* @since 2.1.0 | ||
*/ | ||
public class MetricsServlet extends HttpServlet { | ||
|
||
private boolean metricsEnabled; | ||
|
||
@Override | ||
public void init() throws ServletException { | ||
metricsEnabled = Config.getInstance().getPropertyAsBoolean(Config.AlpineKey.METRICS_ENABLED); | ||
} | ||
|
||
@Override | ||
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws IOException { | ||
if (metricsEnabled) { | ||
Metrics.getRegistry().scrape(resp.getWriter()); | ||
} else { | ||
resp.setStatus(HttpServletResponse.SC_NOT_FOUND); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters