Java Code Examples for com.netflix.servo.monitor.Monitors#registerObject()

The following examples show how to use com.netflix.servo.monitor.Monitors#registerObject() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MetricAnnotationManualTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenAnnotatedMonitor_whenUpdated_thenDataCollected() throws Exception {
    Monitors.registerObject("testObject", this);
    assertTrue(Monitors.isObjectRegistered("testObject", this));

    updateCount.incrementAndGet();
    updateCount.incrementAndGet();
    SECONDS.sleep(1);

    List<List<Metric>> metrics = observer.getObservations();
    System.out.println(metrics);
    assertThat(metrics, hasSize(greaterThanOrEqualTo(1)));

    Iterator<List<Metric>> metricIterator = metrics.iterator();
    //skip first empty observation
    metricIterator.next();
    while (metricIterator.hasNext()) {
        assertThat(metricIterator.next(), hasItem(hasProperty("config", hasProperty("name", is("integerCounter")))));
    }

}
 
Example 2
Source File: LoadBalancerContext.java    From ribbon with Apache License 2.0 6 votes vote down vote up
/**
 * Set necessary parameters from client configuration and register with Servo monitors.
 */
@Override
public void initWithNiwsConfig(IClientConfig clientConfig) {
    if (clientConfig == null) {
        return;    
    }
    clientName = clientConfig.getClientName();
    if (StringUtils.isEmpty(clientName)) {
        clientName = "default";
    }
    vipAddresses = clientConfig.resolveDeploymentContextbasedVipAddresses();
    maxAutoRetries = clientConfig.getOrDefault(CommonClientConfigKey.MaxAutoRetries);
    maxAutoRetriesNextServer = clientConfig.getOrDefault(CommonClientConfigKey.MaxAutoRetriesNextServer);
    okToRetryOnAllOperations = clientConfig.getOrDefault(CommonClientConfigKey.OkToRetryOnAllOperations);
    defaultRetryHandler = new DefaultLoadBalancerRetryHandler(clientConfig);
    
    tracer = getExecuteTracer();

    Monitors.registerObject("Client_" + clientName, this);
}
 
Example 3
Source File: PrimeConnections.java    From ribbon with Apache License 2.0 6 votes vote down vote up
private void setUp(String name, int maxRetries, 
        long maxTotalTimeToPrimeConnections, String primeConnectionsURI, float primeRatio) {        
    this.name = name;
    this.maxRetries = maxRetries;
    this.maxTotalTimeToPrimeConnections = maxTotalTimeToPrimeConnections;
    this.primeConnectionsURIPath = primeConnectionsURI;        
    this.primeRatio = primeRatio;
    executorService = new ThreadPoolExecutor(1 /* minimum */,
            maxExecutorThreads /* max threads */,
            executorThreadTimeout /*
                                   * timeout - same property as create
                                   * timeout
                                   */, TimeUnit.MILLISECONDS,
            new LinkedBlockingQueue<Runnable>()
            /* Bounded queue with FIFO- bounded to max tasks */,
            new ASyncPrimeConnectionsThreadFactory(name) /*
                                                          * So we can give
                                                          * our Thread a
                                                          * name
                                                          */
    );        
    totalCounter = Monitors.newCounter(name + "_PrimeConnection_TotalCounter");
    successCounter = Monitors.newCounter(name + "_PrimeConnection_SuccessCounter");
    initialPrimeTimer = Monitors.newTimer(name + "_initialPrimeConnectionsTimer", TimeUnit.MILLISECONDS);
    Monitors.registerObject(name + "_PrimeConnection", this);
}
 
Example 4
Source File: ConnectionPool.java    From suro with Apache License 2.0 6 votes vote down vote up
/**
 *
 * @param config Client configuration
 * @param lb LoadBalancer implementation
 */
