Skip to content

Commit

Permalink
Merge pull request #46 from SUSE/use_json_raw_message_for_blob_transfer
Browse files Browse the repository at this point in the history
Eliminate unnecessary JSON blob processing
  • Loading branch information
rtamalin authored Aug 2, 2024
2 parents 8dcbeda + 017b1f9 commit 4731af8
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 35 deletions.
17 changes: 2 additions & 15 deletions app/telemetry_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,12 @@ type DefaultTelemetryDataRow struct {
// Embed the common rows
TelemetryDataCommon

DataItem any `json:"dataItem"`
DataItem []byte `json:"dataItem"`
}

func (t *DefaultTelemetryDataRow) Init(dItm *telemetrylib.TelemetryDataItem, bHdr *telemetrylib.TelemetryBundleHeader, tagSetId int64) (err error) {
t.TelemetryDataCommon.Init(dItm, bHdr, tagSetId)

// marshal telemetry data as JSON
jsonData, err := json.Marshal(dItm.TelemetryData)
if err != nil {
slog.Error(
"JSON marshal failed",
slog.Int64("clientId", t.ClientId),
slog.String("telemetryId", t.TelemetryId),
slog.String("timestamp", t.Timestamp),
slog.String("error", err.Error()),
)
return
}
t.DataItem = jsonData
t.DataItem = []byte(dItm.TelemetryData)

return
}
Expand Down
18 changes: 15 additions & 3 deletions app/telemetry_scchwinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,25 @@ func int64Conv(value any) (outValue int64, err error) {
func (t *SccHwInfoTelemetryDataRow) Init(dItm *telemetrylib.TelemetryDataItem, bHdr *telemetrylib.TelemetryBundleHeader, tagSetId int64) (err error) {
t.TelemetryDataCommon.Init(dItm, bHdr, tagSetId)

// unmarshal the provided telemetry JSON blob
var tData map[string]any
err = json.Unmarshal([]byte(dItm.TelemetryData), &tData)
if err != nil {
slog.Error(
"Failed to unmarshal telemetry data JSON blob",
slog.String("telemetryType", t.TelemetryType),
slog.String("error", err.Error()),
)
return
}

hwiName := "hwinfo"
err = checkRequiredMapFieldsExist(dItm.TelemetryData, hwiName, "distro_target")
err = checkRequiredMapFieldsExist(tData, hwiName, "distro_target")
if err != nil {
slog.Error("required data fields missing", slog.String("telemetryType", t.TelemetryType), slog.String("error", err.Error()))
return
}
hwi, ok := dItm.TelemetryData[hwiName].(map[string]any)
hwi, ok := tData[hwiName].(map[string]any)
if !ok {
err := fmt.Errorf("field %q in telemetryType %q data is not a map", hwiName, t.TelemetryType)
return err
Expand All @@ -102,7 +114,7 @@ func (t *SccHwInfoTelemetryDataRow) Init(dItm *telemetrylib.TelemetryDataItem, b
}

t.Hostname = hwi["hostname"].(string)
t.DistroTarget = dItm.TelemetryData["distro_target"].(string)
t.DistroTarget = tData["distro_target"].(string)

t.Cpus, err = int64Conv(hwi["cpus"])
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion server/telemetry-admin/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
telemetry-server
telemetry-admin
28 changes: 12 additions & 16 deletions server/telemetry-server/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func (t *AppTestSuite) TestReportTelemetry() {
// Simulated request handled via the router's ServeHTTP
// Response recorded via the httptest.HttpRecorder

body := createReportPayload(t.T())
body := createReportPayload()

rr, err := postToReportTelemetryHandler(body, "", true, t)
assert.NoError(t.T(), err)
Expand All @@ -136,7 +136,7 @@ func (t *AppTestSuite) TestReportTelemetryCompressedPayloadGZIP() {
// Simulated request handled via the router's ServeHTTP
// Response recorded via the httptest.HttpRecorder

body := createReportPayload(t.T())
body := createReportPayload()

//Compress payload
cbody, err := compressedData([]byte(body), "gzip")
Expand All @@ -161,7 +161,7 @@ func (t *AppTestSuite) TestReportTelemetryCompressedPayloadDeflate() {
// Simulated request handled via the router's ServeHTTP
// Response recorded via the httptest.HttpRecorder

body := createReportPayload(t.T())
body := createReportPayload()

//Compress payload
cbody, err := compressedData([]byte(body), "deflate")
Expand Down Expand Up @@ -447,23 +447,19 @@ func TestAppTestSuite(t *testing.T) {
suite.Run(t, new(AppTestSuite))
}

func createReportPayload(t *testing.T) (reportPayload string) {
func createReportPayload() (reportPayload string) {
// Create 2 dataitems
telemetryType := types.TelemetryType("SLE-SERVER-Test")
itags1 := types.Tags{types.Tag("ikey1=ivalue1"), types.Tag("ikey2")}
itags2 := types.Tags{types.Tag("ikey1=ivalue1")}
payload := `
{
"ItemA": 1,
"ItemB": "b",
"ItemC": "c"
}
`

item1, err := telemetrylib.NewTelemetryDataItem(telemetryType, itags1, []byte(payload))
assert.NoError(t, err)
item2, err := telemetrylib.NewTelemetryDataItem(telemetryType, itags2, []byte(payload))
assert.NoError(t, err)
payload := types.NewTelemetryBlob([]byte(`{
"ItemA": 1,
"ItemB": "b",
"ItemC": "c"
}`))

item1 := telemetrylib.NewTelemetryDataItem(telemetryType, itags1, payload)
item2 := telemetrylib.NewTelemetryDataItem(telemetryType, itags2, payload)

client_id := int64(12345)

Expand Down

0 comments on commit 4731af8

Please sign in to comment.