Skip to content

Commit

Permalink
Remove legacy code from libpf.UnixTime64 (#252)
Browse files Browse the repository at this point in the history
  • Loading branch information
rockdaboot authored Nov 21, 2024
1 parent 0dac5dd commit 7c5db82
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 65 deletions.
31 changes: 1 addition & 30 deletions libpf/libpf.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ package libpf // import "go.opentelemetry.io/ebpf-profiler/libpf"

import (
"encoding/json"
"fmt"
"math"
"time"
)

Expand All @@ -32,36 +30,9 @@ func NowAsUInt32() uint32 {
return uint32(time.Now().Unix())
}

// UnixTime64 represents nanoseconds or (reduced precision) seconds since epoch.
// UnixTime64 represents nanoseconds since epoch.
type UnixTime64 uint64

func (t UnixTime64) MarshalJSON() ([]byte, error) {
if t > math.MaxUint32 {
// Nanoseconds, ES does not support 'epoch_nanoseconds' so
// we have to pass it a value formatted as 'strict_date_optional_time_nanos'.
out := []byte(fmt.Sprintf("%q",
time.Unix(0, int64(t)).UTC().Format(time.RFC3339Nano)))
return out, nil
}

// Reduced precision seconds-since-the-epoch, ES 'epoch_second' formatter will match these.
out := []byte(fmt.Sprintf("%d", t))
return out, nil
}

// Unix returns the value as seconds since epoch.
func (t UnixTime64) Unix() int64 {
if t > math.MaxUint32 {
// Nanoseconds, convert to seconds-since-the-epoch
return time.Unix(0, int64(t)).Unix()
}

return int64(t)
}

// Compile-time interface checks
var _ json.Marshaler = (*UnixTime64)(nil)

// AddressOrLineno represents a line number in an interpreted file or an offset into
// a native file.
type AddressOrLineno uint64
Expand Down
35 changes: 0 additions & 35 deletions libpf/libpf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@
package libpf

import (
"fmt"
"strconv"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestTraceType(t *testing.T) {
Expand Down Expand Up @@ -45,35 +42,3 @@ func TestTraceType(t *testing.T) {
assert.Equal(t, test.str, test.ty.String())
}
}

func TestUnixTime64_MarshalJSON(t *testing.T) {
tests := []struct {
name string
time UnixTime64
want []byte
}{
{
name: "zero",
time: UnixTime64(0),
want: []byte(strconv.Itoa(0)),
},
{
name: "non-zero, seconds since the epoch",
time: UnixTime64(1710349106),
want: []byte(strconv.Itoa(1710349106)),
},
{
name: "non-zero, nanoseconds since the epoch",
time: UnixTime64(1710349106864964685),
want: []byte(fmt.Sprintf("%q", "2024-03-13T16:58:26.864964685Z")),
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
b, err := test.time.MarshalJSON()
require.NoError(t, err)
assert.Equal(t, test.want, b)
})
}
}

0 comments on commit 7c5db82

Please sign in to comment.