public static void main()

in solr/prometheus-exporter/src/java/org/apache/solr/prometheus/exporter/SolrExporter.java [145:339]


  public static void main(String[] args) {
    Options mainOptions = new Options();

    Option solrUrlOption =
        Option.builder("s")
            .longOpt("solr-url")
            .hasArg()
            .argName("HOST")
            .type(String.class)
            .desc(
                "Specify the Solr base URL when connecting to Solr in standalone mode. If omitted both the -s parameter and the -z parameter, connect to "
                    + DEFAULT_BASE_URL
                    + ".")
            .build();
    mainOptions.addOption(solrUrlOption);

    Option configOption =
        Option.builder()
            .longOpt("config-file")
            .hasArg()
            .argName("CONFIG")
            .type(String.class)
            .desc("Specify the configuration file; the default is " + DEFAULT_CONFIG + ".")
            .build();

    mainOptions.addOption(configOption);

    Option helpOption =
        Option.builder("h").longOpt("help").desc("Prints this help message.").build();
    mainOptions.addOption(helpOption);

    Option clusterIdOption =
        Option.builder()
            .longOpt("cluster-id")
            .hasArg()
            .argName("CLUSTER_ID")
            .type(String.class)
            .desc(
                "Specify a unique identifier for the cluster, which can be used to select between multiple clusters in Grafana. By default this ID will be equal to a hash of the -b or -z argument")
            .build();
    mainOptions.addOption(clusterIdOption);

    Option numThreadsOption =
        Option.builder()
            .longOpt("num-threads")
            .hasArg()
            .argName("NUM_THREADS")
            .type(Integer.class)
            .desc(
                "Specify the number of threads. solr-exporter creates a thread pools for request to Solr. If you need to improve request latency via solr-exporter, you can increase the number of threads; the default is "
                    + DEFAULT_NUM_THREADS
                    + ".")
            .build();
    mainOptions.addOption(numThreadsOption);

    Option portOption =
        Option.builder("p")
            .longOpt("port")
            .hasArg()
            .argName("PORT")
            .type(Integer.class)
            .desc("Specify the solr-exporter HTTP listen port; default is " + DEFAULT_PORT + ".")
            .build();
    mainOptions.addOption(portOption);

    Option scrapeIntervalOption =
        Option.builder()
            .longOpt("scrape-interval")
            .hasArg()
            .argName("SCRAPE_INTERVAL")
            .type(Integer.class)
            .desc(
                "Specify the delay between scraping Solr metrics; the default is "
                    + DEFAULT_SCRAPE_INTERVAL
                    + " seconds.")
            .build();
    mainOptions.addOption(scrapeIntervalOption);

    Option sslOption =
        Option.builder()
            .longOpt("ssl-enabled")
            .type(Boolean.class)
            .desc(
                "Enable TLS connection to Solr. Expects following env variables: SOLR_SSL_KEY_STORE, SOLR_SSL_KEY_STORE_PASSWORD, SOLR_SSL_TRUST_STORE, SOLR_SSL_TRUST_STORE_PASSWORD. Example: --ssl-enabled")
            .build();
    mainOptions.addOption(sslOption);

    Option credentialsOption =
        Option.builder("u")
            .longOpt("credentials")
            .hasArg()
            .argName("CREDENTIALS")
            .type(String.class)
            .desc(
                "Specify the credentials in the format username:password. Example: --credentials solr:SolrRocks")
            .build();
    mainOptions.addOption(credentialsOption);

    Option zkHostOption =
        Option.builder("z")
            .longOpt("zk-host")
            .hasArg()
            .argName("ZK_HOST")
            .type(String.class)
            .desc(
                "Specify the ZooKeeper connection string when connecting to Solr in SolrCloud mode. If omitted both the -s parameter and the -z parameter, connect to "
                    + DEFAULT_BASE_URL
                    + ".")
            .build();
    mainOptions.addOption(zkHostOption);

    Options options = new Options();
    options.addOptions(mainOptions);

    try {
      CommandLineParser parser = new DefaultParser();
      CommandLine commandLine = parser.parse(options, args);

      if (commandLine.hasOption(helpOption)) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp(
            "bin/solr-exporter", "Prometheus exporter for Apache Solr.", mainOptions, null, true);
        return;
      }

      final SolrScrapeConfiguration scrapeConfiguration;
      final String defaultClusterId;
      String zkHost = getZkHost(commandLine, zkHostOption);
      if (zkHost != null && !zkHost.isBlank()) {
        defaultClusterId = makeShortHash(zkHost);
        scrapeConfiguration = SolrScrapeConfiguration.solrCloud(zkHost);
      } else if (commandLine.hasOption(solrUrlOption)) {
        String baseUrl = commandLine.getOptionValue(solrUrlOption);
        defaultClusterId = makeShortHash(baseUrl);
        scrapeConfiguration = SolrScrapeConfiguration.standalone(baseUrl);
      } else {
        String baseUrl = DEFAULT_BASE_URL;
        if (log.isInfoEnabled()) {
          log.info(
              "Neither --{} or --{} parameters provided so assuming solr url is {}",
              solrUrlOption.getLongOpt(),
              zkHostOption.getLongOpt(),
              baseUrl);
        }
        defaultClusterId = makeShortHash(baseUrl);
        scrapeConfiguration = SolrScrapeConfiguration.standalone(baseUrl);
      }

      int port = commandLine.getParsedOptionValue(portOption, DEFAULT_PORT);
      String clusterId = commandLine.getOptionValue(clusterIdOption, defaultClusterId);

      if (commandLine.hasOption(credentialsOption)) {
        String credentials = commandLine.getOptionValue(credentialsOption, DEFAULT_CREDENTIALS);
        if (credentials.indexOf(':') > 0) {
          String[] credentialsArray = credentials.split(":", 2);
          scrapeConfiguration.withBasicAuthCredentials(credentialsArray[0], credentialsArray[1]);
        }
      }

      if (commandLine.hasOption(sslOption)) {
        log.info("SSL ENABLED");

        scrapeConfiguration.withSslConfiguration(
            Path.of(getSystemVariable("SOLR_SSL_KEY_STORE")),
            getSystemVariable("SOLR_SSL_KEY_STORE_PASSWORD"),
            Path.of(getSystemVariable("SOLR_SSL_TRUST_STORE")),
            getSystemVariable("SOLR_SSL_TRUST_STORE_PASSWORD"));
      }

      String configFile = commandLine.getOptionValue(configOption, DEFAULT_CONFIG);
      int numberOfThreads = commandLine.getParsedOptionValue(numThreadsOption, DEFAULT_NUM_THREADS);
      int scrapeInterval =
          commandLine.getParsedOptionValue(scrapeIntervalOption, DEFAULT_SCRAPE_INTERVAL);

      SolrExporter solrExporter =
          new SolrExporter(
              port,
              numberOfThreads,
              scrapeInterval,
              scrapeConfiguration,
              loadMetricsConfiguration(configFile),
              clusterId);

      log.info("Starting Solr Prometheus Exporting on port {}", port);
      solrExporter.start();
      log.info(
          "Solr Prometheus Exporter is running. Collecting metrics for cluster {}: {}",
          clusterId,
          scrapeConfiguration);
    } catch (IOException e) {
      exit(1, "Failed to start Solr Prometheus Exporter: " + e.getMessage());
    } catch (ParseException e) {
      exit(1, "Failed to parse command line arguments: " + e.getMessage());
    }
  }