001package org.avaje.metric.elastic;
002
003import java.util.LinkedHashMap;
004import java.util.Map;
005
006/**
007 * Configuration for pushing metrics to ElasticSearch.
008 */
009public class ElasticReporterConfig {
010
011  private String timestampField = "ts";
012  private String typeField = "type";
013  private String nameField = "name";
014  private String indexType = "metric";
015  private String indexPrefix = "metric-";
016  private String url = "http://localhost:9200";
017  private String templateName = "metric-1";
018
019  private Map<String,String> tags = new LinkedHashMap<>();
020
021  /**
022   * Return the base url for the ElasticSearch instance.
023   */
024  public String getUrl() {
025    return url;
026  }
027
028  /**
029   * Set the base url for the ElasticSearch instance.
030   */
031  public ElasticReporterConfig setUrl(String url) {
032    this.url = normalise(url);
033    return this;
034  }
035
036  /**
037   * Trim trailing slash if supplied.
038   */
039  String normalise(String url) {
040    if (url.endsWith("/")) {
041      url = url.substring(0, url.length()-1);
042    }
043    return url;
044  }
045
046  /**
047   * Return the index type (defaults to "metric").
048   */
049  public String getIndexType() {
050    return indexType;
051  }
052
053  /**
054   * Set the index type.
055   */
056  public ElasticReporterConfig setIndexType(String indexType) {
057    this.indexType = indexType;
058    return this;
059  }
060
061  /**
062   * Return the index name prefix (defaults to "metric-").
063   */
064  public String getIndexPrefix() {
065    return indexPrefix;
066  }
067
068  /**
069   * Set the index name prefix (defaults to "metric-").
070   */
071  public ElasticReporterConfig setIndexPrefix(String indexPrefix) {
072    this.indexPrefix = indexPrefix;
073    return this;
074  }
075
076  /**
077   * Add a name value pair to include in each metric entry.
078   */
079  public ElasticReporterConfig addTag(String key, String value) {
080    this.tags.put(key, value);
081    return this;
082  }
083
084  /**
085   * Return all the tags.
086   */
087  public Map<String, String> getTags() {
088    return tags;
089  }
090
091  public ElasticReporterConfig setTags(Map<String, String> tags) {
092    this.tags = tags;
093    return this;
094  }
095
096  /**
097   * Return the name of the timestamp field (defaults to "ts").
098   */
099  public String getTimestampField() {
100    return timestampField;
101  }
102
103  /**
104   * Set the name of the timestamp field.
105   */
106  public ElasticReporterConfig setTimestampField(String timestampField) {
107    this.timestampField = timestampField;
108    return this;
109  }
110
111  /**
112   * Return the name of the metric type field (defaults to "type").
113   */
114  public String getTypeField() {
115    return typeField;
116  }
117
118  /**
119   * Set the name of the metric type field (defaults to "type").
120   */
121  public ElasticReporterConfig setTypeField(String typeField) {
122    this.typeField = typeField;
123    return this;
124  }
125
126  /**
127   * Return the name of the field that holds the metric name (defaults to "name").
128   */
129  public String getNameField() {
130    return nameField;
131  }
132
133  /**
134   * Set the name of the field that holds the metric name.
135   */
136  public ElasticReporterConfig setNameField(String nameField) {
137    this.nameField = nameField;
138    return this;
139  }
140
141  /**
142   * Return the name of the elastic template (defaults to "metric-1").
143   */
144  public String getTemplateName() {
145    return templateName;
146  }
147
148  /**
149   * Set the name of the Elastic template.
150   * <p>
151   * Set this to null means the template will not be checked and set on startup.
152   * </p>
153   * <p>
154   * When changing this value you may want to add a template as a resource under
155   * the elastic-template path such that it is checked and set if it does not exist
156   * in ElasticSearch.
157   * </p>
158   */
159  public void setTemplateName(String templateName) {
160    this.templateName = templateName;
161  }
162}