Skip to content

Commit

Permalink
add persistence support
Browse files Browse the repository at this point in the history
  • Loading branch information
CtrlSpice committed Nov 27, 2024
1 parent c005761 commit 5a3ed57
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 12 deletions.
6 changes: 2 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
otel-desktop-viewer
desktopexporter/internal/app/node_modules
.vscode/
duck.*
*.db
*.wal
help
# dist/
# distribution/linux/*
# distribution/darwin/*
go.work*
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-db-go
run-db-go:
SERVE_FROM_FS=true cd desktopcollector; go run ./... --db ../duck.db

.PHONY: build-js
build-js:
cd desktopexporter/internal/app; npx esbuild --bundle main.tsx main.css --outdir=../server/static
Expand Down
4 changes: 3 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"`

// Endpoint defines the path of your database file. Setting an enpty string opens DuckDB in in-memory mode
DbPath string `mapstructure:"db"`
}

// 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.DbPath)
return &desktopExporter{
server: server,
}
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 string, dbPath string) *Server {
s := Server{
server: http.Server{
Addr: endpoint,
},
Store: store.NewStore(context.Background()),
Store: store.NewStore(context.Background(), dbPath),
}

serveFromFS, err := strconv.ParseBool(os.Getenv("SERVE_FROM_FS"))
Expand Down
5 changes: 3 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 Expand Up @@ -63,6 +63,7 @@ func setupWithTrace(t *testing.T) (*httptest.Server, func(*testing.T)) {
server.Store.Close()
}
}

func TestTracesHandler(t *testing.T) {
t.Run("Traces Handler (Empty)", func(t *testing.T) {
testServer, teardown := setupEmpty()
Expand Down
5 changes: 3 additions & 2 deletions desktopexporter/internal/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ type Store struct {
conn driver.Conn
}

func NewStore(ctx context.Context) *Store {
connector, err := duckdb.NewConnector("", nil)
func NewStore(ctx context.Context, dbPath string) *Store {
connector, err := duckdb.NewConnector(dbPath, nil)

if err != nil {
log.Fatalf("could not initialize new connector: %s", err.Error())
}
Expand Down
49 changes: 49 additions & 0 deletions desktopexporter/internal/store/store_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package store

import (
"context"
"os"
"testing"

"github.com/CtrlSpice/otel-desktop-viewer/desktopexporter/internal/telemetry"
"github.com/stretchr/testify/assert"
)

func TestPersistence(t *testing.T) {
ctx := context.Background()
store := NewStore(ctx, "./quack.db")

// Check that db file is created properly
_, err := os.Stat("./quack.db")
assert.NoErrorf(t, err, "database file does not exist: %v", err)

// Add sample spans to the store
err = store.AddSpans(ctx, telemetry.NewSampleTelemetry().Spans)
assert.NoErrorf(t, err, "could not add spans to the database: %v", err)

// Get trace summaries and check length
summaries, err := store.GetTraceSummaries(ctx)
if assert.NoErrorf(t, err, "could not get trace summaries: %v", err) {
assert.Len(t, *summaries, 2)
}

// Close store
err = store.Close()
assert.NoErrorf(t, err, "could not close database: %v", err)

// Reopen store from the database file
store = NewStore(ctx, "./quack.db")

// Get a trace by ID and check ID of root span
trace, err := store.GetTrace(ctx, "42957c7c2fca940a0d32a0cdd38c06a4")
if assert.NoErrorf(t, err, "could not get trace: %v", err) {
assert.Equal(t, "37fd1349bf83d330", trace.Spans[0].SpanID)
}

// Clean up
err = store.Close()
assert.NoErrorf(t, err, "could not close database: %v", err)

err = os.Remove("./quack.db")
assert.NoError(t, err, "could not remove database file: %v", err)
}

0 comments on commit 5a3ed57

Please sign in to comment.