View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.omid.tso;
19  
20  import com.google.common.base.Stopwatch;
21  import com.google.common.base.Throwables;
22  import org.apache.omid.metrics.MetricsRegistry;
23  import org.slf4j.Logger;
24  import org.slf4j.LoggerFactory;
25  
26  import javax.annotation.concurrent.NotThreadSafe;
27  import java.util.HashMap;
28  import java.util.Map;
29  import java.util.concurrent.ConcurrentHashMap;
30  import java.util.concurrent.TimeUnit;
31  
32  import static org.apache.omid.metrics.MetricsUtils.name;
33  
34  @NotThreadSafe
35  public class MonitoringContext {
36  
37      private static final Logger LOG = LoggerFactory.getLogger(MonitoringContext.class);
38  
39      private volatile boolean flag;
40      private Map<String, Long> elapsedTimeMsMap = new HashMap<>();
41      private Map<String, Stopwatch> timers = new ConcurrentHashMap<>();
42      private MetricsRegistry metrics;
43  
44      public MonitoringContext(MetricsRegistry metrics) {
45          this.metrics = metrics;
46      }
47  
48      public void timerStart(String name) {
49          Stopwatch stopwatch = new Stopwatch();
50          stopwatch.start();
51          timers.put(name, stopwatch);
52      }
53  
54      public void timerStop(String name) {
55          if (flag) {
56              LOG.warn("timerStop({}) called after publish. Measurement was ignored. {}", name, Throwables.getStackTraceAsString(new Exception()));
57              return;
58          }
59          Stopwatch activeStopwatch = timers.get(name);
60          if (activeStopwatch == null) {
61              throw new IllegalStateException(
62                      String.format("There is no %s timer in the %s monitoring context.", name, this));
63          }
64          activeStopwatch.stop();
65          elapsedTimeMsMap.put(name, activeStopwatch.elapsedTime(TimeUnit.NANOSECONDS));
66          timers.remove(name);
67      }
68  
69      public void publish() {
70          flag = true;
71          for (Map.Entry<String, Long> entry : elapsedTimeMsMap.entrySet()) {
72              metrics.timer(name("tso", entry.getKey())).update(entry.getValue());
73          }
74      }
75  
76  }