Repository navigation
chart: fix division by zero and step overflow in json engine #2011
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -55,7 +55,7 @@ class JsonGraphEngine extends GraphEngine { | |
| gen.writeEndArray() | ||
|
|
||
| gen.writeArrayPropertyStart("values") | ||
| val step = config.step.asInstanceOf[Int] | ||
| val step = config.step | ||
| val endTime = config.endTime.toEpochMilli | ||
| var timestamp = config.startTime.toEpochMilli | ||
| while (timestamp < endTime) { | ||
|
|
@@ -74,10 +74,10 @@ class JsonGraphEngine extends GraphEngine { | |
| gen.writeEndArray() | ||
|
|
||
| if (config.stats.inputLines > 0) { | ||
| val start = config.startTime.toEpochMilli / 1000 | ||
| val end = config.endTime.toEpochMilli / 1000 | ||
| val start = config.startTime.toEpochMilli | ||
| val end = config.endTime.toEpochMilli | ||
| val graphLines = config.plots.map(_.data.size).sum | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| val graphDatapoints = graphLines * ((end - start) / (config.step / 1000) + 1) | ||
| val graphDatapoints = graphLines * ((end - start) / config.step + 1) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggest moving this into def numDatapoints: Long = (endTime.toEpochMilli - startTime.toEpochMilli) / stepand using it from both |
||
|
|
||
| gen.writeObjectPropertyStart("explain") | ||
| gen.writeNumberProperty("dataFetchTime", config.loadTime) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -54,7 +54,7 @@ class CsvGraphEngineSuite extends FunSuite { | |
| """"timestamp","a","b" | ||
| |2012-01-01T00:00:00Z,42.000000,NaN | ||
| |2012-01-01T00:01:00Z,42.000000,NaN | ||
| |""".stripMargin | ||
| |""".stripMargin.replace("\r\n", "\n") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The CRLF handling seems unrelated to the fix and only covers a few suites, many others compare against |
||
| val actual = process(new CommaSepGraphEngine, List(line(42.0, "a"), line(Double.NaN, "b"))) | ||
| assertEquals(actual, expected) | ||
| } | ||
|
|
@@ -64,7 +64,7 @@ class CsvGraphEngineSuite extends FunSuite { | |
| """"timestamp","a","b" | ||
| |2012-01-01T00:00:00Z,42.000000,NaN | ||
| |2012-01-01T00:01:00Z,42.000000,NaN | ||
| |""".stripMargin.replace(",", "\t") | ||
| |""".stripMargin.replace("\r\n", "\n").replace(",", "\t") | ||
| val actual = process(new TabSepGraphEngine, List(line(42.0, "a"), line(Double.NaN, "b"))) | ||
| assertEquals(actual, expected) | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,7 @@ import java.time.ZonedDateTime | |
| import com.netflix.atlas.chart.model.GraphDef | ||
| import com.netflix.atlas.chart.model.LineDef | ||
| import com.netflix.atlas.chart.model.PlotDef | ||
| import com.netflix.atlas.core.model.CollectorStats | ||
| import com.netflix.atlas.core.model.DsType | ||
| import com.netflix.atlas.core.model.FunctionTimeSeq | ||
| import com.netflix.atlas.core.model.TimeSeries | ||
|
|
@@ -31,18 +32,18 @@ class JsonGraphEngineSuite extends FunSuite { | |
|
|
||
| val step = 60000 | ||
|
|
||
| def constant(v: Double): TimeSeries = { | ||
| TimeSeries(Map("name" -> v.toString), new FunctionTimeSeq(DsType.Gauge, step, _ => v)) | ||
| def constant(v: Double, s: Long = step): TimeSeries = { | ||
| TimeSeries(Map("name" -> v.toString), new FunctionTimeSeq(DsType.Gauge, s, _ => v)) | ||
| } | ||
|
|
||
| def constantSeriesDef(value: Double): LineDef = LineDef(constant(value)) | ||
| def constantSeriesDef(value: Double, s: Long = step): LineDef = LineDef(constant(value, s)) | ||
|
|
||
| def label(vs: LineDef*): List[LineDef] = { | ||
| vs.toList.zipWithIndex.map { case (v, i) => v.copy(data = v.data.withLabel(i.toString)) } | ||
| } | ||
|
|
||
| def strip(expected: String): String = { | ||
| expected.stripMargin.split("\n").mkString("") | ||
| expected.stripMargin.replace("\r", "").split("\n").mkString("") | ||
| } | ||
|
|
||
| def process(engine: GraphEngine, expected: String): Unit = { | ||
|
|
@@ -89,4 +90,101 @@ class JsonGraphEngineSuite extends FunSuite { | |
| process(new StdJsonGraphEngine, expected) | ||
| } | ||
|
|
||
| test("stats.json") { | ||
| val expected = | ||
| """{ | ||
| |"start":1325376000000, | ||
| |"end":1325376180000, | ||
| |"step":60000, | ||
| |"legend":["0","1"], | ||
| |"metrics":[{"name":"42.0"},{"name":"NaN"}], | ||
| |"stats":[{"count":3,"avg":42.0,"total":126.0,"max":42.0,"min":42.0,"last":42.0},{"count":0,"avg":"NaN","total":"NaN","max":"NaN","min":"NaN","last":"NaN"}], | ||
| |"notices":[] | ||
| |}""" | ||
|
|
||
| process(new StatsJsonGraphEngine, expected) | ||
| } | ||
|
|
||
| test("json with explain stats") { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: the new tests repeat the same build/write/compare block. Consider an overload of |
||
| val data = PlotDef(label(constantSeriesDef(42))) | ||
| val graphDef = GraphDef( | ||
| plots = List(data), | ||
| startTime = ZonedDateTime.of(2012, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC).toInstant, | ||
| endTime = ZonedDateTime.of(2012, 1, 1, 0, 3, 0, 0, ZoneOffset.UTC).toInstant, | ||
| loadTime = 123, | ||
| stats = CollectorStats(10, 5, 20, 10) | ||
| ) | ||
| val bytes = Streams.byteArray { out => | ||
| new JsonGraphEngine().write(graphDef, out) | ||
| } | ||
| val json = new String(bytes, "UTF-8") | ||
| val expected = | ||
| """{ | ||
| |"start":1325376000000, | ||
| |"step":60000, | ||
| |"legend":["0"], | ||
| |"metrics":[{"name":"42.0"}], | ||
| |"values":[[42.0],[42.0],[42.0]], | ||
| |"notices":[], | ||
| |"explain":{"dataFetchTime":123,"inputLines":10,"intermediateLines":20,"graphLines":1,"inputDatapoints":5,"intermediateDatapoints":10,"graphDatapoints":4} | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This expects |
||
| |}""" | ||
| assertEquals(json, strip(expected)) | ||
| } | ||
|
|
||
| test("json subsecond step explain does not divide by zero") { | ||
| val subsecondStep = 500L | ||
| val data = PlotDef(label(constantSeriesDef(42, subsecondStep))) | ||
| val start = ZonedDateTime.of(2012, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC).toInstant | ||
| val end = start.plusMillis(1000) | ||
| val graphDef = GraphDef( | ||
| plots = List(data), | ||
| startTime = start, | ||
| endTime = end, | ||
| step = subsecondStep, | ||
| loadTime = 50, | ||
| stats = CollectorStats(1, 1, 2, 2) | ||
| ) | ||
| val bytes = Streams.byteArray { out => | ||
| new JsonGraphEngine().write(graphDef, out) | ||
| } | ||
| val json = new String(bytes, "UTF-8") | ||
| val expected = | ||
| """{ | ||
| |"start":1325376000000, | ||
| |"step":500, | ||
| |"legend":["0"], | ||
| |"metrics":[{"name":"42.0"}], | ||
| |"values":[[42.0],[42.0]], | ||
| |"notices":[], | ||
| |"explain":{"dataFetchTime":50,"inputLines":1,"intermediateLines":2,"graphLines":1,"inputDatapoints":1,"intermediateDatapoints":2,"graphDatapoints":3} | ||
| |}""" | ||
| assertEquals(json, strip(expected)) | ||
| } | ||
|
|
||
| test("json large step does not overflow") { | ||
| val largeStep = 2592000000L // 30 days > Int.MaxValue millis | ||
| val data = PlotDef(label(constantSeriesDef(42, largeStep))) | ||
| val start = ZonedDateTime.of(2012, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC).toInstant | ||
| val end = start.plusMillis(largeStep * 2) | ||
| val graphDef = GraphDef( | ||
| plots = List(data), | ||
| startTime = start, | ||
| endTime = end, | ||
| step = largeStep | ||
| ) | ||
| val bytes = Streams.byteArray { out => | ||
| new JsonGraphEngine().write(graphDef, out) | ||
| } | ||
| val json = new String(bytes, "UTF-8") | ||
| val expected = | ||
| s"""{ | ||
| |"start":1325376000000, | ||
| |"step":$largeStep, | ||
| |"legend":["0"], | ||
| |"metrics":[{"name":"42.0"}], | ||
| |"values":[[42.0],[42.0]], | ||
| |"notices":[] | ||
| |}""" | ||
| assertEquals(json, strip(expected)) | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor, not from this PR:
DefaultGraphEnginealso checksconfig.loadTime > 0. Without that, this can emit"dataFetchTime": -1whenloadTimeis left at the default.