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.random;
016
017import io.prometheus.client.Prometheus;
018import io.prometheus.client.Register;
019import io.prometheus.client.metrics.Counter;
020import io.prometheus.client.metrics.Summary;
021import io.prometheus.client.utility.servlet.MetricsServlet;
022import org.apache.commons.math3.random.RandomDataImpl;
023import org.eclipse.jetty.server.Server;
024import org.eclipse.jetty.servlet.ServletContextHandler;
025import org.eclipse.jetty.servlet.ServletHolder;
026
027import java.util.concurrent.TimeUnit;
028
029/**
030 * @author matt.proud@gmail.com (Matt T. Proud)
031 */
032public class Main {
033  private static final Counter rpcCalls = Counter.newBuilder()
034          .namespace("rpc")
035          .name("calls_total")
036          .documentation("The total number of RPC calls partitioned by RPC service.")
037          .build();
038
039  private static final Summary rpcLatency = Summary.newBuilder()
040          .namespace("rpc")
041          .name("latency_microseconds")
042          .documentation("RPC latency partitioned by RPC service.")
043          .targetQuantile(0.01, 0.001)
044          .targetQuantile(0.05, 0.025)
045          .targetQuantile(0.50, 0.05)
046          .targetQuantile(0.90, 0.01)
047          .targetQuantile(0.99, 0.001)
048          .purgeInterval(2, TimeUnit.MINUTES)
049          .build();
050
051  public static void main(final String[] arguments) {
052    Prometheus.defaultInitialize();
053    final Server server = new Server(8080);
054    final ServletContextHandler context = new ServletContextHandler();
055    context.setContextPath("/");
056    server.setHandler(context);
057    context.addServlet(new ServletHolder(new MetricsServlet()), "/");
058
059    new Thread() {
060      @Override
061      public void run() {
062        final RandomDataImpl randomData = new RandomDataImpl();
063
064        try {
065          while (true) {
066            rpcLatency.newPartial()
067                    .labelPair("service", "foo")
068                    .apply()
069                    .observe((double) randomData.nextLong(0, 200));
070            rpcLatency.newPartial()
071                    .labelPair("service", "bar")
072                    .apply()
073                    .observe(randomData.nextGaussian(100, 20));
074            rpcLatency.newPartial()
075                    .labelPair("service", "zed")
076                    .apply()
077                    .observe((double) randomData.nextExponential(100));
078
079            rpcCalls.newPartial()
080                    .labelPair("service", "foo")
081                    .apply()
082                    .increment();
083            rpcCalls.newPartial()
084                    .labelPair("service", "bar")
085                    .apply()
086                    .increment();
087            rpcCalls.newPartial()
088                    .labelPair("service", "zed")
089                    .apply()
090                    .increment();
091
092            Thread.sleep(250);
093          }
094        } catch (final InterruptedException e) {
095        }
096      }
097    }.start();
098
099    try {
100      server.start();
101      server.join();
102    } catch (Exception e) {
103      e.printStackTrace();
104    }
105  }
106}