使用 Golang opentelemetry (otel) 覆盖跨度的开始时间和结束时间
Overwrite the StartTime and EndTime of a span with Golang opentelemetry (otel)
我正在为收集的数据生成跟踪 在我进行跟踪之前。这意味着跨度创建时间与实际开始时间不匹配。
我过去曾使用 opentracing
:
做到这一点
span := tracer.StartSpan(
name,
opentracing.StartTime(startTime),
)
我一直无法在 golang otel 库中找到等效项。我的印象是应该可以 based on this quote from the spec:
Start timestamp, default to current time. This argument SHOULD only be set when span creation time has already passed. If API is called at a moment of a Span logical start, API user MUST NOT explicitly set this argument.
“默认”一词意味着应该可以在跨度创建后更改此时间戳。
我试过使用 span.SetAttributes
:
span.SetAttributes(attribute.Int64("StartTime", 0))
但这(不出所料)设置了一个属性:
{
// ...
"StartTime": "2022-04-09T12:28:57.271375+10:00",
"EndTime": "2022-04-09T12:28:57.271417375+10:00",
"Attributes": [
{
"Key": "StartTime",
"Value": {
"Type": "INT64",
"Value": 0
}
},
// ...
}
您需要使用WithTimestamp
。
startTime := time.Now()
ctx, span := tracer.Start(ctx, "foo", trace.WithTimestamp(startTime))
// do your work
span.End(trace.WithTimestamp(time.Now()))
我正在为收集的数据生成跟踪 在我进行跟踪之前。这意味着跨度创建时间与实际开始时间不匹配。
我过去曾使用 opentracing
:
span := tracer.StartSpan(
name,
opentracing.StartTime(startTime),
)
我一直无法在 golang otel 库中找到等效项。我的印象是应该可以 based on this quote from the spec:
Start timestamp, default to current time. This argument SHOULD only be set when span creation time has already passed. If API is called at a moment of a Span logical start, API user MUST NOT explicitly set this argument.
“默认”一词意味着应该可以在跨度创建后更改此时间戳。
我试过使用 span.SetAttributes
:
span.SetAttributes(attribute.Int64("StartTime", 0))
但这(不出所料)设置了一个属性:
{
// ...
"StartTime": "2022-04-09T12:28:57.271375+10:00",
"EndTime": "2022-04-09T12:28:57.271417375+10:00",
"Attributes": [
{
"Key": "StartTime",
"Value": {
"Type": "INT64",
"Value": 0
}
},
// ...
}
您需要使用WithTimestamp
。
startTime := time.Now()
ctx, span := tracer.Start(ctx, "foo", trace.WithTimestamp(startTime))
// do your work
span.End(trace.WithTimestamp(time.Now()))