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