From 470b8fb8b6e2803c3c43f24fb549c158a282e063 Mon Sep 17 00:00:00 2001 From: CtrlSpice Date: Thu, 28 Nov 2024 13:32:38 -0800 Subject: [PATCH] Implement --dev flag --- Makefile | 4 +-- desktopcollector/main.go | 7 ++++-- desktopexporter/config.go | 6 ++++- desktopexporter/exporter.go | 2 +- desktopexporter/internal/server/server.go | 25 ++++++++----------- .../internal/server/server_test.go | 8 +++--- 6 files changed, 28 insertions(+), 24 deletions(-) diff --git a/Makefile b/Makefile index 933aa52..dcfca82 100644 --- a/Makefile +++ b/Makefile @@ -12,11 +12,11 @@ test-go: .PHONY: run-go run-go: - SERVE_FROM_FS=true cd desktopcollector; go run ./... + cd desktopcollector; go run ./... --dev .PHONY: run-db-go run-db-go: - SERVE_FROM_FS=true cd desktopcollector; go run ./... --db ../duck.db + cd desktopcollector; go run ./... --dev --db ../duck.db .PHONY: build-js build-js: diff --git a/desktopcollector/main.go b/desktopcollector/main.go index 7758a1c..c7d5f08 100644 --- a/desktopcollector/main.go +++ b/desktopcollector/main.go @@ -54,6 +54,7 @@ func runInteractive(params otelcol.CollectorSettings) error { func newCommand(set otelcol.CollectorSettings) *cobra.Command { var httpPortFlag, grpcPortFlag, browserPortFlag int var hostFlag, dbFlag string + var devFlag bool rootCmd := &cobra.Command{ Use: set.BuildInfo.Command, @@ -66,6 +67,7 @@ func newCommand(set otelcol.CollectorSettings) *cobra.Command { `yaml:exporters::desktop:`, `yaml:exporters::desktop::endpoint: ` + hostFlag + `:` + strconv.Itoa(browserPortFlag), `yaml:exporters::desktop::db: ` + dbFlag, + `yaml:exporters::desktop::dev: ` + strconv.FormatBool(devFlag), `yaml:service::pipelines::traces::receivers: [otlp]`, `yaml:service::pipelines::traces::exporters: [desktop]`, `yaml:service::pipelines::metrics::receivers: [otlp]`, @@ -85,8 +87,9 @@ func newCommand(set otelcol.CollectorSettings) *cobra.Command { rootCmd.Flags().IntVar(&httpPortFlag, "http", 4318, "The port number on which we listen for OTLP http payloads") rootCmd.Flags().IntVar(&grpcPortFlag, "grpc", 4317, "The port number on which we listen for OTLP grpc payloads") rootCmd.Flags().IntVar(&browserPortFlag, "browser", 8000, "The port number where we expose our data") - rootCmd.Flags().StringVar(&hostFlag, "host", "localhost", "The host where we expose our all endpoints (OTLP receivers and browser)") - rootCmd.Flags().StringVar(&dbFlag, "db", "", "The path of your database file. Omitting this flag opens DuckDB in in-memory mode, with no data persisted to disk.") + rootCmd.Flags().StringVar(&hostFlag, "host", "localhost", "The host where we expose our endpoints (OTLP receivers and browser)") + rootCmd.Flags().StringVar(&dbFlag, "db", "", "The path of our database file. Omitting this flag opens DuckDB in in-memory mode, with no data persisted to disk.") + rootCmd.Flags().BoolVar(&devFlag, "dev", false, "Avoids recompiling the back-end during front-end development by serving the latter from the filesystem instead of embeddeding it") return rootCmd } diff --git a/desktopexporter/config.go b/desktopexporter/config.go index 84ec334..e858fb0 100644 --- a/desktopexporter/config.go +++ b/desktopexporter/config.go @@ -9,8 +9,12 @@ 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 defines the path of your database file. Setting an enpty string opens DuckDB in in-memory mode DbPath string `mapstructure:"db"` + + // IsDev launches the app in dev mode, which Avoids recompiling the back-end during + // front-end development by serving the latter from the filesystem instead of embeddeding it. + IsDev bool `mapstructure:"dev"` } // Validate checks if the exporter configuration is valid diff --git a/desktopexporter/exporter.go b/desktopexporter/exporter.go index a057859..1be04f2 100644 --- a/desktopexporter/exporter.go +++ b/desktopexporter/exporter.go @@ -20,7 +20,7 @@ type desktopExporter struct { } func newDesktopExporter(cfg *Config) *desktopExporter { - server := server.NewServer(cfg.Endpoint, cfg.DbPath) + server := server.NewServer(cfg.Endpoint, cfg.DbPath, cfg.IsDev) return &desktopExporter{ server: server, } diff --git a/desktopexporter/internal/server/server.go b/desktopexporter/internal/server/server.go index 55f5841..541baa7 100644 --- a/desktopexporter/internal/server/server.go +++ b/desktopexporter/internal/server/server.go @@ -8,7 +8,6 @@ import ( "log" "net/http" "os" - "strconv" "time" "github.com/pkg/browser" @@ -23,22 +22,17 @@ var assets embed.FS type Server struct { server http.Server Store *store.Store + isDev bool } -func NewServer(endpoint string, dbPath string) *Server { +func NewServer(endpoint string, dbPath string, isDev bool) *Server { s := Server{ server: http.Server{ Addr: endpoint, }, Store: store.NewStore(context.Background(), dbPath), } - - serveFromFS, err := strconv.ParseBool(os.Getenv("SERVE_FROM_FS")) - if err != nil { - serveFromFS = false - } - - s.server.Handler = s.Handler(serveFromFS) + s.server.Handler = s.Handler() return &s } @@ -46,6 +40,7 @@ func (s *Server) Start() error { defer s.Store.Close() _, isCI := os.LookupEnv("CI") + if !isCI { go func() { // Wait a bit for the server to come up to avoid a 404 as a first experience @@ -61,17 +56,19 @@ func (s *Server) Close() error { return s.server.Close() } -func (s *Server) Handler(serveFromFS bool) http.Handler { +func (s *Server) Handler() http.Handler { router := http.NewServeMux() router.HandleFunc("GET /api/traces", s.tracesHandler) router.HandleFunc("GET /api/traces/{id}", s.traceIDHandler) router.HandleFunc("GET /api/sampleData", s.sampleDataHandler) router.HandleFunc("GET /api/clearData", s.clearTracesHandler) - router.HandleFunc("GET /traces/{id}", indexHandler) + router.HandleFunc("GET /traces/{id}", s.indexHandler) - if serveFromFS { + if s.isDev { + // Serve front-end from the filesystem router.Handle("/", http.FileServer(http.Dir("./static/"))) } else { + // Serve front end from embedded static content staticContent, err := fs.Sub(assets, "static") if err != nil { log.Fatal(err) @@ -122,8 +119,8 @@ func (s *Server) traceIDHandler(writer http.ResponseWriter, request *http.Reques } } -func indexHandler(writer http.ResponseWriter, request *http.Request) { - if os.Getenv("SERVE_FROM_FS") == "true" { +func (s *Server) indexHandler(writer http.ResponseWriter, request *http.Request) { + if s.isDev { http.ServeFile(writer, request, "./desktopexporter/internal/server/static/index.html") } else { indexBytes, err := assets.ReadFile("static/index.html") diff --git a/desktopexporter/internal/server/server_test.go b/desktopexporter/internal/server/server_test.go index 636f72f..aefe6a3 100644 --- a/desktopexporter/internal/server/server_test.go +++ b/desktopexporter/internal/server/server_test.go @@ -16,8 +16,8 @@ import ( ) func setupEmpty() (*httptest.Server, func()) { - server := NewServer("localhost:8000", "") - testServer := httptest.NewServer(server.Handler(false)) + server := NewServer("localhost:8000", "", false) + testServer := httptest.NewServer(server.Handler()) return testServer, func() { testServer.Close() @@ -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", "", false) testSpanData := telemetry.SpanData{ TraceID: "1234567890", TraceState: "", @@ -56,7 +56,7 @@ func setupWithTrace(t *testing.T) (*httptest.Server, func(*testing.T)) { err := server.Store.AddSpans(context.Background(), []telemetry.SpanData{testSpanData}) assert.Nilf(t, err, "could not create test span: %v", err) - testServer := httptest.NewServer(server.Handler(false)) + testServer := httptest.NewServer(server.Handler()) return testServer, func(t *testing.T) { testServer.Close()