Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Implement telemetry client interfaces #36

Merged
merged 1 commit into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# telemetry
SUSE Telemetry Client Library and associated client CLI tools

See [examples](examples/) directory for examples of how to use the
Telemetry Client library.

# What's available

## cmd/generator
Expand Down
17 changes: 16 additions & 1 deletion cmd/clientds/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/SUSE/telemetry/pkg/client"
"github.com/SUSE/telemetry/pkg/config"
"github.com/SUSE/telemetry/pkg/logging"
)

// options is a struct of the options
Expand All @@ -15,23 +16,36 @@ type options struct {
items bool
bundles bool
reports bool
debug bool
}

func (o options) String() string {
return fmt.Sprintf("config=%v, items=%v, bundles=%v, reports=%v", o.config, o.items, o.bundles, o.reports)
return fmt.Sprintf("config=%v, items=%v, bundles=%v, reports=%v, debug=%v", o.config, o.items, o.bundles, o.reports, o.debug)
}

var opts options

func main() {
fmt.Printf("clientds: %s\n", opts)

if err := logging.SetupBasicLogging(opts.debug); err != nil {
panic(err)
}

cfg, err := config.NewConfig(opts.config)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Config: %+v\n", cfg)

lm := logging.NewLogManager()
if opts.debug {
lm.SetLevel("debug")
}
if err := lm.ConfigAndSetup(&cfg.Logging); err != nil {
panic(err)
}

tc, err := client.NewTelemetryClient(cfg)
if err != nil {
log.Fatal(err)
Expand Down Expand Up @@ -86,6 +100,7 @@ func main() {
}

func init() {
flag.BoolVar(&opts.debug, "debug", false, "Enable debug level logging")
flag.StringVar(&opts.config, "config", client.CONFIG_PATH, "Path to config file to read")
flag.BoolVar(&opts.items, "items", false, "Report details on telemetry data items datastore")
flag.BoolVar(&opts.bundles, "bundles", false, "Report details on telemetry bundles datastore")
Expand Down
17 changes: 16 additions & 1 deletion cmd/generator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/SUSE/telemetry/pkg/client"
"github.com/SUSE/telemetry/pkg/config"
"github.com/SUSE/telemetry/pkg/logging"
"github.com/SUSE/telemetry/pkg/types"
)

Expand All @@ -21,23 +22,36 @@ type options struct {
tags types.Tags
telemetry types.TelemetryType
jsonFiles []string
debug bool
}

func (o options) String() string {
return fmt.Sprintf("config=%v, dryrun=%v, tags=%v, telemetry=%v, jsonFiles=%v", o.config, o.dryrun, o.tags, o.telemetry, o.jsonFiles)
return fmt.Sprintf("config=%v, dryrun=%v, tags=%v, telemetry=%v, jsonFiles=%v, debug=%v", o.config, o.dryrun, o.tags, o.telemetry, o.jsonFiles, o.debug)
}

var opts options

func main() {
fmt.Printf("Generator: %s\n", opts)

if err := logging.SetupBasicLogging(opts.debug); err != nil {
panic(err)
}

cfg, err := config.NewConfig(opts.config)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Config: %+v\n", cfg)

lm := logging.NewLogManager()
if opts.debug {
lm.SetLevel("debug")
}
if err := lm.ConfigAndSetup(&cfg.Logging); err != nil {
panic(err)
}

tc, err := client.NewTelemetryClient(cfg)
if err != nil {
log.Fatal(err)
Expand Down Expand Up @@ -85,6 +99,7 @@ func main() {

func init() {
flag.StringVar(&opts.config, "config", client.CONFIG_PATH, "Path to config file to read")
flag.BoolVar(&opts.debug, "debug", false, "Whether to enable debug level logging.")
flag.BoolVar(&opts.dryrun, "dryrun", false, "Process provided JSON files but do add them to the telemetry staging area.")
flag.BoolVar(&opts.noreports, "noreports", false, "Do not create Telemetry reports")
flag.BoolVar(&opts.nobundles, "nobundles", false, "Do not create Telemetry bundles")
Expand Down
14 changes: 14 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Telemetry Examples
This directory contains examples of:
* telemetry client configuration files
* telemetry data item payloads
* application integration with telemetry client

The [app](app/) directory contains a minimal example of using the
telemetry client from a Go application.

The [config](config/) directory contains example configuration files
for use with these examples.

The [telemetry](telemetry/) directory contains example telemetry data
item JSON blob payloads named for their associated telemetry type.
14 changes: 14 additions & 0 deletions examples/app/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module github.com/SUSE/telemetry/examples/app

go 1.21.9

replace github.com/SUSE/telemetry => ../../

require github.com/SUSE/telemetry v0.0.0-00010101000000-000000000000

require (
github.com/google/uuid v1.6.0 // indirect
github.com/mattn/go-sqlite3 v1.14.22 // indirect
github.com/xyproto/randomstring v1.0.5 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
16 changes: 16 additions & 0 deletions examples/app/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/xyproto/randomstring v1.0.5 h1:YtlWPoRdgMu3NZtP45drfy1GKoojuR7hmRcnhZqKjWU=
github.com/xyproto/randomstring v1.0.5/go.mod h1:rgmS5DeNXLivK7YprL0pY+lTuhNQW3iGxZ18UQApw/E=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
88 changes: 88 additions & 0 deletions examples/app/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package main

import (
"encoding/json"
"fmt"
"log/slog"

"github.com/SUSE/telemetry"
)

func status_check_example() {
status := telemetry.Status()
slog.Info("Telemetry Client", slog.String("status", status.String()))
}

type SomeTelemetryType struct {
Version int `json:"version"`
Application string `json:"application"`
Fields []string `josn:"fields"`
}

func (stt *SomeTelemetryType) String() string {
return fmt.Sprintf("%+v", *stt)
}

func telemetry_content() []byte {
appTelemetry := SomeTelemetryType{
Version: 1,
Application: "specific",
Fields: []string{
"Of",
"Data",
},
}

content, err := json.Marshal(appTelemetry)
if err != nil {
slog.Error(
"Failed to json.Marshal() content",
slog.Any("telemetry", appTelemetry),
slog.String("error", err.Error()),
)
panic(fmt.Errorf("json.Marshal() failed: %s", err.Error()))
}

return content
}

func generate_telemetry_example() {
telemetryType := telemetry.TelemetryType("SOME-TELEMETRY-TYPE")
class := telemetry.MANDATORY_TELEMETRY
content := telemetry_content()
tags := telemetry.Tags{}
flags := telemetry.GENERATE | telemetry.SUBMIT

// verify the telemetry type
if valid, err := telemetryType.Valid(); !valid {
slog.Error(
"Invalid telemetry type",
slog.Any("type", telemetryType),
slog.String("error", err.Error()),
)
return
}

err := telemetry.Generate(
telemetryType,
class,
content,
tags,
flags)
if err != nil {
slog.Error(
"Generate() failed",
slog.String("type", telemetryType.String()),
slog.String("class", class.String()),
slog.Any("content", content),
slog.String("tags", tags.String()),
slog.String("flags", flags.String()),
slog.String("error", err.Error()),
)
}
}

func main() {
status_check_example()
generate_telemetry_example()
}
13 changes: 5 additions & 8 deletions examples/config/telemetry.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@ tags: []
datastores:
driver: sqlite3
params: /tmp/telemetry/client/telemetry.db
extras:
some: thing
else:
- one
- two
- three
or:
maybe: 2024
logging:
level: info
location: stderr
style: text
class_options:
opt_out: true
opt_in: false
allow: []
deny: []
42 changes: 42 additions & 0 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"bytes"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"io/fs"
"log/slog"
"net/http"
"os"
Expand Down Expand Up @@ -42,6 +44,30 @@ func NewTelemetryClient(cfg *config.Config) (tc *TelemetryClient, err error) {
return
}

func checkFileExists(filePath string) bool {
slog.Debug("checking for existence", slog.String("filePath", filePath))

if _, err := os.Stat(filePath); err != nil {
if !errors.Is(err, fs.ErrExist) {
slog.Debug(
"failed to stat path",
slog.String("filePath", filePath),
slog.String("error", err.Error()),
)
return false
}
}

return true
}

func checkFileReadAccessible(filePath string) bool {
if _, err := os.Open(filePath); err != nil {
return false
}
return true
}

func ensureInstanceIdExists(instIdPath string) error {

slog.Info("ensuring existence of instIdPath", slog.String("instIdPath", instIdPath))
Expand All @@ -68,6 +94,22 @@ func ensureInstanceIdExists(instIdPath string) error {
return nil
}

func (tc *TelemetryClient) AuthAccessible() bool {
return checkFileReadAccessible(tc.AuthPath())
}

func (tc *TelemetryClient) InstanceIdAccessible() bool {
return checkFileReadAccessible(tc.InstIdPath())
}

func (tc *TelemetryClient) HasAuth() bool {
return checkFileExists(tc.AuthPath())
}

func (tc *TelemetryClient) HasInstanceId() bool {
return checkFileExists(tc.InstIdPath())
}

func (tc *TelemetryClient) Processor() telemetrylib.TelemetryProcessor {
// may want to just make the processor a public field
return tc.processor
Expand Down
Loading