All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.wavefront.sdk.common.clients.WavefrontClientFactory Maven / Gradle / Ivy

package com.wavefront.sdk.common.clients;

import com.wavefront.sdk.common.WavefrontSender;
import com.wavefront.sdk.common.annotation.Nullable;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class WavefrontClientFactory {
  private static final String PROXY_SCHEME = "proxy";
  private static final String HTTP_PROXY_SCHEME = "http";
  private static final String DIRECT_DATA_INGESTION_SCHEME = "https";

  private List clients = new ArrayList<>();
  private static Logger log = Logger.getLogger(WavefrontClientFactory.class.getCanonicalName());


  /**
   * Adds an existing WavefrontSender that is configured to
   * forward points to a proxy or directly to a Wavefront service.
   *
   * @param sender the sender to add to this factory
   *
   * @return
   * Returns {@link WavefrontClientFactory} so that more clients may be initialized
   */
  public WavefrontClientFactory addClient(WavefrontSender sender) {
    if (!existingClient(sender.getClientId())) {
      clients.add(sender);
    }

    return this;
  }

  /**
   * Adds a new WavefrontSender that will either forward points to a proxy or directly to a Wavefront service.
   * 
* proxy ingestion: proxy://your.proxy.com:port
* direct ingestion: https://[email protected]
*
* * @param url The URL of either yourCluster or your.proxy.com * * @return * Returns {@link WavefrontClientFactory} so that more clients may be initialized */ public WavefrontClientFactory addClient(String url) { return addClient(url, null, null, null, null); } /** * Adds a new WavefrontSender that will either forward points to a proxy or directly to a Wavefront service. *
* proxy ingestion: proxy://your.proxy.com:port
* direct ingestion: https://[email protected]
*
* * @param url The URL of either yourCluster or your.proxy.com * @param batchSize The total metrics, histograms, spans, or span logs to send in a single flush * @param maxQueueSize The total metrics, histograms, spans, or span logs to queue internally before dropping data * @param flushIntervalSeconds How often to flush data upstream * @return * Returns {@link WavefrontClientFactory} so that more clients may be initialized */ public WavefrontClientFactory addClient(String url, @Nullable Integer batchSize, @Nullable Integer maxQueueSize, @Nullable Integer flushIntervalSeconds, @Nullable Integer messageSizeInBytes) { ParsedHostString parsedHostString = getServerAndTokenFromEndpoint(url); if (existingClient(parsedHostString.server)) { throw new UnsupportedOperationException("client with id " + url + " already exists."); } WavefrontClient.Builder builder = new WavefrontClient.Builder(parsedHostString.server, parsedHostString.token); if (batchSize != null) { builder.batchSize(batchSize); } if (maxQueueSize != null) { builder.maxQueueSize(maxQueueSize); } if (flushIntervalSeconds != null) { builder.flushIntervalSeconds(flushIntervalSeconds); } if (messageSizeInBytes != null) { builder.messageSizeBytes(messageSizeInBytes); } clients.add(builder.build()); return this; } /** * * @return {@link WavefrontClient} or {@link WavefrontMultiClient} */ public WavefrontSender getClient() { if (clients.size() == 0) { log.log(Level.WARNING, "Call to getClient without any endpoints having been specified"); return null; } if (clients.size() == 1) { return clients.get(0); } WavefrontMultiClient.Builder builder = new WavefrontMultiClient.Builder(); clients.forEach(builder::withWavefrontSender); return builder.build(); } private boolean existingClient(String server) { return clients.stream().anyMatch(c -> c.getClientId().equals(server)); } private ParsedHostString getServerAndTokenFromEndpoint(String endpoint) { URI uri = URI.create(endpoint); final String token; final String host; if (uri.getScheme().equals(DIRECT_DATA_INGESTION_SCHEME)) { token = uri.getUserInfo(); host = DIRECT_DATA_INGESTION_SCHEME + "://" + uri.getHost(); } else if (uri.getScheme().equals(PROXY_SCHEME) || uri.getScheme().equals(HTTP_PROXY_SCHEME)) { token = null; host = HTTP_PROXY_SCHEME + "://" + uri.getHost() + ":" + uri.getPort(); if (uri.getUserInfo() != null) { log.log(Level.WARNING, "Attempting to send a token over clear-text, dropping token."); } } else { throw new RuntimeException("Unknown scheme specified while attempting to build a client " + uri.getScheme()); } return new ParsedHostString(host, token); } private static class ParsedHostString { final String server; final String token; ParsedHostString(String server, String token) { this.server = server; this.token = token; } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy