Skip to content

Commit

Permalink
Merge branch 'develop' into 'master'
Browse files Browse the repository at this point in the history
Release 0.52.0-SNAPSHOT

See merge request hercules/hercules!392
  • Loading branch information
gnkoshelev committed Apr 21, 2022
2 parents d0747df + 2e68508 commit 0395de6
Show file tree
Hide file tree
Showing 87 changed files with 1,261 additions and 225 deletions.
2 changes: 1 addition & 1 deletion hercules-application/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>hercules</artifactId>
<groupId>ru.kontur.vostok.hercules</groupId>
<version>0.51.0-SNAPSHOT</version>
<version>0.52.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion hercules-auth/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<artifactId>hercules</artifactId>
<groupId>ru.kontur.vostok.hercules</groupId>
<version>0.51.0-SNAPSHOT</version>
<version>0.52.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion hercules-cassandra-sink/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>hercules</artifactId>
<groupId>ru.kontur.vostok.hercules</groupId>
<version>0.51.0-SNAPSHOT</version>
<version>0.52.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion hercules-cassandra-util/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<artifactId>hercules</artifactId>
<groupId>ru.kontur.vostok.hercules</groupId>
<version>0.51.0-SNAPSHOT</version>
<version>0.52.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion hercules-clickhouse-sink/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>hercules</artifactId>
<groupId>ru.kontur.vostok.hercules</groupId>
<version>0.51.0-SNAPSHOT</version>
<version>0.52.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion hercules-clickhouse-util/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>hercules</artifactId>
<groupId>ru.kontur.vostok.hercules</groupId>
<version>0.51.0-SNAPSHOT</version>
<version>0.52.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion hercules-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>hercules</artifactId>
<groupId>ru.kontur.vostok.hercules</groupId>
<version>0.51.0-SNAPSHOT</version>
<version>0.52.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion hercules-configuration/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<artifactId>hercules</artifactId>
<groupId>ru.kontur.vostok.hercules</groupId>
<version>0.51.0-SNAPSHOT</version>
<version>0.52.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion hercules-curator/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>hercules</artifactId>
<groupId>ru.kontur.vostok.hercules</groupId>
<version>0.51.0-SNAPSHOT</version>
<version>0.52.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion hercules-elastic-adapter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>hercules</artifactId>
<groupId>ru.kontur.vostok.hercules</groupId>
<version>0.51.0-SNAPSHOT</version>
<version>0.52.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
31 changes: 31 additions & 0 deletions hercules-elastic-sink/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ default value: `yyyy-MM-dd'T'HH:mm:ss.nnnnnnnnnX`

`sink.sender.elastic.format.file` - path to the mapping file. Can use `resource://log-event.mapping` if sink processes logs. See [MappingLoader](../hercules-json/src/main/java/ru/kontur/vostok/hercules/json/format/MappingLoader.java) for details, required

`sink.sender.elastic.format.ignore.unknown.tags` - should ignore event tags for which no mapping is specified, default value: `false`

##### Elastic Client settings
`sink.sender.elastic.client.hosts` - list of elastic hosts

