Skip to content

Commit

Permalink
Merge pull request #37 from SUSE/finish_switch_to_slog
Browse files Browse the repository at this point in the history
Finish switching from log to slog for telemetry client
  • Loading branch information
rtamalin authored Jul 22, 2024
2 parents f4e158d + e966cc0 commit be124bb
Show file tree
Hide file tree
Showing 8 changed files with 259 additions and 89 deletions.
34 changes: 28 additions & 6 deletions cmd/clientds/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import (
"flag"
"fmt"
"log"
"log/slog"

"github.com/SUSE/telemetry/pkg/client"
"github.com/SUSE/telemetry/pkg/config"
Expand Down Expand Up @@ -34,7 +34,12 @@ func main() {

cfg, err := config.NewConfig(opts.config)
if err != nil {
log.Fatal(err)
slog.Error(
"Failed to load specified config",
slog.String("config", opts.config),
slog.String("Error", err.Error()),
)
panic(err)
}
fmt.Printf("Config: %+v\n", cfg)

Expand All @@ -48,15 +53,24 @@ func main() {

tc, err := client.NewTelemetryClient(cfg)
if err != nil {
log.Fatal(err)
slog.Error(
"Failed to instantiate TelemetryClient",
slog.String("config", opts.config),
slog.String("Error", err.Error()),
)
panic(err)
}

processor := tc.Processor()

if opts.items {
itemRows, err := processor.GetItemRows()
if err != nil {
log.Fatal(err.Error())
slog.Error(
"Failed to retrieve items from client datastore",
slog.String("error", err.Error()),
)
panic(err)
}

itemCount := len(itemRows)
Expand All @@ -71,7 +85,11 @@ func main() {
if opts.bundles {
bundleRows, err := processor.GetBundleRows()
if err != nil {
log.Fatal(err.Error())
slog.Error(
"Failed to retrieve bundles from client datastore",
slog.String("error", err.Error()),
)
panic(err)
}

bundleCount := len(bundleRows)
Expand All @@ -86,7 +104,11 @@ func main() {
if opts.reports {
reportRows, err := processor.GetReportRows()
if err != nil {
log.Fatal(err.Error())
slog.Error(
"Failed to retrieve reports from client datastore",
slog.String("error", err.Error()),
)
panic(err)
}

reportCount := len(reportRows)
Expand Down
54 changes: 45 additions & 9 deletions cmd/generator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import (
"flag"
"fmt"
"log"
"log/slog"
"os"

"github.com/SUSE/telemetry/pkg/client"
Expand Down Expand Up @@ -40,7 +40,12 @@ func main() {

cfg, err := config.NewConfig(opts.config)
if err != nil {
log.Fatal(err)
slog.Error(
"Failed to load config",
slog.String("config", opts.config),
slog.String("error", err.Error()),
)
panic(err)
}
fmt.Printf("Config: %+v\n", cfg)

Expand All @@ -54,45 +59,76 @@ func main() {

tc, err := client.NewTelemetryClient(cfg)
if err != nil {
log.Fatal(err)
slog.Error(
"Failed to instantiate TelemetryClient",
slog.String("config", opts.config),
slog.String("error", err.Error()),
)
panic(err)
}

err = tc.Register()
if err != nil {
log.Fatal(err)
slog.Error(
"Failed to register TelemetryClient",
slog.String("error", err.Error()),
)
panic(err)
}

for _, jsonFile := range opts.jsonFiles {
jsonContent, err := os.ReadFile(jsonFile)
if err != nil {
log.Fatal(fmt.Errorf("error reading contents of telemetry JSON file: %s", err))
slog.Error(
"Error reading telemetry data",
slog.String("jsonFile", jsonFile),
slog.String("error", err.Error()),
)
panic(err)
}

err = tc.Generate(opts.telemetry, jsonContent, opts.tags)
if err != nil {
log.Fatal(fmt.Errorf("error generating a telemetry data item from JSON file '%s': %s", jsonFile, err))
slog.Error(
"Error generating telemetry data item",
slog.String("jsonFile", jsonFile),
slog.String("error", err.Error()),
)
panic(err)
}
}

// create one or more bundles from available data items
if !opts.nobundles {
if err := tc.CreateBundles(opts.tags); err != nil {
log.Fatal(fmt.Errorf("error telemetry bundles: %s", err))
slog.Error(
"Error creating telemetry bundles",
slog.String("error", err.Error()),
)
panic(err)
}
}

// create one or more reports from available bundles
if !opts.noreports {
if err := tc.CreateReports(opts.tags); err != nil {
log.Fatal(fmt.Errorf("error creating telemetry reports: %s", err))
slog.Error(
"Error creating telemetry reports",
slog.String("error", err.Error()),
)
panic(err)
}
}

// create one or more reports from available bundles and then
// submit available reports.
if !opts.nosubmit {
if err := tc.Submit(); err != nil {
log.Fatal(fmt.Errorf("error submitting telemetry: %s", err))
slog.Error(
"Error submitting telemetry",
slog.String("error", err.Error()),
)
panic(err)
}
}
}
Expand Down
2 changes: 0 additions & 2 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package config
import (
"encoding/json"
"fmt"
"log"
"log/slog"
"os"
"slices"
Expand Down Expand Up @@ -94,7 +93,6 @@ func NewConfig(cfgFile string) (*Config, error) {
return cfg, fmt.Errorf("failed to read contents of config file '%s': %s", cfgFile, err)
}

log.Printf("Contents: %q", contents)
slog.Info("Contents", slog.String("contents", string(contents)))
err = yaml.Unmarshal(contents, &cfg)
if err != nil {
Expand Down
8 changes: 6 additions & 2 deletions pkg/lib/bundles.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package telemetrylib

import (
"database/sql"
"log"
"log/slog"
"strings"

Expand Down Expand Up @@ -136,7 +135,12 @@ func (b *TelemetryBundleRow) Insert(db *sql.DB, itemIDs []int64) (bundleId strin
for _, itemID := range itemIDs {
_, err := db.Exec("UPDATE items SET bundleId = ? WHERE id = ?", b.Id, itemID)
if err != nil {
log.Fatal(err)
slog.Error(
"Failed to update bundleId in item",
slog.Int64("itemId", itemID),
slog.String("error", err.Error()),
)
return "", err
}
}

Expand Down
Loading

0 comments on commit be124bb

Please sign in to comment.