Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -74,10 +74,10 @@ class JsonGraphEngine extends GraphEngine {
gen.writeEndArray()

if (config.stats.inputLines > 0) {

Copy link
Copy Markdown
Contributor

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: DefaultGraphEngine also checks config.loadTime > 0. Without that, this can emit "dataFetchTime": -1 when loadTime is left at the default.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

plots.map(_.data.size) counts all data defs including spans and messages, but the output only includes lines. A graph with one line and a :vspan reports graphLines = 2. seriesList.size would match what is actually written.

val graphDatapoints = graphLines * ((end - start) / (config.step / 1000) + 1)
val graphDatapoints = graphLines * ((end - start) / config.step + 1)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

endTime is exclusive and the values loop above emits (end - start) / step rows, so the + 1 overcounts by one per line. For example, the 3 minute window in the new test emits 3 rows but reports graphDatapoints = 4.

Suggest moving this into GraphDef, something like:

def numDatapoints: Long = (endTime.toEpochMilli - startTime.toEpochMilli) / step

and using it from both JsonGraphEngine and DefaultGraphEngine so they stay consistent.


gen.writeObjectPropertyStart("explain")
gen.writeNumberProperty("dataFetchTime", config.loadTime)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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 stripMargin literals in the same way. If this is for Windows checkouts, a .gitattributes with *.scala text eol=lf would handle it repo wide. I'd suggest dropping these changes from this PR.

val actual = process(new CommaSepGraphEngine, List(line(42.0, "a"), line(Double.NaN, "b")))
assertEquals(actual, expected)
}
Expand All @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 = {
Expand Down Expand Up @@ -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") {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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 process that takes a GraphDef.

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}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This expects graphDatapoints = 4, but values has 3 rows. Same for the sub-second test below (2 rows, expects 3). Should be updated along with the off-by-one fix.

|}"""
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))
}
}
Loading