influxdbinfluxdb-2

"i.micrometer.influx.InfluxMeterRegistry : unable to create database 'mydb': {"code":"unauthorized","message":"unauthorized access"}"


I'm trying out Influxdb for the first and I want to use it to save and fetch the metrics of my application. I have version v2.7.5 installed, I have created an organization, bucket and generated an API token with all access on Influx UI. In my application, I set the MicrometerMetricsOptions, InfluxMeterRegistry, and a Metrics controller but when I call the endpoint I get an error message i.micrometer.influx.InfluxMeterRegistry : unable to create database 'mydb': {"code":"unauthorized","message":"unauthorized access"} . on the UI there's no place to create a db and even when I setDb method on my configuration it's still the same issue. Below is my configuration

@Configuration
public class VertxConfig {

    @Value("${influxdb.api.token}")
    public String authToken;

    @Bean
    @Autowired
    public Vertx vertx(VerticleFactory verticleFactory) {
        MicrometerMetricsOptions metricsOptions = new MicrometerMetricsOptions()
                .setInfluxDbOptions(new VertxInfluxDbOptions()
                        .setUri("http://localhost:8086")
                        .setOrg("Technologies")
                        .setDb("life")
                        .setUserName("life")
                        .setPassword("1234")
                        .setBucket("life")
                        .setToken(authToken)
                        .setEnabled(true))
                .setEnabled(true);

        VertxOptions vertxOptions = new VertxOptions()
                .setMetricsOptions(metricsOptions)
                .setWorkerPoolSize(35);
        Vertx vertx = Vertx.vertx(vertxOptions);
        vertx.registerVerticleFactory(verticleFactory);
        return vertx;
    }
}
@SpringBootApplication
public class MetricsApplication {

    public static void main(String[] args) {
        SpringApplication springApplication = new SpringApplication(MetricsApplication.class);
        springApplication.setWebApplicationType(WebApplicationType.NONE);
        springApplication.setAdditionalProfiles("prod");
        springApplication.run(args);


        InfluxConfig config = new InfluxConfig() {
            @Override
            public String get(String key) {
                return switch (key) {
                    case "influxdb.uri" -> "http://localhost:8086";
//                  case "influxdb.db" -> "life";
                    case "influxdb.userName" -> "life";
                    case "influxdb.password" -> "1234";
                    default -> null;
                };
            }
        };


        InfluxMeterRegistry registry = InfluxMeterRegistry.builder(config)
                .clock(Clock.SYSTEM)
                .build();

        registry.config().meterFilter(new MeterFilter() {
            @Override
            public DistributionStatisticConfig configure(Meter.Id id, DistributionStatisticConfig config) {
                return DistributionStatisticConfig.builder()
                        .percentilesHistogram(true)
                        .build()
                        .merge(config);
            }
        });
    }

}

Solution

  • I figured the problem was the InfluxMeterRegistry I implemented in the main method. That's Influxdb v1, once I removed it the error was gone. The InfluxMeterRegistry is configured in v2. What's needed for Influxdb V2 configuration is Uri, organization or organizationId, bucket, and token. Then you can visualize metrics stored in Influxdb with tools like Grafana