client_java
client_java copied to clipboard
hotspot provide unregistry all collector
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.
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()?
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.
Can you share sample code of how you are registering the MicrometerCollector?
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
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?
Yes, it will be ok if I remove DefaultExports.initialize().
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.
@fstab How about supporting unregister(String name)? It allows anyone can solve the duplicate issue by hand.
@joey-yoonsung Can you provide a valid use case where this would be required that doesn't involve incorrect coding semantics (duplicate registration) ?