Expand Down Expand Up @@ -209,3 +211,32 @@ move properties/* to *
# Render structured exception as string stack trace
transform exception to stackTrace using ru.kontur.vostok.hercules.elastic.sink.format.ExceptionToStackTraceTransformer
```

### `indices.json` sample:
```json
[
{
"index": "first-index",
"tagMaps": [
{
"some-tag": "some-value",
"other-tag": "other-value"
},
{
"some-tag": "alternative-value"
}
]
},
{
"index": "second-index",
"tagMaps": [
{
"some-tag": "for-second-index-value"
},
{
"special-tag": "special-value"
}
]
}
]
```
2 changes: 1 addition & 1 deletion hercules-elastic-sink/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>hercules</artifactId>
<groupId>ru.kontur.vostok.hercules</groupId>
<version>0.51.0-SNAPSHOT</version>
<version>0.52.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import ru.kontur.vostok.hercules.protocol.TinyString;
import ru.kontur.vostok.hercules.protocol.Variant;
import ru.kontur.vostok.hercules.protocol.util.ContainerUtil;
import ru.kontur.vostok.hercules.protocol.util.VariantUtil;
import ru.kontur.vostok.hercules.tags.CommonTags;
import ru.kontur.vostok.hercules.tags.LogEventTags;
import ru.kontur.vostok.hercules.util.time.TimeUtil;
Expand Down Expand Up @@ -59,7 +58,7 @@ public static void writeEvent(OutputStream stream, Event event, boolean mergePro
}

if (LogEventTags.EXCEPTION_TAG.getName().equals(tag.getKey())) {
Optional<Container> exception = VariantUtil.extractContainer(tag.getValue());
Optional<Container> exception = ContainerUtil.extractContainer(tag.getValue());
if (exception.isPresent()) {
String stackTrace = StackTraceCreator.createStackTrace(exception.get());
EventToJsonWriter.writeVariantAsField(generator, STACKTRACE_FIELD, Variant.ofString(stackTrace));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,18 @@ class LeproserySender {
private final Lazy<String> sinkGroupId;
private final Lazy<String> sinkSubscription;

private final Meter sentToLeproseryEventCountMeter;
private final Meter sentToLeproseryWithErrorsEventCountMeter;
private final Meter leproseryEventsMeter;
private final Meter leproseryEventsWithErrorsMeter;
private final IndicesMetricsCollector leproseryEventsIndicesMetricsCollector;

LeproserySender(Properties properties, MetricsCollector metricsCollector) {
sentToLeproseryEventCountMeter = metricsCollector.meter("sentToLeproseryEventCount");
sentToLeproseryWithErrorsEventCountMeter = metricsCollector.meter("sentToLeproseryWithErrorsEventCount");
leproseryEventsMeter = metricsCollector.meter("leproseryEvents");
leproseryEventsWithErrorsMeter = metricsCollector.meter("leproseryEventsWithErrors");
leproseryEventsIndicesMetricsCollector = new IndicesMetricsCollector(
"leproseryEvents",
10_000,
metricsCollector
);

this.leproseryStream = PropertiesUtil.get(Props.LEPROSERY_STREAM, properties).get();
this.leproseryIndex = PropertiesUtil.get(Props.LEPROSERY_INDEX, properties).get();
Expand All @@ -100,11 +106,11 @@ public void convertAndSend(Map<ElasticDocument, ValidationResult> eventErrorInfo
gateClient.send(leproseryApiKey, leproseryStream, data);

LOGGER.info("Send to leprosery {} events", count);
sentToLeproseryEventCountMeter.mark(count);
sentToLeproseryWithErrorsEventCountMeter.mark(eventErrorInfos.size() - count);
leproseryEventsMeter.mark(count);
leproseryEventsWithErrorsMeter.mark(eventErrorInfos.size() - count);
} catch (BadRequestException | UnavailableClusterException e) {
LOGGER.error("Leprosery sending error", e);
sentToLeproseryWithErrorsEventCountMeter.mark(count);
leproseryEventsWithErrorsMeter.mark(count);
}
}

Expand All @@ -113,6 +119,7 @@ private List<Event> convert(Map<ElasticDocument, ValidationResult> eventErrorInf
.map(entry -> {
ElasticDocument document = entry.getKey();
ValidationResult validationResult = entry.getValue();
leproseryEventsIndicesMetricsCollector.markEvent(document.index());
return toLeproseryEvent(document, validationResult.error());
})
.filter(Optional::isPresent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,29 @@ public IlmIndexCreator(RestClient restClient) {

@Override
public boolean create(String index) {
return createTemplate(index) && createIndex(index);
boolean result = templateExists(index) || createTemplate(index);
return result && createIndex(index);
}

/**
* <pre>{@code GET _template/${index}
* }</pre>
*
* @param index
* @return
*/
public boolean templateExists(String index) {
try {
Response response =
restClient.performRequest(
"GET",
"/_template/" + index
);
return HttpStatusCodes.isSuccess(response.getStatusLine().getStatusCode());
} catch (IOException ex) {
LOGGER.warn("Cannot check template due to exception", ex);
return false;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package ru.kontur.vostok.hercules.elastic.sink.index;

import ru.kontur.vostok.hercules.configuration.Sources;
import ru.kontur.vostok.hercules.elastic.sink.index.matching.ConfigParser;
import ru.kontur.vostok.hercules.elastic.sink.index.matching.IndexData;
import ru.kontur.vostok.hercules.protocol.Event;
import ru.kontur.vostok.hercules.util.parameter.Parameter;
import ru.kontur.vostok.hercules.util.properties.PropertiesUtil;

import java.util.Optional;
import java.util.Properties;

/**
* Resolves the index name by choice from file with predefined indices and corresponding tag values.
* <p>
* File format rules:
* <ul>
* <li> File content must be in JSON format.
* <li> The content consist of index data array.
* <li> Value of the field {@code index} is an index name.
* <li> Value of the field {@code tagMaps} is an array of tag maps.
* <li> Every tag map consist of set of tag-value pairs.
* <li> Tag value may be present as plain string or regular expressions.
* </ul>
* <p>
* File content sample:
* <pre>{@code
* [
* {
* "index": "first-index",
* "tagMaps": [
* {
* "some-tag": "some-value",
* "outer-tag/inner-tag": "inner-value"
* },
* {
* "some-tag": "alternative-value"
* }
* ]
* },
* {
* "index": "second-index",
* "tagMaps": [
* {
* "some-tag": "for-second-index-value"
* },
* {
* "special-tag": "special-value"
* }
* ]
* }
* ]}</pre>
* The event matches the index if the event contains all tags from at least one of index tag maps. <p>
* If the event matches one or more indices then the name of first of these indices is returned with {@link Optional} wrap. <p>
* If the event doesn't match all indices then the resolver returns empty {@link Optional}. <p>
* <br>
* Example for above file content sample:
* <li> for event with single tag {@code 'some-tag=for-second-index-value'} index {@code 'second-index'} will be resolved,
* <li> for event with single tag {@code 'special-tag=special-value'} index {@code 'second-index'} will be resolved,
* <li> for event with tags {@code 'some-tag=some-value'} and {@code 'outer-tag/inner-tag=inner-value'}
* without other tags, index {@code 'first-index'} will be resolved,
* <li> for event with tags {@code 'some-tag=alternative-value'} and {@code 'special-tag=special-value'}
* without other tags, index {@code 'first-index'} will be resolved,
* <li> for event with tags {@code 'some-tag=some-value'} and {@code 'special-tag=non-special-value'}
* without other tags, no index will be resolved.
*
* @author Petr Demenev
*/
public class MatchingIndexResolver extends IndexResolver {

private final IndexData[] indices;

public MatchingIndexResolver(Properties properties) {
super(properties);
String filePath = PropertiesUtil.get(Props.FILE_PATH, properties).get();
indices = ConfigParser.parse(Sources.load(filePath));
}

@Override
public Optional<String> resolve(Event event) {
for (IndexData index : indices) {
boolean matches = index.getTagMaps().stream().anyMatch(tagMap -> tagMap.matches(event));
if (matches) {
return Optional.of(index.getIndex());
}
}
return Optional.empty();
}

private static class Props {
private static final Parameter<String> FILE_PATH = Parameter.stringParameter("file").
withDefault("file://indices.json").
build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package ru.kontur.vostok.hercules.elastic.sink.index.matching;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import ru.kontur.vostok.hercules.protocol.hpath.HPath;
import ru.kontur.vostok.hercules.util.Maps;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
* Configures MatchingIndexResolver.
* Parses raw index data array from JSON and transform its contents to usable object types
*
* @author Petr Demenev
*/
public class ConfigParser {

public static IndexData[] parse(InputStream in) {
ObjectMapper mapper = new ObjectMapper();
ObjectReader reader = mapper.readerFor(new TypeReference<IndexDataModel[]>() {});
IndexDataModel[] indices;
try {
indices = reader.readValue(in);
} catch (IOException ex) {
throw new IllegalArgumentException("Cannot parse index map", ex);
}
return Arrays.stream(indices).
map(i -> new IndexData(i.getIndex(), transform(i.getTagMaps()))).
toArray(IndexData[]::new);
}

private static List<TagMap> transform(List<Map<String, String>> list) {
return list.stream().
map(sourceMap -> {
Map<HPath, Pattern> resultMap = new HashMap<>(Maps.effectiveHashMapCapacity(sourceMap.size()));
sourceMap.forEach((key, value) -> resultMap.put(
HPath.fromPath(key),
Pattern.compile(value == null ? "null" : value)));
return new TagMap(resultMap);
}).
collect(Collectors.toList());
}
}
Loading

0 comments on commit 0395de6

Please sign in to comment.