Skip to content
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

NullPointerException when running with recordMetrics #51

Closed
gunjdesai opened this issue Jun 10, 2020 · 7 comments · Fixed by #52
Closed

NullPointerException when running with recordMetrics #51

gunjdesai opened this issue Jun 10, 2020 · 7 comments · Fixed by #52
Assignees
Labels
bug Something isn't working

Comments

@gunjdesai
Copy link

I am trying to use the akka-http-metrics-prometheus registry

Scala Version: 2.12.11
Akka Http Version: 10.1.12
Akka Http Metrics Prometheus Version: 1.1.0

My Code looks like this

object Registry {

  def apply() = {

    val settings = PrometheusSettings
      .default
      .withDurationConfig(Buckets(1, 2, 3, 5, 8, 13, 21, 34))
      .withReceivedBytesConfig(Quantiles(0.5, 0.75, 0.9, 0.95, 0.99))
      .withSentBytesConfig(PrometheusSettings.DefaultQuantiles)

    val prometheus = new CollectorRegistry()

    PrometheusRegistry(settings = settings)

  }

}
Http().bindAndHandle(routes.recordMetrics(Registry()), appConfig.server.interface,  appConfig.server.port)

When i try to run any route, I am facing a NullPointerException.

Could not materialize handling flow for IncomingConnection(/127.0.0.1:8080,/127.0.0.1:64662,Flow(FlowShape(IncomingTCP.in(1710177258),GraphStages$Detacher.out(817845304))))
java.lang.NullPointerException
	at scala.concurrent.impl.Promise$DefaultPromise.onComplete(Promise.scala:307)
	at fr.davit.akka.http.metrics.core.HttpMetricsRegistry.onConnection(HttpMetricsRegistry.scala:126)
	at fr.davit.akka.http.metrics.core.scaladsl.server.HttpMetricsRoute.$anonfun$recordMetrics$1(HttpMetricsRoute.scala:65)
	at akka.stream.impl.Compose.apply(TraversalBuilder.scala:169)
	at akka.stream.impl.PhasedFusingActorMaterializer.materialize(PhasedFusingActorMaterializer.scala:529)
	at akka.stream.impl.PhasedFusingActorMaterializer.materialize(PhasedFusingActorMaterializer.scala:449)
	at akka.stream.impl.PhasedFusingActorMaterializer.materialize(PhasedFusingActorMaterializer.scala:441)
	at akka.stream.scaladsl.RunnableGraph.run(Flow.scala:703)
	at akka.http.scaladsl.HttpExt.$anonfun$bindAndHandle$1(Http.scala:252)
	at akka.stream.impl.fusing.MapAsyncUnordered$$anon$31.onPush(Ops.scala:1401)
	at akka.stream.impl.fusing.GraphInterpreter.processPush(GraphInterpreter.scala:541)
	at akka.stream.impl.fusing.GraphInterpreter.processEvent(GraphInterpreter.scala:495)
	at akka.stream.impl.fusing.GraphInterpreter.execute(GraphInterpreter.scala:390)
	at akka.stream.impl.fusing.GraphInterpreterShell.runBatch(ActorGraphInterpreter.scala:624)
	at akka.stream.impl.fusing.GraphInterpreterShell$AsyncInput.execute(ActorGraphInterpreter.scala:501)
	at akka.stream.impl.fusing.GraphInterpreterShell.processEvent(ActorGraphInterpreter.scala:599)
	at akka.stream.impl.fusing.ActorGraphInterpreter.akka$stream$impl$fusing$ActorGraphInterpreter$$processEvent(ActorGraphInterpreter.scala:768)
	at akka.stream.impl.fusing.ActorGraphInterpreter$$anonfun$receive$1.applyOrElse(ActorGraphInterpreter.scala:783)
	at akka.actor.Actor.aroundReceive(Actor.scala:534)
	at akka.actor.Actor.aroundReceive$(Actor.scala:532)
	at akka.stream.impl.fusing.ActorGraphInterpreter.aroundReceive(ActorGraphInterpreter.scala:690)
	at akka.actor.ActorCell.receiveMessage(ActorCell.scala:573)
	at akka.actor.ActorCell.invoke(ActorCell.scala:543)
	at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:269)
	at akka.dispatch.Mailbox.run(Mailbox.scala:230)
	at akka.dispatch.Mailbox.exec(Mailbox.scala:242)
	at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)
	at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056)
	at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692)
	at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157)

