Skip to content

Commit

Permalink
Make the document type configurable when writing to ES index (#1328)
Browse files Browse the repository at this point in the history
Resolves #1891

Co-authored-by: Alexander Molnár <malex@sonrisa.hu>
Co-authored-by: Tommy Ludwig <8924140+shakuzen@users.noreply.github.com>
  • Loading branch information
shakuzen and Alexander Molnár authored Mar 17, 2020
2 parents 40e942f + 6002574 commit c076de2
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,17 @@ default String indexDateSeparator() {
String v = get(prefix() + ".indexDateSeparator");
return v == null ? "-" : v;
}

/**
* The type to be used when writing metrics documents to an index.
* This configuration is only used with Elasticsearch versions before 7.
* Default is: "doc"
*
* @return document type
* @since 1.4.0
*/
default String documentType() {
String v = get(prefix() + ".documentType");
return v == null ? "doc" : v;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ static int getMajorVersion(String responseBody) {
}

private String getTypePath() {
return majorVersion < 7 ? "/" + indexType() : TYPE_PATH_AFTER_VERSION_7;
return majorVersion < 7 ? "/" + config.documentType() : TYPE_PATH_AFTER_VERSION_7;
}

// VisibleForTesting
Expand All @@ -285,16 +285,6 @@ static int countCreatedItems(String responseBody) {
return count;
}

/**
* Return index type. Default is 'doc'
* @implNote this only applies to Elasticsearch versions before 7.
* @return index type.
* @since 1.4.0
*/
protected String indexType() {
return "doc";
}

/**
* Return index name.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,22 +281,15 @@ void getVersionWhenVersionIs5AndNotPrettyPrinted() {
assertThat(ElasticMeterRegistry.getMajorVersion(responseBody)).isEqualTo(5);
}

@Issue("#1891")
@Test
void canExtendElasticMeterRegistry() {
ElasticMeterRegistry registry = new ElasticMeterRegistry(config, clock) {
@Override
public String indexName() {
return "my-metrics";
}

@Override
public String indexType() {
return "my-metric";
}
};
assertThat(registry.indexName()).isEqualTo("my-metrics");
assertThat(registry.indexType()).isEqualTo("my-metric");
}


Expand Down

0 comments on commit c076de2

Please sign in to comment.