-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
BoltMetrics.java
119 lines (102 loc) · 4.85 KB
/
BoltMetrics.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
* Copyright (c) 2002-2016 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.metrics.source.db;
import com.codahale.metrics.Gauge;
import com.codahale.metrics.MetricRegistry;
import java.util.concurrent.atomic.AtomicLong;
import org.neo4j.bolt.v1.runtime.MonitoredSessions;
import org.neo4j.kernel.impl.annotations.Documented;
import org.neo4j.kernel.lifecycle.LifecycleAdapter;
import org.neo4j.kernel.monitoring.Monitors;
import static com.codahale.metrics.MetricRegistry.name;
@Documented( ".Bolt Metrics" )
public class BoltMetrics extends LifecycleAdapter
{
private static final String NAME_PREFIX = "neo4j.bolt";
@Documented( "The total number of messages received via Bolt since this instance started." )
public static final String MESSAGES_RECIEVED = name( NAME_PREFIX, "messages_received" );
@Documented( "The total number of messages work has started on since this instance started. This is different " +
"from messages received in that this counter tracks how many of the received messages have" +
"been taken on by a worker thread." )
public static final String MESSAGES_STARTED = name( NAME_PREFIX, "messages_started" );
@Documented( "The total number of messages work has completed on since this instance started. This includes " +
"successful, failed and ignored Bolt messages." )
public static final String MESSAGES_DONE = name( NAME_PREFIX, "messages_done" );
@Documented( "The accumulated time messages have spent waiting for a worker thread." )
public static final String TOTAL_QUEUE_TIME = name( NAME_PREFIX, "accumulated_queue_time" );
@Documented( "The accumulated time worker threads have spent processing messages." )
public static final String TOTAL_PROCESSING_TIME = name( NAME_PREFIX, "accumulated_processing_time" );
private final MetricRegistry registry;
private final Monitors monitors;
private final BoltMetricsMonitor boltMonitor = new BoltMetricsMonitor();
public BoltMetrics( MetricRegistry registry, Monitors monitors )
{
this.registry = registry;
this.monitors = monitors;
}
@Override
public void start()
{
monitors.addMonitorListener( boltMonitor );
registry.register( MESSAGES_RECIEVED, (Gauge<Long>) boltMonitor.recieved::get );
registry.register( MESSAGES_STARTED, (Gauge<Long>) boltMonitor.started::get );
registry.register( MESSAGES_DONE, (Gauge<Long>) boltMonitor.done::get );
registry.register( TOTAL_QUEUE_TIME, (Gauge<Long>) boltMonitor.queueTime::get );
registry.register( TOTAL_PROCESSING_TIME, (Gauge<Long>) boltMonitor.processingTime::get );
}
@Override
public void stop()
{
registry.remove( MESSAGES_RECIEVED );
registry.remove( MESSAGES_STARTED );
registry.remove( MESSAGES_DONE );
registry.remove( TOTAL_QUEUE_TIME );
registry.remove( TOTAL_PROCESSING_TIME );
monitors.removeMonitorListener( boltMonitor );
}
private class BoltMetricsMonitor implements MonitoredSessions.SessionMonitor
{
public final AtomicLong recieved = new AtomicLong();
public final AtomicLong started = new AtomicLong();
public final AtomicLong done = new AtomicLong();
// It will take about 300 million years of queue/processing time to overflow these
// Even if we run a million processors concurrently, the instance would need to
// run uninterrupted for three hundred years before the monitoring had a hiccup.
public final AtomicLong queueTime = new AtomicLong();
public final AtomicLong processingTime = new AtomicLong();
@Override
public void messageReceived()
{
recieved.incrementAndGet();
}
@Override
public void processingStarted( long queueTime )
{
this.queueTime.addAndGet( queueTime );
started.incrementAndGet();
}
@Override
public void processingDone( long processingTime )
{
this.processingTime.addAndGet( processingTime );
done.incrementAndGet();
}
}
}