Skip to content

Commit

Permalink
Try clamping timestamp up to 6 fractions of a second (#56)
Browse files Browse the repository at this point in the history
* Try clamping timestamp up to 6 fractions of a second

* Slight formatting change
  • Loading branch information
StrongestNumber9 authored Jul 20, 2023
1 parent bccba4f commit de2cd52
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 13 deletions.
1 change: 1 addition & 0 deletions etc/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"kubernetes": {
"logdir": "/var/log/containers",
"url": "https://127.0.0.1:8443",
"timezone": "Europe/Helsinki",
"cacheExpireInterval": 900,
"cacheMaxEntries": 4096,
"labels": {
Expand Down
15 changes: 13 additions & 2 deletions src/main/java/com/teragrep/k8s_01/K8SConsumer.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.UUID;
import java.util.concurrent.BlockingQueue;
import java.util.function.Consumer;
Expand All @@ -46,6 +50,8 @@ public class K8SConsumer implements Consumer<FileRecord> {
private final KubernetesCachingAPIClient cacheClient;

private final BlockingQueue<RelpOutput> relpOutputPool;
private static final DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSSSxxx");
private final ZoneId timezoneId;

K8SConsumer(
AppConfig appConfig,
Expand All @@ -55,6 +61,8 @@ public class K8SConsumer implements Consumer<FileRecord> {
this.appConfig = appConfig;
this.cacheClient = cacheClient;
this.relpOutputPool = relpOutputPool;
this.timezoneId = ZoneId.of(appConfig.getKubernetes().getTimezone());
LOGGER.info("Using {} as log timezone", timezoneId);
}
@Override
public void accept(FileRecord record) {
Expand Down Expand Up @@ -124,6 +132,9 @@ public void accept(FileRecord record) {
)
);
}
Instant instant = Instant.parse(log.getTimestamp());
ZonedDateTime zdt = instant.atZone(timezoneId);
String timestamp = zdt.format(format);

LOGGER.trace(
"[{}] Raw record value: {}",
Expand Down Expand Up @@ -173,7 +184,7 @@ public void accept(FileRecord record) {
hostname,
namespace,
podname,
log.getTime()
timestamp
);
}

Expand All @@ -193,7 +204,7 @@ public void accept(FileRecord record) {
dockerMetadata
);
SyslogMessage syslog = new SyslogMessage()
.withTimestamp(log.getTimestamp())
.withTimestamp(timestamp, true)
.withSeverity(Severity.WARNING)
.withHostname(appConfig.getKubernetes().getLabels().getHostname().getPrefix() + hostname)
.withAppName(appConfig.getKubernetes().getLabels().getAppname().getPrefix() + appname)
Expand Down
11 changes: 0 additions & 11 deletions src/main/java/com/teragrep/k8s_01/KubernetesLogFilePOJO.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,6 @@
package com.teragrep.k8s_01;
import com.google.gson.Gson;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;
import java.util.Date;

/* POJO presenting Kubernetes logs, for now used only for getting timestamp */
public class KubernetesLogFilePOJO {
private String log;
private String stream;
Expand All @@ -38,10 +31,6 @@ public String getStream() {
return stream;
}

public String getTime() {
return time;
}

public String getTimestamp() {
return time;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class AppConfigKubernetes {
private AppConfigLabels labels;
private String[] logfiles;
private String url;
private String timezone;

public int getCacheExpireInterval() {
return cacheExpireInterval;
Expand All @@ -50,6 +51,7 @@ public String getUrl() {
public int getCacheMaxEntries() {
return cacheMaxEntries;
}
public String getTimezone() { return timezone; }

@Override
public String toString() {
Expand Down

0 comments on commit de2cd52

Please sign in to comment.