Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

add -db flag for persistent data #155

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ test-go:
run-go:
SERVE_FROM_FS=true cd desktopcollector; go run ./...

.PHONY: run-go-db
run-go-db:
SERVE_FROM_FS=true cd desktopcollector; go run ./... --db /tmp/otel.db

.PHONY: build-js
build-js:
cd desktopexporter/internal/app; npx esbuild --bundle main.tsx main.css --outdir=../server/static
Expand Down
6 changes: 3 additions & 3 deletions desktopcollector/components.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions desktopcollector/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ require (
golang.org/x/sys v0.24.0
)

replace github.com/CtrlSpice/otel-desktop-viewer/desktopexporter => ../desktopexporter

require (
github.com/apache/arrow/go/v17 v17.0.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
Expand Down
7 changes: 6 additions & 1 deletion desktopcollector/main.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions desktopexporter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import (
type Config struct {
// Endpoint defines the host and port where we serve our frontend app
Endpoint string `mapstructure:"endpoint"`
// DBFilename defines the local filesystem path for persistent database storage.
// If set to "", no data will be stored persistently.
DBFilename string
}

// Validate checks if the exporter configuration is valid
Expand Down
2 changes: 1 addition & 1 deletion desktopexporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type desktopExporter struct {
}

func newDesktopExporter(cfg *Config) *desktopExporter {
server := server.NewServer(cfg.Endpoint)
server := server.NewServer(cfg.Endpoint, cfg.DBFilename)
return &desktopExporter{
server: server,
}
Expand Down
13 changes: 8 additions & 5 deletions desktopexporter/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,23 @@ const (
)

// Creates a factory for the Desktop Exporter
func NewFactory() exporter.Factory {
func NewFactory(dbFilename string) exporter.Factory {
Copy link
Contributor

@nopcoder nopcoder Nov 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not qualified to do a full review here. but I think there is no need to add dbFilename as argument here and pass it to createDefaultConfig.
If you add a line in main.go as part of set.ConfigProviderSettings.ResolverSettings.URIs you can set a the value for the db filename using yaml:exporters::desktop::dbfilename: + dbFilenameFlag

Can scope the flag variable to the newCommand and have mapstructure populate the value based on the flag.

This way you can skip the value passing and have the value populated based on the flag.

Hope it helps to simplify the code

return exporter.NewFactory(
metadata.Type,
createDefaultConfig,
createDefaultConfig(dbFilename),
exporter.WithTraces(createTracesExporter, metadata.TracesStability),
exporter.WithMetrics(createMetricsExporter, metadata.MetricsStability),
exporter.WithLogs(createLogsExporter, metadata.LogsStability),
)
}

// Create default configurations
func createDefaultConfig() component.Config {
return &Config{
Endpoint: defaultEndpoint,
func createDefaultConfig(dbFilename string) func() component.Config {
return func() component.Config {
return &Config{
Endpoint: defaultEndpoint,
DBFilename: dbFilename,
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions desktopexporter/internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ type Server struct {
Store *store.Store
}

func NewServer(endpoint string) *Server {
func NewServer(endpoint, dbFilename string) *Server {
s := Server{
server: http.Server{
Addr: endpoint,
},
Store: store.NewStore(context.Background()),
Store: store.NewStore(context.Background(), dbFilename),
}

serveFromFS, err := strconv.ParseBool(os.Getenv("SERVE_FROM_FS"))
Expand Down
4 changes: 2 additions & 2 deletions desktopexporter/internal/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
)

func setupEmpty() (*httptest.Server, func()) {
server := NewServer("localhost:8000")
server := NewServer("localhost:8000", "")
testServer := httptest.NewServer(server.Handler(false))

return testServer, func() {
Expand All @@ -26,7 +26,7 @@ func setupEmpty() (*httptest.Server, func()) {
}

func setupWithTrace(t *testing.T) (*httptest.Server, func(*testing.T)) {
server := NewServer("localhost:8000")
server := NewServer("localhost:8000", "")
testSpanData := telemetry.SpanData{
TraceID: "1234567890",
TraceState: "",
Expand Down
4 changes: 2 additions & 2 deletions desktopexporter/internal/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ type Store struct {
conn driver.Conn
}

func NewStore(ctx context.Context) *Store {
connector, err := duckdb.NewConnector("", nil)
func NewStore(ctx context.Context, dbFilename string) *Store {
connector, err := duckdb.NewConnector(dbFilename, nil)
if err != nil {
log.Fatalf("could not initialize new connector: %s", err.Error())
}
Expand Down