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

Integration with micrometer #1263

Open
Paxa opened this issue Aug 5, 2022 · 0 comments
Open

Integration with micrometer #1263

Paxa opened this issue Aug 5, 2022 · 0 comments

Comments

@Paxa
Copy link

Paxa commented Aug 5, 2022

Kinda dirty hack but it works, may be will be useful for someone

Spark creates jetty server after Spark.init() is called
This code try to access private field that holds embedded server and if server not initialized yet then wait 1 sec and try again

This can be also useful to get jetty thread pool status

    public static void bindStarkAsync() {
        new Thread(() -> {
            int retryCount = 0;
            while (retryCount < 20) {
                try {
                    boolean registered = bindStark();
                    if (registered) {
                        break;
                    }
                    retryCount += 1;
                    Thread.sleep(1000);
                } catch (Throwable error) {
                    logger.error("bindStark failed", error);
                }
            }
        }).start();
    }

    @SuppressWarnings("PMD.UnusedAssignment")
    public static boolean bindStark() {
        spark.Service sparkService = null;
        try {
            Method m = Spark.class.getDeclaredMethod("getInstance");
            m.setAccessible(true);
            sparkService = (spark.Service) m.invoke(null);
        } catch (Exception error) {
            logger.error("can not invoke Spark.getInstance()", error);
            return false;
        }
        if (sparkService == null) {
            logger.warn("sparkService is null");
            return false;
        }

        EmbeddedJettyServer embeddedServer = null;
        try {
            embeddedServer = (EmbeddedJettyServer) FieldUtils.readDeclaredField(sparkService, "server", true);
        } catch (Exception error) {
            logger.error("can not read sparkService.server", error);
            return false;
        }
        if (embeddedServer == null) {
            logger.warn("embeddedServer is null");
            return false;
        }

        Server jettyServer = null;
        try {
            jettyServer = (Server) FieldUtils.readDeclaredField(embeddedServer, "server", true);
        } catch (Exception error) {
            logger.error("can not read embeddedServer.server", error);
            return false;
        }
        if (jettyServer == null) {
            logger.warn("jettyServer is null");
            return false;
        }

        JettyConnectionMetrics.addToAllConnectors(jettyServer, metricRegistry);
        new JettyServerThreadPoolMetrics(jettyServer.getThreadPool(), Tags.empty()).bindTo(metricRegistry);

        logger.info("JETTY server registerred to micrometer");
        return true;
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant