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