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 import org.joda.time.Seconds;
027
028 /**
029 * @author matt.proud@gmail.com (Matt T. Proud)
030 */
031 public class Main {
032 @Register
033 private static final Counter rpcCalls = Counter.builder().inNamespace("rpc").named("calls_total")
034 .documentedAs("The total number of RPC calls partitioned by RPC service.").build();
035 @Register
036 private static final Summary rpcLatency = Summary.builder().inNamespace("rpc")
037 .named("latency_microseconds").documentedAs("RPC latency partitioned by RPC service.")
038 .withTarget(0.01, 0.001).withTarget(0.05, 0.025).withTarget(0.50, 0.05)
039 .withTarget(0.90, 0.01).withTarget(0.99, 0.001)
040 .purgesEvery(15, Seconds.ONE.toStandardDuration()).build();
041
042 public static void main(final String[] arguments) {
043 Prometheus.defaultInitialize();
044 final Server server = new Server(8181);
045 final ServletContextHandler context = new ServletContextHandler();
046 context.setContextPath("/");
047 server.setHandler(context);
048 context.addServlet(new ServletHolder(new MetricsServlet()), "/");
049
050 new Thread() {
051 @Override
052 public void run() {
053 final RandomDataImpl randomData = new RandomDataImpl();
054
055 try {
056 while (true) {
057 rpcLatency.newPartial()
058 .withDimension("service", "foo")
059 .apply()
060 .observe((double)randomData.nextLong(0, 200));
061 rpcLatency.newPartial()
062 .withDimension("service", "bar")
063 .apply()
064 .observe(randomData.nextGaussian(100, 20));
065 rpcLatency.newPartial()
066 .withDimension("service", "zed")
067 .apply()
068 .observe((double)randomData.nextExponential(100));
069
070 rpcCalls.newPartial()
071 .withDimension("service", "foo")
072 .apply()
073 .increment();
074 rpcCalls.newPartial()
075 .withDimension("service", "bar")
076 .apply()
077 .increment();
078 rpcCalls.newPartial()
079 .withDimension("service", "zed")
080 .apply()
081 .increment();
082
083 Thread.sleep(250);
084 }
085 } catch (final InterruptedException e) {
086 }
087 }
088 }.start();
089
090 try {
091 server.start();
092 server.join();
093 } catch (Exception e) {
094 e.printStackTrace();
095 }
096 }
097 }