aerogear / keycloak-metrics-spi

Adds a Metrics Endpoint to Keycloak
Apache License 2.0
560 stars 159 forks source link

NullPointerException when enabling metrics-listener #220

Closed daniel-pimenta-DME closed 1 week ago

daniel-pimenta-DME commented 1 week ago

Description

Exception when trying to enable metrics-listener on Realm settings > Events > Events listener.

[java.lang.NullPointerException: Cannot invoke "io.opentelemetry.context.Context.makeCurrent()" because "context" is null], SQL: delete from REALM_EVENTS_LISTENERS where REALM_ID=?
2024-11-02 22:12:18,264 traceId=, parentId=, spanId=, sampled= ERROR [org.keycloak.services.error.KeycloakErrorHandler] (executor-thread-8) Uncaught server error: org.keycloak.models.ModelException: Database operation failed
    at org.keycloak.connections.jpa.PersistenceExceptionConverter.convert(PersistenceExceptionConverter.java:109)
    at org.keycloak.connections.jpa.PersistenceExceptionConverter.invoke(PersistenceExceptionConverter.java:68)
    at jdk.proxy3/jdk.proxy3.$Proxy105.flush(Unknown Source)
    at org.keycloak.models.jpa.RealmAdapter.setEventsListeners(RealmAdapter.java:1156)
    at org.keycloak.models.cache.infinispan.RealmAdapter.setEventsListeners(RealmAdapter.java:1036)
    at org.keycloak.services.managers.RealmManager.updateRealmEventsConfig(RealmManager.java:299)
    at org.keycloak.services.resources.admin.RealmAdminResource.updateRealmEventsConfig(RealmAdminResource.java:749)
    at org.keycloak.services.resources.admin.RealmAdminResource$quarkusrestinvoker$updateRealmEventsConfig_819a7611841478b25afbae197af50b540fbebc6f.invoke(Unknown Source)
    at org.jboss.resteasy.reactive.server.handlers.InvocationHandler.handle(InvocationHandler.java:29)
2024-11-02T23:12:18.265230929+01:00     at io.quarkus.resteasy.reactive.server.runtime.QuarkusResteasyReactiveRequestContext.invokeHandler(QuarkusResteasyReactiveRequestContext.java:141)
    at org.jboss.resteasy.reactive.common.core.AbstractResteasyReactiveContext.run(AbstractResteasyReactiveContext.java:147)
2024-11-02T23:12:18.265251018+01:00     at io.quarkus.vertx.core.runtime.VertxCoreRecorder$14.runWith(VertxCoreRecorder.java:635)
2024-11-02T23:12:18.265260881+01:00     at org.jboss.threads.EnhancedQueueExecutor$Task.doRunWith(EnhancedQueueExecutor.java:2516)
2024-11-02T23:12:18.265286840+01:00     at org.jboss.threads.EnhancedQueueExecutor$Task.run(EnhancedQueueExecutor.java:2495)
2024-11-02T23:12:18.265297447+01:00     at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1495)
2024-11-02T23:12:18.265307687+01:00     at org.jboss.threads.DelegatingRunnable.run(DelegatingRunnable.java:11)
2024-11-02T23:12:18.265317545+01:00     at org.jboss.threads.ThreadLocalResettingRunnable.run(ThreadLocalResettingRunnable.java:11)
    at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
2024-11-02T23:12:18.265337259+01:00     at java.base/java.lang.Thread.run(Thread.java:1583)
Caused by: java.lang.NullPointerException: Cannot invoke "io.opentelemetry.context.Context.makeCurrent()" because "context" is null
2024-11-02T23:12:18.265357368+01:00     at io.opentelemetry.instrumentation.jdbc.internal.OpenTelemetryStatement.wrapCall(OpenTelemetryStatement.java:303)
2024-11-02T23:12:18.265367357+01:00     at io.opentelemetry.instrumentation.jdbc.internal.OpenTelemetryStatement.executeBatch(OpenTelemetryStatement.java:116)
2024-11-02T23:12:18.265377429+01:00     at org.hibernate.engine.jdbc.batch.internal.BatchImpl.lambda$performExecution$2(BatchImpl.java:279)
2024-11-02T23:12:18.265387200+01:00     at org.hibernate.engine.jdbc.mutation.internal.PreparedStatementGroupSingleTable.forEachStatement(PreparedStatementGroupSingleTable.java:67)
2024-11-02T23:12:18.265397066+01:00     at org.hibernate.engine.jdbc.batch.internal.BatchImpl.performExecution(BatchImpl.java:264)
2024-11-02T23:12:18.265422849+01:00     at org.hibernate.engine.jdbc.batch.internal.BatchImpl.execute(BatchImpl.java:242)
2024-11-02T23:12:18.265433564+01:00     at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.executeBatch(JdbcCoordinatorImpl.java:188)
2024-11-02T23:12:18.265443589+01:00     at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:674)
2024-11-02T23:12:18.265453477+01:00     at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:511)
2024-11-02T23:12:18.265463589+01:00     at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:414)
2024-11-02T23:12:18.265473701+01:00     at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:41)
2024-11-02T23:12:18.265483586+01:00     at org.hibernate.event.service.internal.EventListenerGroupImpl.fireEventOnEachListener(EventListenerGroupImpl.java:127)
2024-11-02T23:12:18.265493388+01:00     at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1429)
2024-11-02T23:12:18.265503274+01:00     at org.hibernate.internal.SessionImpl.flush(Sessi
onImpl.java:1415)
2024-11-02T23:12:18.265513190+01:00     at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)
2024-11-02T23:12:18.265523114+01:00     at java.base/java.lang.reflect.Method.invoke(Method.java:580)
2024-11-02T23:12:18.265533262+01:00     at org.keycloak.connections.jpa.PersistenceExceptionConverter.invoke(PersistenceExceptionConverter.java:66)
2024-11-02T23:12:18.265543207+01:00     ... 17 more

Expected Behavior

It should enable metrics-listener.

Actual Behavior

An exception is thrown by the server and metrics-listener is not enabled.

Environment

Kubernetes Keycloak 26.0.5

Steps to reproduce

Enable keycloak metrics, opentelemetry feature, and java auto instrumentation with Opentelemetry. Try to enable metrics-listener.

daniel-pimenta-DME commented 1 week ago

Fixed by disabling java auto instrumentation from Opentelemetry. But i would like to have it enabled to have traces monitoring.

daniel-pimenta-DME commented 1 week ago

Instead of using opentelemetry's java auto instrumentation, I've enabled tracing from Keycloak itself and pointed KC_TRACING_ENDPOINT to the opentelemetry collector.