-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improve record log formatting (#200)
- Loading branch information
1 parent
58eada8
commit 696f2ff
Showing
17 changed files
with
742 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
package simple_test | ||
|
||
import ( | ||
"time" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
|
||
"github.com/teslamotors/fleet-telemetry/datastore/simple" | ||
logrus "github.com/teslamotors/fleet-telemetry/logger" | ||
"github.com/teslamotors/fleet-telemetry/protos" | ||
"github.com/teslamotors/fleet-telemetry/telemetry" | ||
|
||
"github.com/sirupsen/logrus/hooks/test" | ||
"google.golang.org/protobuf/proto" | ||
"google.golang.org/protobuf/types/known/timestamppb" | ||
) | ||
|
||
var _ = Describe("ProtoLogger", func() { | ||
var ( | ||
protoLogger *simple.ProtoLogger | ||
testLogger *logrus.Logger | ||
hook *test.Hook | ||
config *simple.Config | ||
) | ||
|
||
BeforeEach(func() { | ||
testLogger, hook = logrus.NoOpLogger() | ||
config = &simple.Config{Verbose: false} | ||
protoLogger = simple.NewProtoLogger(config, testLogger).(*simple.ProtoLogger) | ||
}) | ||
|
||
Describe("NewProtoLogger", func() { | ||
It("creates a new ProtoLogger", func() { | ||
Expect(protoLogger).NotTo(BeNil()) | ||
Expect(protoLogger.Config).To(Equal(config)) | ||
}) | ||
}) | ||
|
||
Describe("ProcessReliableAck", func() { | ||
It("does not panic", func() { | ||
entry := &telemetry.Record{} | ||
Expect(func() { protoLogger.ProcessReliableAck(entry) }).NotTo(Panic()) | ||
}) | ||
}) | ||
|
||
Describe("Produce", func() { | ||
var ( | ||
record *telemetry.Record | ||
) | ||
|
||
BeforeEach(func() { | ||
payload := &protos.Payload{ | ||
Vin: "TEST123", | ||
CreatedAt: timestamppb.New(time.Unix(0, 0)), | ||
Data: []*protos.Datum{ | ||
{ | ||
Key: protos.Field_VehicleName, | ||
Value: &protos.Value{ | ||
Value: &protos.Value_StringValue{StringValue: "TestVehicle"}, | ||
}, | ||
}, | ||
{ | ||
Key: protos.Field_Gear, | ||
Value: &protos.Value{ | ||
Value: &protos.Value_ShiftStateValue{ShiftStateValue: protos.ShiftState_ShiftStateD}, | ||
}, | ||
}, | ||
}, | ||
} | ||
payloadBytes, err := proto.Marshal(payload) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
record = &telemetry.Record{ | ||
Vin: "TEST123", | ||
PayloadBytes: payloadBytes, | ||
TxType: "V", | ||
} | ||
}) | ||
|
||
It("logs data", func() { | ||
protoLogger.Produce(record) | ||
|
||
lastLog := hook.LastEntry() | ||
Expect(lastLog.Message).To(Equal("logger_json_unmarshal")) | ||
Expect(lastLog.Data).To(HaveKeyWithValue("vin", "TEST123")) | ||
Expect(lastLog.Data).To(HaveKey("data")) | ||
|
||
data, ok := lastLog.Data["data"].(map[string]interface{}) | ||
Expect(ok).To(BeTrue()) | ||
Expect(data).To(Equal(map[string]interface{}{ | ||
"VehicleName": "TestVehicle", | ||
"Gear": "ShiftStateD", | ||
"Vin": "TEST123", | ||
"CreatedAt": "1970-01-01T00:00:00Z", | ||
})) | ||
}) | ||
|
||
It("logs an error when unmarshaling fails", func() { | ||
record.PayloadBytes = []byte("invalid payload") | ||
protoLogger.Produce(record) | ||
|
||
lastLog := hook.LastEntry() | ||
Expect(lastLog.Message).To(Equal("json_unmarshal_error")) | ||
Expect(lastLog.Data).To(HaveKeyWithValue("vin", "TEST123")) | ||
Expect(lastLog.Data).To(HaveKey("metadata")) | ||
}) | ||
|
||
Context("when verbose set to true", func() { | ||
BeforeEach(func() { | ||
config.Verbose = true | ||
protoLogger = simple.NewProtoLogger(config, testLogger).(*simple.ProtoLogger) | ||
}) | ||
|
||
It("does not include types in the data", func() { | ||
protoLogger.Produce(record) | ||
|
||
data, ok := hook.LastEntry().Data["data"].(map[string]interface{}) | ||
Expect(ok).To(BeTrue()) | ||
Expect(data).To(Equal(map[string]interface{}{ | ||
"VehicleName": map[string]interface{}{"stringValue": "TestVehicle"}, | ||
"Gear": map[string]interface{}{"shiftStateValue": "ShiftStateD"}, | ||
"Vin": "TEST123", | ||
"CreatedAt": "1970-01-01T00:00:00Z", | ||
})) | ||
}) | ||
}) | ||
}) | ||
|
||
Describe("ReportError", func() { | ||
It("succeeds", func() { | ||
Expect(func() { | ||
protoLogger.ReportError("test error", nil, logrus.LogInfo{}) | ||
}).NotTo(Panic()) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package simple_test | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestSimple(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Simple Suite Tests") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package transformers | ||
|
||
import ( | ||
"time" | ||
|
||
logrus "github.com/teslamotors/fleet-telemetry/logger" | ||
"github.com/teslamotors/fleet-telemetry/protos" | ||
) | ||
|
||
func PayloadToMap(payload *protos.Payload, includeTypes bool, logger *logrus.Logger) map[string]interface{} { | ||
convertedPayload := make(map[string]interface{}, len(payload.Data)+2) | ||
convertedPayload["Vin"] = payload.Vin | ||
convertedPayload["CreatedAt"] = payload.CreatedAt.AsTime().Format(time.RFC3339) | ||
|
||
for _, datum := range payload.Data { | ||
if datum == nil || datum.Value == nil { | ||
logger.ActivityLog("unknown_payload_data_type", logrus.LogInfo{"vin": payload.Vin}) | ||
continue | ||
} | ||
name := protos.Field_name[int32(datum.Key.Number())] | ||
value, ok := transformValue(datum.Value.Value, includeTypes) | ||
if !ok { | ||
logger.ActivityLog("unknown_payload_value_data_type", logrus.LogInfo{"name": name, "vin": payload.Vin}) | ||
continue | ||
} | ||
convertedPayload[name] = value | ||
} | ||
|
||
return convertedPayload | ||
} | ||
|
||
func transformValue(value interface{}, includeTypes bool) (interface{}, bool) { | ||
var outputValue interface{} | ||
var outputType string | ||
|
||
// ordered by expected frequency | ||
switch v := value.(type) { | ||
case *protos.Value_StringValue: | ||
outputType = "stringValue" | ||
outputValue = v.StringValue | ||
case *protos.Value_LocationValue: | ||
outputType = "locationValue" | ||
outputValue = map[string]float64{ | ||
"latitude": v.LocationValue.Latitude, | ||
"longitude": v.LocationValue.Longitude, | ||
} | ||
case *protos.Value_FloatValue: | ||
outputType = "floatValue" | ||
outputValue = v.FloatValue | ||
case *protos.Value_IntValue: | ||
outputType = "intValue" | ||
outputValue = v.IntValue | ||
case *protos.Value_DoubleValue: | ||
outputType = "doubleValue" | ||
outputValue = v.DoubleValue | ||
case *protos.Value_LongValue: | ||
outputType = "longValue" | ||
outputValue = v.LongValue | ||
case *protos.Value_BooleanValue: | ||
outputType = "booleanValue" | ||
outputValue = v.BooleanValue | ||
case *protos.Value_Invalid: | ||
outputType = "invalid" | ||
outputValue = "<invalid>" | ||
if includeTypes { | ||
outputValue = true | ||
} | ||
case *protos.Value_ShiftStateValue: | ||
outputType = "shiftStateValue" | ||
outputValue = v.ShiftStateValue.String() | ||
case *protos.Value_ChargingValue: | ||
outputType = "chargingValue" | ||
outputValue = v.ChargingValue.String() | ||
default: | ||
return nil, false | ||
} | ||
|
||
if includeTypes { | ||
return map[string]interface{}{outputType: outputValue}, true | ||
} | ||
|
||
return outputValue, true | ||
} |
Oops, something went wrong.