@Inject
public ConnectionPool(ClientConfig config, ILoadBalancer lb) {
    this.config = config;
    this.lb = lb;

    connectionSweeper = Executors.newScheduledThreadPool(1);
    newConnectionBuilder = Executors.newFixedThreadPool(1);

    Monitors.registerObject(this);

    populationLatch = new CountDownLatch(Math.min(lb.getServerList(true).size(), config.getAsyncSenderThreads()));
    Executors.newSingleThreadExecutor().submit(new Runnable() {
        @Override
        public void run() {
            populateClients();
        }
    });
    try {
        populationLatch.await(populationLatch.getCount() * config.getConnectionTimeout(), TimeUnit.MILLISECONDS);
    } catch (InterruptedException e) {
        logger.error("Exception on CountDownLatch awaiting: " + e.getMessage(), e);
    }
    logger.info("ConnectionPool population finished with the size: " + getPoolSize()
            + ", will continue up to: " + lb.getServerList(true).size());
}
 
Example 5
Source File: BaseLoadBalancer.java    From ribbon with Apache License 2.0 5 votes vote down vote up
/**
 * Register with monitors and start priming connections if it is set.
 */
protected void init() {
    Monitors.registerObject("LoadBalancer_" + name, this);
    // register the rule as it contains metric for available servers count
    Monitors.registerObject("Rule_" + name, this.getRule());
    if (enablePrimingConnections && primeConnections != null) {
        primeConnections.primeConnections(getReachableServers());
    }
}
 
Example 6
Source File: HealthMonitor.java    From Raigad with Apache License 2.0 5 votes vote down vote up
@Inject
public HealthMonitor(IConfiguration config, InstanceManager instanceManager, HttpModule httpModule) {
    super(config);
    this.instanceManager = instanceManager;
    this.httpModule = httpModule;
    healthReporter = new Elasticsearch_HealthReporter();
    discoveryClient = DiscoveryManager.getInstance().getDiscoveryClient();
    Monitors.registerObject(healthReporter);
}
 
Example 7
Source File: ClientFactory.java    From ribbon with Apache License 2.0 5 votes vote down vote up
/**
 * Utility method to create client and load balancer (if enabled in client config) given the name and client config.
 * Instances are created using reflection (see {@link #instantiateInstanceWithClientConfig(String, IClientConfig)}
 *
 * @param restClientName
 * @param clientConfig
 * @throws ClientException if any errors occurs in the process, or if the client with the same name already exists
 */
public static synchronized IClient<?, ?> registerClientFromProperties(String restClientName, IClientConfig clientConfig) throws ClientException { 
	IClient<?, ?> client = null;
	ILoadBalancer loadBalancer = null;
	if (simpleClientMap.get(restClientName) != null) {
		throw new ClientException(
				ClientException.ErrorType.GENERAL,
				"A Rest Client with this name is already registered. Please use a different name");
	}
	try {
		String clientClassName = clientConfig.getOrDefault(CommonClientConfigKey.ClientClassName);
		client = (IClient<?, ?>) instantiateInstanceWithClientConfig(clientClassName, clientConfig);
		boolean initializeNFLoadBalancer = clientConfig.getOrDefault(CommonClientConfigKey.InitializeNFLoadBalancer);
		if (initializeNFLoadBalancer) {
			loadBalancer = registerNamedLoadBalancerFromclientConfig(restClientName, clientConfig);
		}
		if (client instanceof AbstractLoadBalancerAwareClient) {
			((AbstractLoadBalancerAwareClient) client).setLoadBalancer(loadBalancer);
		}
	} catch (Throwable e) {
		String message = "Unable to InitializeAndAssociateNFLoadBalancer set for RestClient:"
				+ restClientName;
		logger.warn(message, e);
		throw new ClientException(ClientException.ErrorType.CONFIGURATION, 
				message, e);
	}
	simpleClientMap.put(restClientName, client);

	Monitors.registerObject("Client_" + restClientName, client);

	logger.info("Client Registered:" + client.toString());
	return client;
}
 
