client_java icon indicating copy to clipboard operation
client_java copied to clipboard

hotspot provide unregistry all collector

Open yangtaoran opened this issue 3 years ago • 9 comments
trafficstars

https://github.com/prometheus/client_java/blob/master/simpleclient_hotspot/src/main/java/io/prometheus/client/hotspot/DefaultExports.java#L36 here, I can registry jvm metrics to the defaultRegistry,but sometimes I want to custom some metrics that name are same as jvm metrics.So I will encounter the error like this: process_start_time_seconds is already in use by another Collector of type StandardExports. So I want to unregistry all hotspot jvm metrics from the defaultRegistry.

yangtaoran avatar Jan 20 '22 07:01 yangtaoran

There is no API to unregister the default exports. However, you don't need to call register in the first place. What about removing your call to DefaultExports.register()?

fstab avatar Jan 20 '22 07:01 fstab

The DefaultExports.register() in another framework, I application depend on the framework,but the framework cannot remove DefaultExports.register().However, I want to use micrometer to registry jvm metrics to prometheus defaultRegistry,but the hostpot and micrometer both have same name metrcis.So,I can only choose one of them,then I need unregister all hostpot metrics.

yangtaoran avatar Jan 20 '22 08:01 yangtaoran

Can you share sample code of how you are registering the MicrometerCollector?

dhoard avatar Jan 21 '22 13:01 dhoard

The dependencies:

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>io.micrometer</groupId>
            <artifactId>micrometer-registry-prometheus</artifactId>
        </dependency>
</dependencies>

The application entrypoint:

@SpringBootApplication
public class HelloServerApplication {

    public static void main(String[] args) throws IOException {
        DefaultExports.initialize();
        new HTTPServer("127.0.0.1", 9090);
        SpringApplication.run(HelloServerApplication.class, args);
    }
}

The MicrometerCollector:

@Configuration
public class ActuatorRegistry {

    @Bean
    public PrometheusMeterRegistry createPrometheusMeterRegistry() {
        return new PrometheusMeterRegistry(PrometheusConfig.DEFAULT, CollectorRegistry.defaultRegistry, Clock.SYSTEM);
    }
}

Start the application, and it will throw exception like this:

Unsatisfied dependency expressed through method 'webMvcMetricsFilter' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'createPrometheusMeterRegistry' defined in class path resource [demo/ActuatorRegistry.class]: Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: Failed to register Collector of type MicrometerCollector: process_start_time_seconds is already in use by another Collector of type StandardExports

yangtaoran avatar Jan 24 '22 07:01 yangtaoran

Looks like DefaultExports.initialize() is called a 2nd time somewhere in your dependencies. Does it help to remove the call DefaultExports.initialize() from your main() method?

fstab avatar Jan 24 '22 16:01 fstab

Yes, it will be ok if I remove DefaultExports.initialize().

yangtaoran avatar Jan 25 '22 06:01 yangtaoran

There may be a valid use-case as to why someone wants to unregister a Collector, but I'm not seeing it in your use-case.

dhoard avatar Jan 25 '22 13:01 dhoard

@fstab How about supporting unregister(String name)? It allows anyone can solve the duplicate issue by hand.

joey-yoonsung avatar Sep 28 '22 05:09 joey-yoonsung

@joey-yoonsung Can you provide a valid use case where this would be required that doesn't involve incorrect coding semantics (duplicate registration) ?

dhoard avatar Apr 25 '23 03:04 dhoard