I am pretty sure that i have made some sort of mistake during initialisation and hence the issue.
Would really appreciate if you can point me in the right direction.

Thanks

@RustedBones RustedBones self-assigned this Jun 10, 2020
@RustedBones RustedBones added the bug Something isn't working label Jun 10, 2020
@RustedBones
Copy link
Owner

Hi @gunjdesai

Thanks a lot for the detailed report. It's actually not your fault but mine.
I've actually introduced a regression in the the 1.1.0 release in recordMetrics:

recordMetrics has a default value for the implicit ExecutionContextExecutor set to null (as akka-http does on the route conversion). I forgot to override the null with a default value for the connection metrics. I Will create a bugfix release.

For now you can add this to your code:

  implicit val system: ActorSystem = ActorSystem()
  implicit val ec: ExecutionContextExecutor =  implicitly[Materializer].executionContext

This import the default materializer explicitly and should make your project run.

Sorry for the inconvenience.

@gunjdesai
Copy link
Author

Thanks @RustedBones for looking into this so fast, adding the execution context executor does work for other routes, but still facing an issue while trying /metrics route.

Error during processing of request: 'java.lang.NullPointerException (No error message supplied)'. Completing with 500 Internal Server Error response. To change default exception handling behavior, provide a custom ExceptionHandler.
java.lang.NullPointerException
	at fr.davit.akka.http.metrics.prometheus.marshalling.PrometheusMarshallers.$anonfun$marshaller$1(PrometheusMarshallers.scala:36)
	at akka.http.scaladsl.marshalling.Marshaller$.$anonfun$opaque$2(Marshaller.scala:162)
	at akka.http.scaladsl.marshalling.Marshalling$Opaque.$anonfun$map$7(Marshaller.scala:215)
	at akka.http.scaladsl.marshalling.Marshal.$anonfun$toResponseFor$1(Marshal.scala:71)
	at akka.http.scaladsl.util.FastFuture$.$anonfun$map$1(FastFuture.scala:23)
	at akka.http.scaladsl.util.FastFuture$.strictTransform$1(FastFuture.scala:41)
	at akka.http.scaladsl.util.FastFuture$.transformWith$extension1(FastFuture.scala:45)
	at akka.http.scaladsl.util.FastFuture$.map$extension(FastFuture.scala:23)
	at akka.http.scaladsl.marshalling.Marshal.toResponseFor(Marshal.scala:55)
	at akka.http.scaladsl.marshalling.ToResponseMarshallable.apply(ToResponseMarshallable.scala:17)
	at akka.http.scaladsl.marshalling.ToResponseMarshallable.apply$(ToResponseMarshallable.scala:16)
	at akka.http.scaladsl.marshalling.ToResponseMarshallable$$anon$1.apply(ToResponseMarshallable.scala:22)
	at akka.http.scaladsl.server.RequestContextImpl.complete(RequestContextImpl.scala:42)
	at akka.http.scaladsl.server.directives.RouteDirectives.$anonfun$complete$1(RouteDirectives.scala:47)
	at akka.http.scaladsl.server.StandardRoute$$anon$1.apply(StandardRoute.scala:19)
	at akka.http.scaladsl.server.StandardRoute$$anon$1.apply(StandardRoute.scala:19)
	at akka.http.scaladsl.server.directives.BasicDirectives.$anonfun$mapRouteResult$2(BasicDirectives.scala:68)
	at akka.http.scaladsl.server.directives.BasicDirectives.$anonfun$textract$2(BasicDirectives.scala:161)
	at akka.http.scaladsl.server.directives.BasicDirectives.$anonfun$mapRequestContext$2(BasicDirectives.scala:45)
	at akka.http.scaladsl.server.directives.BasicDirectives.$anonfun$textract$2(BasicDirectives.scala:161)
	at akka.http.scaladsl.server.RouteConcatenation$RouteWithConcatenation.$anonfun$$tilde$2(RouteConcatenation.scala:47)
	at akka.http.scaladsl.util.FastFuture$.strictTransform$1(FastFuture.scala:41)
	at akka.http.scaladsl.util.FastFuture$.transformWith$extension1(FastFuture.scala:45)
	at akka.http.scaladsl.util.FastFuture$.flatMap$extension(FastFuture.scala:26)
	at akka.http.scaladsl.server.RouteConcatenation$RouteWithConcatenation.$anonfun$$tilde$1(RouteConcatenation.scala:44)
	at akka.http.scaladsl.server.directives.BasicDirectives.$anonfun$mapRouteResultWith$2(BasicDirectives.scala:74)
	at akka.http.scaladsl.server.directives.BasicDirectives.$anonfun$textract$2(BasicDirectives.scala:161)
	at akka.http.scaladsl.server.directives.ExecutionDirectives.$anonfun$handleExceptions$2(ExecutionDirectives.scala:32)
	at akka.http.scaladsl.server.directives.BasicDirectives.$anonfun$textract$2(BasicDirectives.scala:161)
	at akka.http.scaladsl.server.Route$.$anonfun$asyncHandler$1(Route.scala:86)
	at fr.davit.akka.http.metrics.core.scaladsl.server.HttpMetricsRoute.$anonfun$recordMetricsAsync$3(HttpMetricsRoute.scala:90)
	at akka.stream.impl.fusing.MapAsyncUnordered$$anon$31.onPush(Ops.scala:1401)
	at akka.stream.impl.fusing.GraphInterpreter.processPush(GraphInterpreter.scala:541)
	at akka.stream.impl.fusing.GraphInterpreter.execute(GraphInterpreter.scala:423)
	at akka.stream.impl.fusing.GraphInterpreterShell.runBatch(ActorGraphInterpreter.scala:624)
	at akka.stream.impl.fusing.GraphInterpreterShell$AsyncInput.execute(ActorGraphInterpreter.scala:501)
	at akka.stream.impl.fusing.GraphInterpreterShell.processEvent(ActorGraphInterpreter.scala:599)
	at akka.stream.impl.fusing.ActorGraphInterpreter.akka$stream$impl$fusing$ActorGraphInterpreter$$processEvent(ActorGraphInterpreter.scala:768)
	at akka.stream.impl.fusing.ActorGraphInterpreter$$anonfun$receive$1.applyOrElse(ActorGraphInterpreter.scala:783)
	at akka.actor.Actor.aroundReceive(Actor.scala:534)
	at akka.actor.Actor.aroundReceive$(Actor.scala:532)
	at akka.stream.impl.fusing.ActorGraphInterpreter.aroundReceive(ActorGraphInterpreter.scala:690)
	at akka.actor.ActorCell.receiveMessage(ActorCell.scala:573)
	at akka.actor.ActorCell.invoke(ActorCell.scala:543)
	at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:269)
	at akka.dispatch.Mailbox.run(Mailbox.scala:230)
	at akka.dispatch.Mailbox.exec(Mailbox.scala:242)
	at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)
	at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056)
	at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692)
	at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157)