Example 8
Source File: SnapshotBackupMonitor.java    From Raigad with Apache License 2.0 5 votes vote down vote up
@Inject
public SnapshotBackupMonitor(IConfiguration config, SnapshotBackupManager snapshotBackupManager) {
    super(config);
    snapshotBackupReporter = new Elasticsearch_SnapshotBackupReporter();
    this.snapshotBackupManager = snapshotBackupManager;
    Monitors.registerObject(snapshotBackupReporter);
}
 
Example 9
Source File: NFHttpClient.java    From ribbon with Apache License 2.0 5 votes vote down vote up
void init(IClientConfig config, boolean registerMonitor) {
	HttpParams params = getParams();

	HttpProtocolParams.setContentCharset(params, "UTF-8");  
	params.setParameter(ClientPNames.CONNECTION_MANAGER_FACTORY_CLASS_NAME, 
			ThreadSafeClientConnManager.class.getName());
	HttpClientParams.setRedirecting(params, config.get(CommonClientConfigKey.FollowRedirects, true));
	// set up default headers
	List<Header> defaultHeaders = new ArrayList<Header>();
	defaultHeaders.add(new BasicHeader("Netflix.NFHttpClient.Version", "1.0"));
	defaultHeaders.add(new BasicHeader("X-netflix-httpclientname", name));
	params.setParameter(ClientPNames.DEFAULT_HEADERS, defaultHeaders);

	connPoolCleaner = new ConnectionPoolCleaner(name, this.getConnectionManager(), connectionPoolCleanUpScheduler);

	this.retriesProperty = config.getGlobalProperty(RETRIES.format(name));

	this.sleepTimeFactorMsProperty = config.getGlobalProperty(SLEEP_TIME_FACTOR_MS.format(name));
	setHttpRequestRetryHandler(
			new NFHttpMethodRetryHandler(this.name, this.retriesProperty.getOrDefault(), false,
					this.sleepTimeFactorMsProperty.getOrDefault()));
    tracer = Monitors.newTimer(EXECUTE_TRACER + "-" + name, TimeUnit.MILLISECONDS);
    if (registerMonitor) {
           Monitors.registerObject(name, this);
    }
    maxTotalConnectionProperty = config.getDynamicProperty(CommonClientConfigKey.MaxTotalHttpConnections);
    maxTotalConnectionProperty.onChange(newValue ->
    	((ThreadSafeClientConnManager) getConnectionManager()).setMaxTotal(newValue)
    );

    maxConnectionPerHostProperty = config.getDynamicProperty(CommonClientConfigKey.MaxHttpConnectionsPerHost);
    maxConnectionPerHostProperty.onChange(newValue ->
		((ThreadSafeClientConnManager) getConnectionManager()).setDefaultMaxPerRoute(newValue)
       );

	connIdleEvictTimeMilliSeconds = config.getGlobalProperty(CONN_IDLE_EVICT_TIME_MILLIS.format(name));
}
 
Example 10
Source File: JsonLog4jFormatter.java    From suro with Apache License 2.0 5 votes vote down vote up
@Inject
public JsonLog4jFormatter(ClientConfig config, ObjectMapper jsonMapper) {
    this.config     = config;
    if (jsonMapper == null)
        this.jsonMapper = new DefaultObjectMapper();
    else
        this.jsonMapper = jsonMapper;
    fmt = DateTimeFormat.forPattern(config.getLog4jDateTimeFormat());
    stringFormatter = new StringLog4jFormatter(config);

    Monitors.registerObject(this);
}
 
