-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
ContextNotActiveException when access database to sample prometheus gauge #25237
Comments
Try adding |
@ktysiac thanks for the hint You are right. Wrapping the call into a transaction works 👍. @ApplicationScoped
@Startup
public class MetricsCollector {
private final MeterRegistry registry;
private final MyRepository myRepository;
public MetricsCollector(MeterRegistry registry, MyRepository myRepository) {
this.registry = registry;
this.myRepository = myRepository;
Gauge.builder("my-gauge", () -> QuarkusTransaction.call(myRepository::count)).register(registry);
}
} What is still frustrating is the missleading Exception as in I would expect a |
I agree that the error message is not clear here... @gsmet @yrodiere I wonder if we should catch the exception at https://github.com/quarkusio/quarkus/blob/2.9.0.Final/extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/session/TransactionScopedSession.java#L103 and try to provide a more useful error message - basically what is mentioned at (https://github.com/quarkusio/quarkus/blob/2.9.0.Final/extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/session/TransactionScopedSession.java#L101-L102. WDYT? |
Assuming we can accurately identify the "context not active" exception or perform a preliminary check, indeed it would be nice to provide a more helpful message. Something that tells users "you need a transaction to use the entitymanager/session. Alternatively, if you really don't want transactions, you need to be in a request processing thread, or you need to activate the CDI request context explicitly". With better phrasing, of course, but that's the gist of it.
That's out of the question, since sessions can be used outside of transactions, as long as there is a request context (yes, that means
If you're handling a request, I think it's fair to expect the request context to be active. Though I can't tell whether it makes sense in the context of prometheus. |
+1
Technically this is totally correct. But I have seen that this expectation often confuses users, who when they see |
Right, I did not mean to deny the need for a better exception message in |
Though in that case I see the exception occurs in the bean's constructor, so we're probably no handling a request. It's probably less clear-cut than what I thought. |
Was about to say :)
the call to our code is hidden. However, we can enable the request context at: https://github.com/quarkusio/quarkus/blob/2.9.0.Final/extensions/micrometer/runtime/src/main/java/io/quarkus/micrometer/runtime/export/handlers/PrometheusHandler.java#L35 I think I'll go ahead and do that, it makes sense as we don't really know what user code will be executed here, so we'd better be on the safe side. |
#25594 is what I am talking about. In any case, I'll close this issue as it's not a Quarkus issue (apart from the error message discussion above) |
Activate request context when prometheus scraping is invoked
Describe the bug
When building a prometheus gauge via micrometer to count database entities no metric is collected.
Also the exception
ContextNotActiveException
is logged when endpoint/q/metrics
is called.It seem that prometheus endpoint is not handled correctly with an active context.
This makes it hard to collect custom gauges based on data of a panache repository.
Example gauge:
Expected behavior
For every prometheus scape should return the current count of
myRepository
Actual behavior
Stacktrace:
Prometheus metric output:
How to Reproduce?
Output of
uname -a
orver
Darwin xxx 21.4.0 Darwin Kernel Version 21.4.0: Fri Mar 18 00:46:32 PDT 2022; root:xnu-8020.101.4~15/RELEASE_ARM64_T6000 arm64
Output of
java -version
openjdk 17.0.2 2022-01-18 OpenJDK Runtime Environment Temurin-17.0.2+8 (build 17.0.2+8) OpenJDK 64-Bit Server VM Temurin-17.0.2+8 (build 17.0.2+8, mixed mode)
GraalVM version (if different from Java)
No response
Quarkus version or git rev
2.8.2.Final
Build tool (ie. output of
mvnw --version
orgradlew --version
)Apache Maven 3.8.4 (9b656c72d54e5bacbed989b64718c159fe39b537) Maven home: /Users/xxx/.m2/wrapper/dists/apache-maven-3.8.4-bin/52ccbt68d252mdldqsfsn03jlf/apache-maven-3.8.4 Java version: 17.0.2, vendor: Eclipse Adoptium, runtime: /Users/xxx/Library/Java/JavaVirtualMachines/temurin-17.0.2/Contents/Home Default locale: de_DE, platform encoding: UTF-8 OS name: "mac os x", version: "12.3.1", arch: "aarch64", family: "mac"
Additional information
No response
The text was updated successfully, but these errors were encountered: