Skip to content

Commit

Permalink
lwcapi: fix status code for event without id (#1649)
Browse files Browse the repository at this point in the history
If an event is sent to `/evaluate` without an id, respond
with a 400 error instead of 500.
  • Loading branch information
brharrington authored Apr 3, 2024
1 parent f16a7f0 commit 875a7f6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,7 @@ import com.netflix.atlas.json.JsonSupport
* Raw event payload.
*/
case class LwcEvent(id: String, payload: JsonNode) extends JsonSupport {

require(id != null, "id cannot be null")
val `type`: String = "event"
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import com.netflix.atlas.eval.model.LwcEvent
import com.netflix.atlas.json.Json
import com.netflix.atlas.lwcapi.EvaluateApi.*
import com.netflix.atlas.pekko.DiagnosticMessage
import com.netflix.atlas.pekko.RequestHandler
import com.netflix.atlas.pekko.testkit.MUnitRouteSuite
import com.netflix.spectator.api.NoopRegistry

Expand All @@ -34,49 +35,56 @@ class EvaluateApiSuite extends MUnitRouteSuite {
private implicit val routeTestTimeout: RouteTestTimeout = RouteTestTimeout(5.second)

private val sm = new StreamSubscriptionManager(new NoopRegistry)
private val endpoint = new EvaluateApi(new NoopRegistry, sm)
private val routes = RequestHandler.standardOptions(new EvaluateApi(new NoopRegistry, sm).routes)

test("post empty payload") {
val json = EvaluateRequest(1234L, Nil, Nil).toJson
Post("/lwc/api/v1/evaluate", json) ~> endpoint.routes ~> check {
Post("/lwc/api/v1/evaluate", json) ~> routes ~> check {
assertEquals(response.status, StatusCodes.OK)
}
}

test("post metrics") {
val metrics = List(Item("abc", SortedTagMap("a" -> "1"), 42.0))
val json = EvaluateRequest(1234L, metrics, Nil, Nil).toJson
Post("/lwc/api/v1/evaluate", json) ~> endpoint.routes ~> check {
Post("/lwc/api/v1/evaluate", json) ~> routes ~> check {
assertEquals(response.status, StatusCodes.OK)
}
}

test("post events") {
val events = List(LwcEvent("abc", Json.decode[JsonNode]("42.0")))
val json = EvaluateRequest(1234L, Nil, events, Nil).toJson
Post("/lwc/api/v1/evaluate", json) ~> endpoint.routes ~> check {
Post("/lwc/api/v1/evaluate", json) ~> routes ~> check {
assertEquals(response.status, StatusCodes.OK)
}
}

test("post events, missing event id") {
val json = """{"timestamp":1234,"events":[{"payload":42.0,"type":"event"}]}"""
Post("/lwc/api/v1/evaluate", json) ~> routes ~> check {
assertEquals(response.status, StatusCodes.BadRequest)
}
}

test("post diagnostic message") {
val msgs = List(LwcDiagnosticMessage("abc", DiagnosticMessage.error("bad expression")))
val json = EvaluateRequest(1234L, Nil, Nil, msgs).toJson
Post("/lwc/api/v1/evaluate", json) ~> endpoint.routes ~> check {
Post("/lwc/api/v1/evaluate", json) ~> routes ~> check {
assertEquals(response.status, StatusCodes.OK)
}
}

test("post missing messages field") {
val json = """{"timestamp":12345,"metrics":[]}"""
Post("/lwc/api/v1/evaluate", json) ~> endpoint.routes ~> check {
Post("/lwc/api/v1/evaluate", json) ~> routes ~> check {
assertEquals(response.status, StatusCodes.OK)
}
}

test("post missing metrics field") {
val json = """{"timestamp":12345,"messages":[]}"""
Post("/lwc/api/v1/evaluate", json) ~> endpoint.routes ~> check {
Post("/lwc/api/v1/evaluate", json) ~> routes ~> check {
assertEquals(response.status, StatusCodes.OK)
}
}
Expand Down

0 comments on commit 875a7f6

Please sign in to comment.