com.dhatim.io.dropwizard.metrics.elasticsearch.ElasticSearchReporterFactory.java Source code

Java tutorial

Introduction

Here is the source code for com.dhatim.io.dropwizard.metrics.elasticsearch.ElasticSearchReporterFactory.java

Source

/**
 * Copyright 2016 Dhatim
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.dhatim.io.dropwizard.metrics.elasticsearch;

import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.ScheduledReporter;
import com.fasterxml.jackson.annotation.JsonTypeName;
import io.dropwizard.metrics.BaseReporterFactory;
import java.io.IOException;
import java.util.Arrays;
import java.util.Map;
import org.elasticsearch.metrics.ElasticsearchReporter;
import org.hibernate.validator.constraints.NotEmpty;
import org.hibernate.validator.constraints.Range;

@JsonTypeName("elasticsearch")
public class ElasticSearchReporterFactory extends BaseReporterFactory {

    public static class Server {

        @NotEmpty
        public String host = "localhost";

        @Range(min = 1, max = 49151)
        public int port = 9200;

        public Server() {
        }

        public Server(String host, int port) {
            this.host = host;
            this.port = port;
        }
    }

    public Server[] servers = new Server[] { new Server("localhost", 9200) };

    public String prefix = "";

    public String index = "metrics";

    public String indexDateFormat = "yyyy.MM.dd";

    public Map<String, Object> additionalFields;

    @Override
    public ScheduledReporter build(MetricRegistry registry) {
        try {
            String[] hosts = Arrays.stream(servers).map(s -> s.host + ":" + s.port).toArray(String[]::new);

            return ElasticsearchReporter.forRegistry(registry).hosts(hosts).prefixedWith(prefix).index(index)
                    .indexDateFormat(indexDateFormat).additionalFields(additionalFields).build();
        } catch (IOException e) {
            throw new RuntimeException("can't build elasticsearch reporter", e);
        }
    }
}