Example 11
Source File: LocalFileSink.java    From suro with Apache License 2.0 5 votes vote down vote up
@JsonCreator
public LocalFileSink(
        @JsonProperty("outputDir") String outputDir,
        @JsonProperty("writer") FileWriter writer,
        @JsonProperty("notice") Notice notice,
        @JsonProperty("maxFileSize") long maxFileSize,
        @JsonProperty("rotationPeriod") String rotationPeriod,
        @JsonProperty("minPercentFreeDisk") int minPercentFreeDisk,
        @JsonProperty("queue4Sink") MessageQueue4Sink queue4Sink,
        @JsonProperty("batchSize") int batchSize,
        @JsonProperty("batchTimeout") int batchTimeout,
        @JsonProperty("pauseOnLongQueue") boolean pauseOnLongQueue,
        @JacksonInject SpaceChecker spaceChecker) {
    if (!outputDir.endsWith("/")) {
        outputDir += "/";
    }
    Preconditions.checkNotNull(outputDir, "outputDir is needed");

    this.outputDir = outputDir;
    this.writer = writer == null ? new TextFileWriter(null) : writer;
    this.maxFileSize = maxFileSize == 0 ? 200 * 1024 * 1024 : maxFileSize;
    this.rotationPeriod = new Period(rotationPeriod == null ? "PT2m" : rotationPeriod);
    this.minPercentFreeDisk = minPercentFreeDisk == 0 ? 15 : minPercentFreeDisk;
    this.notice = notice == null ? new QueueNotice<String>() : notice;
    this.spaceChecker = spaceChecker;

    Monitors.registerObject(outputDir.replace('/', '_'), this);
    initialize("localfile_" + outputDir.replace('/', '_'),
            queue4Sink == null ? new MemoryQueue4Sink(10000) : queue4Sink,
            batchSize,
            batchTimeout,
            pauseOnLongQueue);
}
 
Example 12
Source File: JvmStatsMonitor.java    From Raigad with Apache License 2.0 4 votes vote down vote up
@Inject
public JvmStatsMonitor(IConfiguration config) {
    super(config);
    jvmStatsReporter = new Elasticsearch_JvmStatsReporter();
    Monitors.registerObject(jvmStatsReporter);
}
 
Example 13
Source File: AsyncSuroClient.java    From suro with Apache License 2.0 4 votes vote down vote up
@Inject
public AsyncSuroClient(
        ClientConfig config,
        Queue4Client messageQueue,
        ConnectionPool connectionPool) {
    this.config = config;
    this.messageQueue = messageQueue;

    this.connectionPool = connectionPool;
    this.builder = new MessageSetBuilder(config)
            .withCompression(Compression.create(config.getCompression()));

    poller.execute(createPoller());

    jobQueue = new ArrayBlockingQueue<Runnable>(config.getAsyncJobQueueCapacity())
    {
        @Override
        public boolean offer(Runnable runnable) {
            try {
                put(runnable); // not to reject the task, slowing down
            } catch (InterruptedException e) {
                // do nothing
            }
            return true;
        }
    }
    ;

    senders = new ThreadPoolExecutor(
            config.getAsyncSenderThreads(), config.getAsyncSenderThreads(),
            10, TimeUnit.SECONDS,
            jobQueue,
            new RejectedExecutionHandler() {
                @Override
                public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                    TMessageSet messageSet = ((AsyncSuroSender) r).getMessageSet();
                    for (Message m : new MessageSetReader(messageSet)) {
                        restore(m);
                    }
                }
            });

    rateLimiter = new RateLimiter(rateLimitMsgPerSec.get());
    Monitors.registerObject(this);
}
 
Example 14
Source File: NodeHealthMonitor.java    From Raigad with Apache License 2.0 4 votes vote down vote up
@Inject
public NodeHealthMonitor(IConfiguration config) {
    super(config);
    healthReporter = new ElasticsearchNodeHealthReporter();
    Monitors.registerObject(healthReporter);
}
 
Example 15
Source File: TransportStatsMonitor.java    From Raigad with Apache License 2.0 4 votes vote down vote up
@Inject
public TransportStatsMonitor(IConfiguration config) {
    super(config);
    transportStatsReporter = new Elasticsearch_TransportStatsReporter();
    Monitors.registerObject(transportStatsReporter);
}
 
Example 16
Source File: AllCircuitBreakerStatsMonitor.java    From Raigad with Apache License 2.0 4 votes vote down vote up
@Inject
public AllCircuitBreakerStatsMonitor(IConfiguration config) {
    super(config);
    allCircuitBreakerStatsReporter = new Elasticsearch_AllCircuitBreakerStatsReporter();
    Monitors.registerObject(allCircuitBreakerStatsReporter);
}
 
