001/* 002 * Copyright 2013 Prometheus Team Licensed under the Apache License, Version 2.0 003 * (the "License"); you may not use this file except in compliance with the 004 * License. You may obtain a copy of the License at 005 * 006 * http://www.apache.org/licenses/LICENSE-2.0 007 * 008 * Unless required by applicable law or agreed to in writing, software 009 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 010 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 011 * License for the specific language governing permissions and limitations under 012 * the License. 013 */ 014 015package io.prometheus.client.examples.guice; 016 017 018import com.google.inject.Guice; 019import com.google.inject.Inject; 020import com.google.inject.Injector; 021import com.google.inject.Singleton; 022 023 024/** 025 * <p>This Maven module demonstrates Prometheus integration with Google's Guice framework as 026 * well as more advanced use of prepopulated {@link io.prometheus.client.metrics.Metric.Builder} 027 * and {@link io.prometheus.client.metrics.Metric.Partial} constructs.</p> 028 * 029 * <p> 030 * In this case, we demonstrate optional provisioning of {@link io.prometheus.client.metrics.Metric} 031 * through Guice {@link com.google.inject.Provider} in {@link com.google.inject.Module}. What is 032 * interesting to note is that we have a fictional HTTP server with <em>two HTTP {@link 033 * javax.servlet.Servlet} handlers that share the same two fundamental metrics defined in {@link 034 * io.prometheus.client.examples.guice.Module.HandlerCounterProvider} and {@link 035 * io.prometheus.client.examples.guice.Module.HandlerLatencyProvider}</em>. These two metrics 036 * track a typical use case of measuring request counts and latency quantiles. The metrics share 037 * the same schema and metric name, but <em>the handlers can be differentiated by label values 038 * </em>. 039 * </p> 040 * 041 * <p> 042 * This example is unremarkable in the sense that manual passing of partially-fabricated metrics 043 * is possible to do by hand as well, and it is easy. In the interests of minimizing static state 044 * and promoting testability, teams may decide to use dependency injection for provisioning of 045 * metrics that are shared between multiple classes, <em>though this is discouraged since except 046 * for a few rare cases it demonstrates violation of law of demeter and separation of concerns 047 * </em>. 048 * </p> 049 * @author matt.proud@gmail.com (Matt T. Proud) 050 */ 051@Singleton 052public class Main { 053 private final Server server; 054 055 @Inject 056 public Main(final Server server) { 057 this.server = server; 058 } 059 060 public void run() { 061 server.configure(); 062 server.run(); 063 } 064 065 public static void main(final String[] arguments) { 066 final Injector injector = Guice.createInjector(new Module()); 067 final Main main = injector.getInstance(Main.class); 068 069 main.run(); 070 } 071}