@@ -76,22 +76,36 @@ private[spark] class MetricsSystem private (
76
76
private val sources = new mutable.ArrayBuffer [Source ]
77
77
private val registry = new MetricRegistry ()
78
78
79
+ private var running : Boolean = false
80
+
79
81
// Treat MetricsServlet as a special sink as it should be exposed to add handlers to web ui
80
82
private var metricsServlet : Option [MetricsServlet ] = None
81
83
82
- /** Get any UI handlers used by this metrics system. */
83
- def getServletHandlers = metricsServlet.map(_.getHandlers).getOrElse(Array ())
84
+ /**
85
+ * Get any UI handlers used by this metrics system; can only be called after start().
86
+ */
87
+ def getServletHandlers = {
88
+ require(running, " Can only call getServletHandlers on a running MetricsSystem" )
89
+ metricsServlet.map(_.getHandlers).getOrElse(Array ())
90
+ }
84
91
85
92
metricsConfig.initialize()
86
93
87
94
def start () {
95
+ require(! running, " Attempting to start a MetricsSystem that is already running" )
96
+ running = true
88
97
registerSources()
89
98
registerSinks()
90
99
sinks.foreach(_.start)
91
100
}
92
101
93
102
def stop () {
94
- sinks.foreach(_.stop)
103
+ if (running) {
104
+ sinks.foreach(_.stop)
105
+ } else {
106
+ logWarning(" Stopping a MetricsSystem that is not running" )
107
+ }
108
+ running = false
95
109
}
96
110
97
111
def report () {
@@ -107,7 +121,7 @@ private[spark] class MetricsSystem private (
107
121
* @return An unique metric name for each combination of
108
122
* application, executor/driver and metric source.
109
123
*/
110
- def buildRegistryName (source : Source ): String = {
124
+ private [spark] def buildRegistryName (source : Source ): String = {
111
125
val appId = conf.getOption(" spark.app.id" )
112
126
val executorId = conf.getOption(" spark.executor.id" )
113
127
val defaultName = MetricRegistry .name(source.sourceName)
@@ -144,7 +158,7 @@ private[spark] class MetricsSystem private (
144
158
})
145
159
}
146
160
147
- def registerSources () {
161
+ private def registerSources () {
148
162
val instConfig = metricsConfig.getInstance(instance)
149
163
val sourceConfigs = metricsConfig.subProperties(instConfig, MetricsSystem .SOURCE_REGEX )
150
164
@@ -160,7 +174,7 @@ private[spark] class MetricsSystem private (
160
174
}
161
175
}
162
176
163
- def registerSinks () {
177
+ private def registerSinks () {
164
178
val instConfig = metricsConfig.getInstance(instance)
165
179
val sinkConfigs = metricsConfig.subProperties(instConfig, MetricsSystem .SINK_REGEX )
166
180
0 commit comments