Example 17
Source File: HttpStatsMonitor.java    From Raigad with Apache License 2.0 4 votes vote down vote up
@Inject
public HttpStatsMonitor(IConfiguration config) {
    super(config);
    httpStatsReporter = new Elasticsearch_HttpStatsReporter();
    Monitors.registerObject(httpStatsReporter);
}
 
Example 18
Source File: NodeIndicesStatsMonitor.java    From Raigad with Apache License 2.0 4 votes vote down vote up
@Inject
public NodeIndicesStatsMonitor(IConfiguration config) {
    super(config);
    nodeIndicesStatsReporter = new Elasticsearch_NodeIndicesStatsReporter();
    Monitors.registerObject(nodeIndicesStatsReporter);
}
 
Example 19
Source File: KafkaSinkV2.java    From suro with Apache License 2.0 4 votes vote down vote up
@JsonCreator
public KafkaSinkV2(
        @JsonProperty("queue4Sink") MessageQueue4Sink queue4Sink,
        @JsonProperty("client.id") String clientId,
        @JsonProperty("metadata.broker.list") String bootstrapServers,
        @JsonProperty("compression.codec") String codec,
        @JsonProperty("send.buffer.bytes") int sendBufferBytes,
        @JsonProperty("batchSize") int batchSize,
        @JsonProperty("batchTimeout") int batchTimeout,
        @JsonProperty("request.timeout.ms") int requestTimeout,
        @JsonProperty("kafka.etc") Properties etcProps,
        @JsonProperty("keyTopicMap") Map<String, String> keyTopicMap,
        @JsonProperty("jobQueueSize") int jobQueueSize,
        @JsonProperty("corePoolSize") int corePoolSize,
        @JsonProperty("maxPoolSize") int maxPoolSize,
        @JsonProperty("jobTimeout") long jobTimeout,
        @JsonProperty("pauseOnLongQueue") boolean pauseOnLongQueue
) {
    super(jobQueueSize, corePoolSize, maxPoolSize, jobTimeout,
            KafkaSink.class.getSimpleName() + "-" + clientId);

    Preconditions.checkNotNull(bootstrapServers);
    Preconditions.checkNotNull(clientId);

    this.clientId = clientId;
    initialize(
            "kafka_" + clientId,
            queue4Sink == null ? new MemoryQueue4Sink(10000) : queue4Sink,
            batchSize,
            batchTimeout,
            pauseOnLongQueue);

    Properties props = new Properties();
    props.put("client.id", clientId);
    // metadata.broker.list was renamed to bootstrap.servers in the new kafka producer
    props.put("bootstrap.servers", bootstrapServers);
    if (codec != null) {
        props.put("compression.codec", codec);
    }
    if (sendBufferBytes > 0) {
        props.put("send.buffer.bytes", Integer.toString(sendBufferBytes));
    }
    if (requestTimeout > 0) {
        props.put("request.timeout.ms", Integer.toString(requestTimeout));
    }

    if (etcProps != null) {
        props.putAll(etcProps);
    }

    this.keyTopicMap = keyTopicMap != null ? keyTopicMap : Maps.<String, String>newHashMap();

    producer = new KafkaProducer<>(props, new ByteArraySerializer(), new ByteArraySerializer());

    Monitors.registerObject(clientId, this);
}
 
Example 20
Source File: LocalFileSink.java    From suro with Apache License 2.0 3 votes vote down vote up
/**
 * When the disk free space percentage becomes less than minPercentFreeDisk
 * we should stop taking the traffic.
 *
 * @param minPercentFreeDisk minimum percentage of free space
 * @param outputDir
 */
public SpaceChecker(int minPercentFreeDisk, String outputDir) {
    this.minPercentFreeDisk = minPercentFreeDisk;
    this.outputDir = new File(outputDir);

    Monitors.registerObject(this);
}