0

I want to create a new endpoint that extends the existing jhimetrics endpoint (or extend the results of the existing jhimetrics). The application is generated with JHipster. So what I have done is:

  • add the new endpoint to the array in application.yml file, specifically:
management:
    endpoints:
        web:
            base-path: /management
            exposure:
                include: [ ..., "health", "info", "jhimetrics", "roxhens"]
  • created the ExtendedMetricsEndpoint.java with the following content:
// imports, etc...

@Endpoint(id = "roxhens")
public class ExtendedMetricsEndpoint {
    private final JHipsterMetricsEndpoint delegate;
    private final SimpUserRegistry simpUserRegistry;

    public ExtendedMetricsEndpoint(
        JHipsterMetricsEndpoint delegate, 
        SimpUserRegistry simpUserRegistry
    ) {
        this.delegate = delegate;
        this.simpUserRegistry = simpUserRegistry;
    }

    @ReadOperation
    public Map<String, Map> getMetrics() {
        Map<String, Map> metrics = this.delegate.allMetrics();
        HashMap<String, Integer> activeUsers = new HashMap<>();
        activeUsers.put("activeUsers", this.simpUserRegistry.getUserCount());
        metrics.put("customMetrics", new HashMap(activeUsers));
        return metrics;
    }

}
  • created the configuration file for this endpoint:
// imports etc...

@Configuration
@ConditionalOnClass(Timed.class)
@AutoConfigureAfter(JHipsterMetricsEndpointConfiguration.class)
public class ExtendedMetricsEndpointConfiguration {

    @Bean
    @ConditionalOnBean({JHipsterMetricsEndpoint.class, SimpUserRegistry.class})
    @ConditionalOnMissingBean
    @ConditionalOnAvailableEndpoint
    public ExtendedMetricsEndpoint extendedMetricsEndpoint(JHipsterMetricsEndpoint jHipsterMetricsEndpoint, SimpUserRegistry simpUserRegistry) {
        return new ExtendedMetricsEndpoint(jHipsterMetricsEndpoint, simpUserRegistry);
    }
}

What step am I missing here, or what am I doing wrong?

roxhens
  • 508
  • 6
  • 22

1 Answers1

0

I had the same issue and after 2 days of struggle I was able to find a solution which works for me:

@Component
@WebEndpoint(id = "xxxmetrics")
public class XXXMetricsEndpoint {
    private final MeterRegistry meterRegistry;

    public SrnMetricsEndpoint(MeterRegistry meterRegistry) {
        this.meterRegistry = meterRegistry;
    }

    @ReadOperation
    public Map<String, Map> allMetrics() {
        Map<String, Map> stringMapMap = new LinkedHashMap<>();

        return stringMapMap;
    }
}

Application yml:

management:
    endpoints:
        web:
            base-path: /management
            exposure:
                include: [... , 'health', 'info', 'jhimetrics', 'xxxmetrics' ,'metrics', 'logfile']

This way the request: /management/xxxmetrics works.

The spring docs: https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-features.html#production-ready-endpoints-custom

Edit: spring version: 5.1.10, spring-boot-actuator: 2.1.9

bazsoja
  • 85
  • 1
  • 14