001package org.avaje.metric.elastic;
002
003import okhttp3.OkHttpClient;
004
005import java.util.LinkedHashMap;
006import java.util.Map;
007
008/**
009 * Configuration for pushing metrics to ElasticSearch.
010 */
011public class ElasticReporterConfig {
012
013  private String timestampField = "ts";
014  private String typeField = "type";
015  private String nameField = "name";
016  private String indexType = "metric";
017  private String indexPrefix = "metric-";
018  private String url = "http://localhost:9200";
019  private String templateName = "metric-1";
020
021  /**
022   * Connect timeout - default 10 seconds.
023   */
024  private int connectTimeout = 10;
025
026  /**
027   * Read timeout - default 30 seconds.
028   */
029  private int readTimeout = 30;
030
031  /**
032   * Write timeout - default 30 seconds.
033   */
034  private int writeTimeout = 30;
035
036  private Map<String,String> tags = new LinkedHashMap<>();
037
038  private OkHttpClient client;
039
040  /**
041   * Return the base url for the ElasticSearch instance.
042   */
043  public String getUrl() {
044    return url;
045  }
046
047  /**
048   * Set the base url for the ElasticSearch instance.
049   */
050  public ElasticReporterConfig setUrl(String url) {
051    this.url = normalise(url);
052    return this;
053  }
054
055  /**
056   * Trim trailing slash if supplied.
057   */
058  String normalise(String url) {
059    if (url.endsWith("/")) {
060      url = url.substring(0, url.length()-1);
061    }
062    return url;
063  }
064
065  /**
066   * Return the index type (defaults to "metric").
067   */
068  public String getIndexType() {
069    return indexType;
070  }
071
072  /**
073   * Set the index type.
074   */
075  public ElasticReporterConfig setIndexType(String indexType) {
076    this.indexType = indexType;
077    return this;
078  }
079
080  /**
081   * Return the index name prefix (defaults to "metric-").
082   */
083  public String getIndexPrefix() {
084    return indexPrefix;
085  }
086
087  /**
088   * Set the index name prefix (defaults to "metric-").
089   */
090  public ElasticReporterConfig setIndexPrefix(String indexPrefix) {
091    this.indexPrefix = indexPrefix;
092    return this;
093  }
094
095  /**
096   * Add a name value pair to include in each metric entry.
097   */
098  public ElasticReporterConfig addTag(String key, String value) {
099    this.tags.put(key, value);
100    return this;
101  }
102
103  /**
104   * Return all the tags.
105   */
106  public Map<String, String> getTags() {
107    return tags;
108  }
109
110  public ElasticReporterConfig setTags(Map<String, String> tags) {
111    this.tags = tags;
112    return this;
113  }
114
115  /**
116   * Return the name of the timestamp field (defaults to "ts").
117   */
118  public String getTimestampField() {
119    return timestampField;
120  }
121
122  /**
123   * Set the name of the timestamp field.
124   */
125  public ElasticReporterConfig setTimestampField(String timestampField) {
126    this.timestampField = timestampField;
127    return this;
128  }
129
130  /**
131   * Return the name of the metric type field (defaults to "type").
132   */
133  public String getTypeField() {
134    return typeField;
135  }
136
137  /**
138   * Set the name of the metric type field (defaults to "type").
139   */
140  public ElasticReporterConfig setTypeField(String typeField) {
141    this.typeField = typeField;
142    return this;
143  }
144
145  /**
146   * Return the name of the field that holds the metric name (defaults to "name").
147   */
148  public String getNameField() {
149    return nameField;
150  }
151
152  /**
153   * Set the name of the field that holds the metric name.
154   */
155  public ElasticReporterConfig setNameField(String nameField) {
156    this.nameField = nameField;
157    return this;
158  }
159
160  /**
161   * Return the name of the elastic template (defaults to "metric-1").
162   */
163  public String getTemplateName() {
164    return templateName;
165  }
166
167  /**
168   * Set the name of the Elastic template.
169   * <p>
170   * Set this to null means the template will not be checked and set on startup.
171   * </p>
172   * <p>
173   * When changing this value you may want to add a template as a resource under
174   * the elastic-template path such that it is checked and set if it does not exist
175   * in ElasticSearch.
176   * </p>
177   */
178  public void setTemplateName(String templateName) {
179    this.templateName = templateName;
180  }
181
182  /**
183   * Return the connect timeout in seconds.
184   */
185  public int getConnectTimeout() {
186    return connectTimeout;
187  }
188
189  /**
190   * Set the connect timeout in seconds.
191   */
192  public ElasticReporterConfig setConnectTimeout(int connectTimeout) {
193    this.connectTimeout = connectTimeout;
194    return this;
195  }
196
197  /**
198   * Return the read timeout in seconds.
199   */
200  public int getReadTimeout() {
201    return readTimeout;
202  }
203
204  /**
205   * Set the read timeout in seconds.
206   */
207  public ElasticReporterConfig setReadTimeout(int readTimeout) {
208    this.readTimeout = readTimeout;
209    return this;
210  }
211
212  /**
213   * Return the write timeout in seconds.
214   */
215  public int getWriteTimeout() {
216    return writeTimeout;
217  }
218
219  /**
220   * Set the write timeout in seconds.
221   */
222  public ElasticReporterConfig setWriteTimeout(int writeTimeout) {
223    this.writeTimeout = writeTimeout;
224    return this;
225  }
226
227  /**
228   * Return the client to use (If null one will be created).
229   */
230  public OkHttpClient getClient() {
231    return client;
232  }
233
234  /**
235   * Set the client to use (If not set one will be created).
236   */
237  public ElasticReporterConfig setClient(OkHttpClient client) {
238    this.client = client;
239    return this;
240  }
241}