Skip to content

Commit

Permalink
[TH2-3724] Parameter for setting time zone for log timestamps (#30)
Browse files Browse the repository at this point in the history
* [TH2-3724] Added parameter to define time zone for timestamp in log file

* [TH2-3724] Update readme and version

* [TH2-3724] Update README.md

Co-authored-by: Nikita Shaposhnikov <[email protected]>

Co-authored-by: Nikita Shaposhnikov <[email protected]>
  • Loading branch information
OptimumCode and Topru333 authored Jun 28, 2022
1 parent d22d1f5 commit 7a8cf00
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 4 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Log Reader User Manual 3.3.3
# Log Reader User Manual 3.4.0

## Document Information

Expand Down Expand Up @@ -43,6 +43,7 @@ spec:
pathFilter: "fileC.*\\.log"
timestampRegexp: "^202.+?(?= QUICK)"
timestampFormat: "yyyy-MM-dd HH:mm:ss"
timestampZone: UTC
D:
regexp: ".*"
pathFilter: "fileC.*\\.log"
Expand Down Expand Up @@ -102,6 +103,7 @@ spec:
+ If the _timestampFormat_ specified the timestamp will be used as a message timestamp. Otherwise, the message's timestamp will be generated.
+ timestampFormat - the format for the timestamp extract from the log's line. **Works only with specified _timestampRegexp_ parameter**.
If _timestampFormat_ is specified the timestamp extract with _timestampRegexp_ will be parsed using this format and used as a message's timestamp.
+ timestampZone - the zone which should be used to process the timestamp from the log file
+ joinGroups - enables joining groups into a message in CSV format. Can be used to extract generic data from the log. Disabled by default.
+ groupsJoinDelimiter - the delimiter that will be used to join groups from the _regexp_ parameter. **Works only if _joinGroups_ is enabled**. The default value is `,`.
+ headersFormat - the headers' definition. The reader uses the keys as headers. The value to the key will be converted to a value for each match in the current line.
Expand Down Expand Up @@ -168,6 +170,10 @@ Output: 8=FIXT.1.1\u00019=66\u000135=A\u000134=1\u000149=NFT2_FIX1\u000156=FGW\u

## Changes

### 3.4.0

+ Add parameter `timestampZone` which should be used to process the timestamp from the log file

### 3.3.3

+ Update common version from `3.16.1` to `3.37.1`
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
release_version=3.3.3
release_version=3.4.0
11 changes: 11 additions & 0 deletions src/main/java/com/exactpro/th2/readlog/LogData.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.exactpro.th2.readlog;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand All @@ -25,6 +26,8 @@ final public class LogData {
private String rawTimestamp;
private LocalDateTime parsedTimestamp;

private ZoneId timestampZone;

public void addBody(String item) {
initIfNeeded();
body.add(item);
Expand All @@ -50,6 +53,14 @@ public void setParsedTimestamp(LocalDateTime localDateTime) {
this.parsedTimestamp = localDateTime;
}

public ZoneId getTimestampZone() {
return timestampZone;
}

public void setTimestampZone(ZoneId timestampZone) {
this.timestampZone = timestampZone;
}

private void initIfNeeded() {
if (body == null) {
body = new ArrayList<>();
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/exactpro/th2/readlog/RegexLogParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public LogData parse(StreamId streamId, String raw) {
DateTimeFormatter timestampFormat = configuration.getTimestampFormat();
if (timestampFormat != null) {
parseTimestamp(timestampFormat, resultData);
resultData.setTimestampZone(configuration.getTimestampZone());
}

return resultData;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import com.exactpro.th2.common.grpc.Direction;
import com.fasterxml.jackson.annotation.JsonPropertyDescription;

import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Collections;
import java.util.EnumMap;
Expand Down Expand Up @@ -54,6 +56,10 @@ public class AliasConfiguration {

private Map<String, String> headersFormat = Collections.emptyMap();

@JsonPropertyDescription("Defines time zone that should be used to parse the timestamp from the log file."
+ "It not set the time zone from the local machine will be taken")
private ZoneId timestampZone;

@JsonCreator
public AliasConfiguration(
@JsonProperty(value = "regexp", required = true) String regexp,
Expand All @@ -73,7 +79,7 @@ public AliasConfiguration(
Pattern.compile(Objects.requireNonNull(directionRegexp, "'Direction regexp' parameter"))));
}
directionToPattern = Collections.unmodifiableMap(patternByDirection);
this.timestampRegexp = timestampRegexp != null ? Pattern.compile(timestampRegexp) : null;
this.timestampRegexp = timestampRegexp == null ? null : Pattern.compile(timestampRegexp);
this.timestampFormat = StringUtils.isEmpty(timestampFormat)
? null
: DateTimeFormatter.ofPattern(timestampFormat);
Expand Down Expand Up @@ -135,4 +141,12 @@ public Map<String, String> getHeadersFormat() {
public void setHeadersFormat(Map<String, String> headersFormat) {
this.headersFormat = new TreeMap<>(headersFormat);
}

public ZoneId getTimestampZone() {
return timestampZone;
}

public void setTimestampZone(ZoneOffset timestampZone) {
this.timestampZone = timestampZone;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ protected List<RawMessage.Builder> lineToMessages(@Nonnull StreamId streamId, @N

private void setupMetadata(RawMessageMetadata.Builder builder, LogData logData) {
if (logData.getParsedTimestamp() != null) {
ZoneOffset currentOffsetForMyZone = ZoneId.systemDefault().getRules().getOffset(Instant.now());
ZoneOffset currentOffsetForMyZone = Objects.requireNonNullElse(
logData.getTimestampZone(),
ZoneId.systemDefault()
).getRules().getOffset(Instant.now());
builder.setTimestamp(MessageUtils.toTimestamp(logData.getParsedTimestamp(),currentOffsetForMyZone));
}
if (logData.getRawTimestamp() != null) {
Expand Down

0 comments on commit 7a8cf00

Please sign in to comment.