Skip to content

Commit

Permalink
Switch SpanEvent times to use time.Time
Browse files Browse the repository at this point in the history
Use `time.Time`, a timestamp, to identify the start and end time of the
`SpanEvent` instead of representing them as nanoseconds since boot time
in a built-in `int64`.

This is motivate by the need to support things that report timestamps
for start/end times directly. Things like the custom SDK (see #1045).

To support the conversion for probes, the `utils.BootOffsetToTime`
function is added. This converts between the measured nanoseconds since
boot-time that an eBPF program measures, and a timestamp.
  • Loading branch information
MrAlias committed Sep 12, 2024
1 parent 8f33bc4 commit b001760
Show file tree
Hide file tree
Showing 21 changed files with 195 additions and 118 deletions.
5 changes: 3 additions & 2 deletions internal/pkg/instrumentation/bpf/database/sql/probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"go.opentelemetry.io/auto/internal/pkg/instrumentation/context"
"go.opentelemetry.io/auto/internal/pkg/instrumentation/probe"
"go.opentelemetry.io/auto/internal/pkg/instrumentation/utils"
)

//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -target amd64,arm64 -cc clang -cflags $CFLAGS bpf ./bpf/probe.bpf.c
Expand Down Expand Up @@ -96,8 +97,8 @@ func convertEvent(e *event) []*probe.SpanEvent {
return []*probe.SpanEvent{
{
SpanName: "DB",
StartTime: int64(e.StartTime),
EndTime: int64(e.EndTime),
StartTime: utils.BootOffsetToTime(e.StartTime),
EndTime: utils.BootOffsetToTime(e.EndTime),
SpanContext: &sc,
Attributes: []attribute.KeyValue{
semconv.DBQueryText(query),
Expand Down
14 changes: 9 additions & 5 deletions internal/pkg/instrumentation/bpf/database/sql/probe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,23 @@ import (

"go.opentelemetry.io/auto/internal/pkg/instrumentation/context"
"go.opentelemetry.io/auto/internal/pkg/instrumentation/probe"
"go.opentelemetry.io/auto/internal/pkg/instrumentation/utils"
)

func TestProbeConvertEvent(t *testing.T) {
start := time.Now()
start := time.Unix(0, time.Now().UnixNano()) // No wall clock.
end := start.Add(1 * time.Second)

startOffset := utils.TimeToBootOffset(start)
endOffset := utils.TimeToBootOffset(end)

traceID := trace.TraceID{1}
spanID := trace.SpanID{1}

got := convertEvent(&event{
BaseSpanProperties: context.BaseSpanProperties{
StartTime: uint64(start.UnixNano()),
EndTime: uint64(end.UnixNano()),
StartTime: startOffset,
EndTime: endOffset,
SpanContext: context.EBPFSpanContext{TraceID: traceID, SpanID: spanID},
},
// "SELECT * FROM foo"
Expand All @@ -41,8 +45,8 @@ func TestProbeConvertEvent(t *testing.T) {
})
want := &probe.SpanEvent{
SpanName: "DB",
StartTime: int64(start.UnixNano()),
EndTime: int64(end.UnixNano()),
StartTime: start,
EndTime: end,
SpanContext: &sc,
Attributes: []attribute.KeyValue{
semconv.DBQueryText("SELECT * FROM foo"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"go.opentelemetry.io/auto/internal/pkg/instrumentation/context"
"go.opentelemetry.io/auto/internal/pkg/instrumentation/probe"
"go.opentelemetry.io/auto/internal/pkg/instrumentation/utils"
"go.opentelemetry.io/auto/internal/pkg/structfield"
)

Expand Down Expand Up @@ -122,8 +123,8 @@ func convertEvent(e *event) []*probe.SpanEvent {
return []*probe.SpanEvent{
{
SpanName: kafkaConsumerSpanName(topic),
StartTime: int64(e.StartTime),
EndTime: int64(e.EndTime),
StartTime: utils.BootOffsetToTime(e.StartTime),
EndTime: utils.BootOffsetToTime(e.EndTime),
SpanContext: &sc,
ParentSpanContext: pscPtr,
Attributes: attributes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,23 @@ import (

"go.opentelemetry.io/auto/internal/pkg/instrumentation/context"
"go.opentelemetry.io/auto/internal/pkg/instrumentation/probe"
"go.opentelemetry.io/auto/internal/pkg/instrumentation/utils"
)

func TestProbeConvertEvent(t *testing.T) {
start := time.Now()
start := time.Unix(0, time.Now().UnixNano()) // No wall clock.
end := start.Add(1 * time.Second)

startOffset := utils.TimeToBootOffset(start)
endOffset := utils.TimeToBootOffset(end)

traceID := trace.TraceID{1}
spanID := trace.SpanID{1}

got := convertEvent(&event{
BaseSpanProperties: context.BaseSpanProperties{
StartTime: uint64(start.UnixNano()),
EndTime: uint64(end.UnixNano()),
StartTime: startOffset,
EndTime: endOffset,
SpanContext: context.EBPFSpanContext{TraceID: traceID, SpanID: spanID},
},
// topic1
Expand All @@ -47,8 +51,8 @@ func TestProbeConvertEvent(t *testing.T) {
})
want := &probe.SpanEvent{
SpanName: kafkaConsumerSpanName("topic1"),
StartTime: int64(start.UnixNano()),
EndTime: int64(end.UnixNano()),
StartTime: start,
EndTime: end,
SpanContext: &sc,
Attributes: []attribute.KeyValue{
semconv.MessagingSystemKafka,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

"go.opentelemetry.io/auto/internal/pkg/instrumentation/context"
"go.opentelemetry.io/auto/internal/pkg/instrumentation/probe"
"go.opentelemetry.io/auto/internal/pkg/instrumentation/utils"
"go.opentelemetry.io/auto/internal/pkg/structfield"
)

Expand Down Expand Up @@ -138,8 +139,8 @@ func convertEvent(e *event) []*probe.SpanEvent {

res = append(res, &probe.SpanEvent{
SpanName: kafkaProducerSpanName(msgTopic),
StartTime: int64(e.StartTime),
EndTime: int64(e.EndTime),
StartTime: utils.BootOffsetToTime(e.StartTime),
EndTime: utils.BootOffsetToTime(e.EndTime),
SpanContext: &sc,
Attributes: msgAttrs,
ParentSpanContext: pscPtr,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,21 @@ import (

"go.opentelemetry.io/auto/internal/pkg/instrumentation/context"
"go.opentelemetry.io/auto/internal/pkg/instrumentation/probe"
"go.opentelemetry.io/auto/internal/pkg/instrumentation/utils"
)

func TestProbeConvertEvent(t *testing.T) {
start := time.Now()
start := time.Unix(0, time.Now().UnixNano()) // No wall clock.
end := start.Add(1 * time.Second)

startOffset := utils.TimeToBootOffset(start)
endOffset := utils.TimeToBootOffset(end)

traceID := trace.TraceID{1}

got := convertEvent(&event{
StartTime: uint64(start.UnixNano()),
EndTime: uint64(end.UnixNano()),
StartTime: startOffset,
EndTime: endOffset,
Messages: [10]messageAttributes{
{
// topic1
Expand Down Expand Up @@ -63,8 +67,8 @@ func TestProbeConvertEvent(t *testing.T) {
})
want1 := &probe.SpanEvent{
SpanName: kafkaProducerSpanName("topic1"),
StartTime: int64(start.UnixNano()),
EndTime: int64(end.UnixNano()),
StartTime: start,
EndTime: end,
SpanContext: &sc1,
Attributes: []attribute.KeyValue{
semconv.MessagingKafkaMessageKey("key1"),
Expand All @@ -78,8 +82,8 @@ func TestProbeConvertEvent(t *testing.T) {

want2 := &probe.SpanEvent{
SpanName: kafkaProducerSpanName("topic2"),
StartTime: int64(start.UnixNano()),
EndTime: int64(end.UnixNano()),
StartTime: start,
EndTime: end,
SpanContext: &sc2,
Attributes: []attribute.KeyValue{
semconv.MessagingKafkaMessageKey("key2"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"go.opentelemetry.io/auto/internal/pkg/inject"
"go.opentelemetry.io/auto/internal/pkg/instrumentation/probe"
"go.opentelemetry.io/auto/internal/pkg/instrumentation/utils"
"go.opentelemetry.io/auto/internal/pkg/process"
"go.opentelemetry.io/auto/internal/pkg/structfield"

Expand Down Expand Up @@ -207,8 +208,8 @@ func convertEvent(e *event) []*probe.SpanEvent {
return []*probe.SpanEvent{
{
SpanName: spanName,
StartTime: int64(e.StartTime),
EndTime: int64(e.EndTime),
StartTime: utils.BootOffsetToTime(e.StartTime),
EndTime: utils.BootOffsetToTime(e.EndTime),
Attributes: convertAttributes(e.Attributes),
SpanContext: &sc,
ParentSpanContext: pscPtr,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@ import (

"go.opentelemetry.io/auto/internal/pkg/instrumentation/context"
"go.opentelemetry.io/auto/internal/pkg/instrumentation/probe"
"go.opentelemetry.io/auto/internal/pkg/instrumentation/utils"
)

func TestProbeConvertEvent(t *testing.T) {
start := time.Now()
start := time.Unix(0, time.Now().UnixNano()) // No wall clock.
end := start.Add(1 * time.Second)

startOffset := utils.TimeToBootOffset(start)
endOffset := utils.TimeToBootOffset(end)

traceID := trace.TraceID{1}
spanID := trace.SpanID{1}

Expand All @@ -30,8 +34,8 @@ func TestProbeConvertEvent(t *testing.T) {

got := convertEvent(&event{
BaseSpanProperties: context.BaseSpanProperties{
StartTime: uint64(start.UnixNano()),
EndTime: uint64(end.UnixNano()),
StartTime: startOffset,
EndTime: endOffset,
SpanContext: context.EBPFSpanContext{TraceID: traceID, SpanID: spanID},
},
// span name: "Foo"
Expand Down Expand Up @@ -104,8 +108,8 @@ func TestProbeConvertEvent(t *testing.T) {
})
want := &probe.SpanEvent{
SpanName: "Foo",
StartTime: int64(start.UnixNano()),
EndTime: int64(end.UnixNano()),
StartTime: start,
EndTime: end,
SpanContext: &sc,
Attributes: []attribute.KeyValue{
attribute.Bool("bool_key", true),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ func convertEvent(e *event) []*probe.SpanEvent {

event := &probe.SpanEvent{
SpanName: method,
StartTime: int64(e.StartTime),
EndTime: int64(e.EndTime),
StartTime: utils.BootOffsetToTime(e.StartTime),
EndTime: utils.BootOffsetToTime(e.EndTime),
Attributes: attrs,
SpanContext: &sc,
ParentSpanContext: pscPtr,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"go.opentelemetry.io/auto/internal/pkg/inject"
"go.opentelemetry.io/auto/internal/pkg/instrumentation/context"
"go.opentelemetry.io/auto/internal/pkg/instrumentation/probe"
"go.opentelemetry.io/auto/internal/pkg/instrumentation/utils"
"go.opentelemetry.io/auto/internal/pkg/process"
"go.opentelemetry.io/auto/internal/pkg/structfield"
)
Expand Down Expand Up @@ -127,8 +128,8 @@ func convertEvent(e *event) []*probe.SpanEvent {
return []*probe.SpanEvent{
{
SpanName: method,
StartTime: int64(e.StartTime),
EndTime: int64(e.EndTime),
StartTime: utils.BootOffsetToTime(e.StartTime),
EndTime: utils.BootOffsetToTime(e.EndTime),
Attributes: []attribute.KeyValue{
semconv.RPCSystemKey.String("grpc"),
semconv.RPCServiceKey.String(method),
Expand Down
4 changes: 2 additions & 2 deletions internal/pkg/instrumentation/bpf/net/http/client/probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,8 @@ func convertEvent(e *event) []*probe.SpanEvent {

spanEvent := &probe.SpanEvent{
SpanName: method,
StartTime: int64(e.StartTime),
EndTime: int64(e.EndTime),
StartTime: utils.BootOffsetToTime(e.StartTime),
EndTime: utils.BootOffsetToTime(e.EndTime),
SpanContext: &sc,
Attributes: attrs,
ParentSpanContext: pscPtr,
Expand Down
Loading

0 comments on commit b001760

Please sign in to comment.