Any idea what i am missing here ?

@RustedBones
Copy link
Owner

I tested the marshaling too yesterday, and it serialized the metrics properly.
Release 1.1.1 should be available now. Can you update and give it another try ?

@RustedBones RustedBones reopened this Jun 11, 2020
@gunjdesai
Copy link
Author

Tried 1.1.1, dont need the variable for ExecutionContextExecutor anymore, thank you so much for that update but still facing issue with the metrics api. I am assuming its because of the way, i have initialised it and not because of the library. Will try to debug
Below is the code FYR

import fr.davit.akka.http.metrics.core.scaladsl.server.HttpMetricsDirectives._
import fr.davit.akka.http.metrics.prometheus.marshalling.PrometheusMarshallers._
import com.api.server.Start.registry

object Metrics {

  val metricRoute = path("metrics") {

    get {

        metrics(registry)

    }

  }

  val routes = this.metricRoute

}

@RustedBones
Copy link
Owner

looking at the stack trace, it seems the registry, or the underlying registry is null.
How is your the com.api.server.Start.registry initialized ?

@gunjdesai
Copy link
Author

@RustedBones, extremely sorry, very basic miss, registry was imported from a variable initialised in try - catch block, hence was null.
Thank you so much for helping me out really appreciate it. Kudos !!

@RustedBones
Copy link
Owner

Glad I could help :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants