Timestamp is null after log_to_metric
Hi!
I am trying to:
- read JSON-formatted log-file (with timestamps attached to each log event)
- parse message (and swap vector timestamp with timestamp from log)
- transform log events to metric preserving original timestamp from log
- output to metric storage that supports backfilling with historical data (Influx/VictoriaMetrics with influx line protocol)
but seems like after log_to_metric (i checked they still there before log_to_metric) timestamps are vanished. When i am deleting timestamp field in original log file, vector-created timestamps successfully passing to stdout.
Do i missing something? I know for statsD https://github.com/timberio/vector/issues/1347 this is architectural decision but why log_to_metric doing this?
As far as i understand this code:
let timestamp = log
.get(&event::log_schema().timestamp_key())
.and_then(Value::as_timestamp)
.cloned();
here: https://github.com/timberio/vector/blob/746792f79f64316214de9fee8d7ba6dfaf6f6cae/src/transforms/log_to_metric.rs#L142
it should copy timestamp from log regardless its origin. Am i right?
vector version:
version="0.9.0" git_version="v0.9.0" released="Mon, 20 Apr 2020 21:15:23 +0000" arch="x86_64"
input file:
{"host":"10.22.11.222","timestamp":"2020-05-06T15:53:09.554068626Z","message":"Sent 200 in 54.2ms","status":200}
{"host":"10.22.11.222","timestamp":"2020-05-06T15:54:09.554068626Z","message":"Sent 200 in 55.1ms","status":200}
{"host":"10.22.11.222","timestamp":"2020-05-06T15:55:09.554068626Z","message":"Sent 200 in 114.2ms","status":200}
config:
data_dir = "./data"
[sources.json_log]
type = "file"
include = ['t.log']
ignore_older = 886400
oldest_first = true
[transforms.parse_message]
type = "json_parser"
inputs = ["json_log"]
drop_field = true
drop_invalid = true
field = "message"
[transforms.log_to_metric]
type = "log_to_metric"
inputs = ["parse_message"]
[[transforms.log_to_metric.metrics]]
type = "counter"
field = "status"
name = "response_total"
tags.status = "{{status}}"
tags.host = "{{host}}"
[sinks.console_debug]
type = "console"
inputs = ["log_to_metric"]
target = "stdout"
encoding.codec = "json"
output:
{"name":"response_total","timestamp":null,"tags":{"host":"10.22.11.222","status":"200"},"kind":"incremental","counter":{"value":1.0}}
{"name":"response_total","timestamp":null,"tags":{"host":"10.22.11.222","status":"200"},"kind":"incremental","counter":{"value":1.0}}
{"name":"response_total","timestamp":null,"tags":{"host":"10.22.11.222","status":"200"},"kind":"incremental","counter":{"value":1.0}}
{"name":"response_total","timestamp":null,"tags":{"host":"10.22.11.222","status":"200"},"kind":"incremental","counter":{"value":1.0}}
@imorph your assumption is correct. We'll have someone investigate and try to rerproduce.
Sorry for the confusion here @imorph, this is definitely something we'd like to handle better.
What's happening here is that when the original timestamp field is overwritten in the json_parser transform, Vector only sees that it's a string value and stores it as such. This causes Value::as_timestamp to return None since it is internally not a timestamp but a string.
One way to fix this is basically to tell Vector how to parse that string into a real timestamp value. You can do that with the coercer transform. Here's what your config looks like with that change:
data_dir = "./data"
[sources.json_log]
type = "file"
include = ['t.log']
ignore_older = 886400
oldest_first = true
[transforms.parse_message]
type = "json_parser"
inputs = ["json_log"]
drop_field = true
drop_invalid = true
field = "message"
[transforms.parse_ts]
type = "coercer"
inputs = ["parse_message"]
types.timestamp = "timestamp|%+"
[transforms.log_to_metric]
type = "log_to_metric"
inputs = ["parse_ts"]
[[transforms.log_to_metric.metrics]]
type = "counter"
field = "status"
name = "response_total"
tags.status = "{{status}}"
tags.host = "{{host}}"
[sinks.console_debug]
type = "console"
inputs = ["log_to_metric"]
target = "stdout"
encoding.codec = "json"
While it works, this is definitely a bit of a bummer from a user experience perspective. We'll very soon be working on some improvements to our internal data model (#2414), and smarter handling of the timestamp field should be included in that.
@lukesteensen thank you!
Hi, any updates ?
Hi @LinTechSo !
The answer above is still relevant: string timestamps can be converted to actual timestamps. The remap transform can be used for this now with the parse_timestamp function: https://vector.dev/docs/reference/vrl/functions/#parse_timestamp . Let me know if your question was something different!
Hi @jszwedko,
I am trying similar use case, but I am getting metric created timestamp (present timestamp) instead of the timestamp present in the log event.
Input { "count": 2, "total": 0, "minimum": 0, "maximum": 0, "average": 0, "timestamp": "2023-10-20T03:00:00.0000000Z", "metricName": "AverageResponseTime", "timeGrain": "1M"}
Config transforms: vector_transform: type: remap inputs: - file_source_1 source: | . = parse_json!(.message)
log_to_metric_transform_1: type: log_to_metric inputs: - vector_transform metrics: - type: gauge field: minimum name: response_time_min tags: resourceId: "{{resourceId}}" timeGrain: "{{timeGrain}}" metricName: "{{metricName}}"
Output {"name":"response_time_min","tags":{"metricName":"AverageResponseTime","timeGrain":"PT1M"},"timestamp":"2023-10-25T10:49:33.837961Z","kind":"absolute","gauge":{"value":0}}
Expected Output {"name":"response_time_min","tags":{"metricName":"AverageResponseTime","timeGrain":"PT1M"},"timestamp":"2023-10-20T03:00:00.0000000Z","kind":"absolute","gauge":{"value":0}}
Please let me how can I get timestamp from the log event without overriding it with current timestamp.
Hi @KrishnaJyothika ,
You'll need to convert the timestamp field to a "real" timestamp with something like:
.timestamp = parse_timestamp(.timestamp, "%+c")
In the same remap you are parsing the input as JSON. The issue you are running into is the same as described above where string timestamps aren't used as timestamps. They need to be parsed/converted to "real timestamps" first.
Thanks @jszwedko, yes figured it out. Used to_timestamp() for timestamp conversion, will also try with parse_timestamp()