Skip to content

Commit

Permalink
Fix Jetty metrics instrumentation
Browse files Browse the repository at this point in the history
The factory is being asked to create a server before our configuration
has a chance to kick in.

Relates to #593.
  • Loading branch information
mweirauch committed May 31, 2018
1 parent 289269e commit b9c6781
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,23 @@
package io.micrometer.spring.autoconfigure.web.jetty;

import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.binder.jetty.InstrumentedQueuedThreadPool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.boot.context.embedded.jetty.JettyEmbeddedServletContainerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Collections;

/**
* @author Manabu Matsuzaki
* @author Jon Schneider
* @author Michael Weirauch
*/
@Configuration
@ConditionalOnClass(name = "org.eclipse.jetty.server.Server")
@ConditionalOnMissingClass("org.apache.catalina.startup.Tomcat")
public class JettyMetricsConfiguration {
private final MeterRegistry registry;

public JettyMetricsConfiguration(MeterRegistry registry) {
this.registry = registry;
}

@Autowired(required = false)
public void instrumentJettyThreadPool(JettyEmbeddedServletContainerFactory jettyContainerFactory) {
if (jettyContainerFactory != null) {
jettyContainerFactory.setThreadPool(new InstrumentedQueuedThreadPool(registry, Collections.emptyList()));
}
@Bean
JettyMetricsPostProcessor jettyMetricsPostProcessor(MeterRegistry registry) {
return new JettyMetricsPostProcessor(registry);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* Copyright 2017 Pivotal Software, Inc.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micrometer.spring.autoconfigure.web.jetty;

import java.util.Collections;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.boot.context.embedded.jetty.JettyEmbeddedServletContainerFactory;
import org.springframework.core.Ordered;

import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.binder.jetty.InstrumentedQueuedThreadPool;

/**
* @author Michael Weirauch
*/
public class JettyMetricsPostProcessor implements BeanPostProcessor, Ordered {
private final MeterRegistry registry;

JettyMetricsPostProcessor(MeterRegistry registry) {
this.registry = registry;
}

@Override
public Object postProcessBeforeInitialization(Object bean, String beanName)
throws BeansException {
return bean;
}

@Override
public Object postProcessAfterInitialization(Object bean, String beanName) {
if (bean instanceof JettyEmbeddedServletContainerFactory) {
((JettyEmbeddedServletContainerFactory) bean).setThreadPool(
new InstrumentedQueuedThreadPool(registry, Collections.emptyList()));
}
return bean;
}

@Override
public int getOrder() {
return Ordered.HIGHEST_PRECEDENCE;
}
}

0 comments on commit b9c6781

Please sign in to comment.