Skip to content

Commit

Permalink
[FLINK-4920] Introduce Scala Function Gauge
Browse files Browse the repository at this point in the history
This closes #3080.
  • Loading branch information
p16i authored and zentol committed Jan 19, 2017
1 parent b36b43b commit 570dbc8
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 deletions.
22 changes: 22 additions & 0 deletions docs/monitoring/metrics.md
Expand Up @@ -83,6 +83,8 @@ A `Gauge` provides a value of any type on demand. In order to use a `Gauge` you
There is no restriction for the type of the returned value.
You can register a gauge by calling `gauge(String name, Gauge gauge)` on a `MetricGroup`.

<div class="codetabs" markdown="1">
<div data-lang="java" markdown="1">
{% highlight java %}

public class MyMapper extends RichMapFunction<String, Integer> {
Expand All @@ -102,6 +104,26 @@ public class MyMapper extends RichMapFunction<String, Integer> {
}

{% endhighlight %}
</div>

<div data-lang="scala" markdown="1">
{% highlight scala %}

public class MyMapper extends RichMapFunction[String,Int] {
val valueToExpose = 5

override def open(parameters: Configuration): Unit = {
getRuntimeContext()
.getMetricGroup()
.gauge("MyGauge", ScalaGauge[Int]( () => valueToExpose ) )
}
...
}

{% endhighlight %}
</div>

</div>

Note that reporters will turn the exposed object into a `String`, which means that a meaningful `toString()` implementation is required.

Expand Down
@@ -0,0 +1,34 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.flink.api.scala.metrics

import org.apache.flink.metrics.Gauge

/**
* This class allows the concise definition of a gauge from Scala using function references.
*/
class ScalaGauge[T](func: () => T) extends Gauge[T] {
override def getValue: T = {
func()
}
}

object ScalaGauge {
def apply[T](func: () => T): ScalaGauge[T] = new ScalaGauge(func)
}
@@ -0,0 +1,36 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.flink.api.scala.metrics

import org.apache.flink.metrics.Gauge
import org.apache.flink.runtime.metrics.{MetricRegistry, MetricRegistryConfiguration}
import org.apache.flink.runtime.metrics.groups.GenericMetricGroup
import org.apache.flink.util.TestLogger
import org.junit.Test
import org.scalatest.junit.JUnitSuiteLike

class ScalaGaugeTest extends TestLogger with JUnitSuiteLike {

@Test
def testGaugeCorrectValue(): Unit = {
val myGauge = ScalaGauge[Long](() => 4)
assert(myGauge.getValue == 4)
}

}

0 comments on commit 570dbc8

Please sign in to comment.