Skip to content

Commit a67a63a

Browse files
authoredJan 24, 2025··
feat: set entity timestamp (at source) if not provided (#20)
Signed-off-by: Michal Fiedorowicz <[email protected]>
1 parent e673170 commit a67a63a

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed
 

‎diode/client.go

+14-4
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ import (
1212
"regexp"
1313
"runtime"
1414
"strings"
15+
"time"
1516

1617
"github.com/google/uuid"
1718
"google.golang.org/grpc"
1819
"google.golang.org/grpc/credentials"
1920
"google.golang.org/grpc/credentials/insecure"
2021
"google.golang.org/grpc/metadata"
22+
"google.golang.org/protobuf/types/known/timestamppb"
2123

2224
"github.com/netboxlabs/diode-sdk-go/diode/v1/diodepb"
2325
)
@@ -230,10 +232,7 @@ func (g *GRPCClient) Close() error {
230232
func (g *GRPCClient) Ingest(ctx context.Context, entities []Entity) (*diodepb.IngestResponse, error) {
231233
stream := defaultStreamName
232234

233-
protoEntities := make([]*diodepb.Entity, 0)
234-
for _, entity := range entities {
235-
protoEntities = append(protoEntities, entity.ConvertToProtoEntity())
236-
}
235+
protoEntities := convertEntitiesToProto(entities)
237236

238237
req := &diodepb.IngestRequest{
239238
Id: uuid.NewString(),
@@ -250,6 +249,17 @@ func (g *GRPCClient) Ingest(ctx context.Context, entities []Entity) (*diodepb.In
250249
return g.client.Ingest(ctx, req)
251250
}
252251

252+
// convertEntitiesToProto converts entities to proto entities
253+
func convertEntitiesToProto(entities []Entity) []*diodepb.Entity {
254+
protoEntities := make([]*diodepb.Entity, 0)
255+
for _, entity := range entities {
256+
entityPb := entity.ConvertToProtoEntity()
257+
entityPb.Timestamp = timestamppb.New(time.Now().UTC())
258+
protoEntities = append(protoEntities, entityPb)
259+
}
260+
return protoEntities
261+
}
262+
253263
// methodUnaryInterceptor returns a gRPC dial option with a unary interceptor
254264
//
255265
// It's used to intercept the client calls and modify the method details.

‎diode/client_test.go

+32
Original file line numberDiff line numberDiff line change
@@ -404,3 +404,35 @@ func TestNewLogger(t *testing.T) {
404404
})
405405
}
406406
}
407+
408+
func TestConvertEntitiesToProto(t *testing.T) {
409+
tests := []struct {
410+
desc string
411+
entities []Entity
412+
}{
413+
{
414+
desc: "empty entities",
415+
entities: nil,
416+
},
417+
{
418+
desc: "non-empty entities",
419+
entities: []Entity{
420+
&Device{Name: String("device-1")},
421+
&Site{Name: String("site-1")},
422+
},
423+
},
424+
}
425+
426+
for _, tt := range tests {
427+
t.Run(tt.desc, func(t *testing.T) {
428+
protoEntities := convertEntitiesToProto(tt.entities)
429+
require.NotNil(t, protoEntities)
430+
assert.Equal(t, len(tt.entities), len(protoEntities))
431+
for _, entityPb := range protoEntities {
432+
assert.NotNil(t, entityPb.Timestamp)
433+
assert.NotZero(t, entityPb.Timestamp.Seconds)
434+
assert.NotZero(t, entityPb.Timestamp.Nanos)
435+
}
436+
})
437+
}
438+
}

0 commit comments

Comments
 (0)
Please sign in to comment.