diff --git a/services/web/pkg/command/server.go b/services/web/pkg/command/server.go index 9d7d40e31..2ec46c31c 100644 --- a/services/web/pkg/command/server.go +++ b/services/web/pkg/command/server.go @@ -8,13 +8,13 @@ import ( "github.com/oklog/run" "github.com/owncloud/ocis/v2/ocis-pkg/config/configlog" + "github.com/owncloud/ocis/v2/ocis-pkg/tracing" "github.com/owncloud/ocis/v2/services/web/pkg/config" "github.com/owncloud/ocis/v2/services/web/pkg/config/parser" "github.com/owncloud/ocis/v2/services/web/pkg/logging" "github.com/owncloud/ocis/v2/services/web/pkg/metrics" "github.com/owncloud/ocis/v2/services/web/pkg/server/debug" "github.com/owncloud/ocis/v2/services/web/pkg/server/http" - "github.com/owncloud/ocis/v2/services/web/pkg/tracing" "github.com/urfave/cli/v2" ) @@ -29,7 +29,7 @@ func Server(cfg *config.Config) *cli.Command { }, Action: func(c *cli.Context) error { logger := logging.Configure(cfg.Service.Name, cfg.Log) - err := tracing.Configure(cfg) + traceProvider, err := tracing.GetServiceTraceProvider(cfg.Tracing, cfg.Service.Name) if err != nil { return err } @@ -67,8 +67,8 @@ func Server(cfg *config.Config) *cli.Command { http.Namespace(cfg.HTTP.Namespace), http.Config(cfg), http.Metrics(metrics), + http.TraceProvider(traceProvider), ) - if err != nil { logger.Info(). Err(err). @@ -102,7 +102,6 @@ func Server(cfg *config.Config) *cli.Command { debug.Context(ctx), debug.Config(cfg), ) - if err != nil { logger.Info().Err(err).Str("transport", "debug").Msg("Failed to initialize server") return err diff --git a/services/web/pkg/config/tracing.go b/services/web/pkg/config/tracing.go index 467193947..5b8b6879a 100644 --- a/services/web/pkg/config/tracing.go +++ b/services/web/pkg/config/tracing.go @@ -1,5 +1,7 @@ package config +import "github.com/owncloud/ocis/v2/ocis-pkg/tracing" + // Tracing defines the available tracing configuration. type Tracing struct { Enabled bool `yaml:"enabled" env:"OCIS_TRACING_ENABLED;WEB_TRACING_ENABLED" desc:"Activates tracing."` @@ -7,3 +9,13 @@ type Tracing struct { Endpoint string `yaml:"endpoint" env:"OCIS_TRACING_ENDPOINT;WEB_TRACING_ENDPOINT" desc:"The endpoint of the tracing agent."` Collector string `yaml:"collector" env:"OCIS_TRACING_COLLECTOR;WEB_TRACING_COLLECTOR" desc:"The HTTP endpoint for sending spans directly to a collector, i.e. http://jaeger-collector:14268/api/traces. Only used if the tracing endpoint is unset."` } + +// Convert Tracing to the tracing package's Config struct. +func (t Tracing) Convert() tracing.Config { + return tracing.Config{ + Enabled: t.Enabled, + Type: t.Type, + Endpoint: t.Endpoint, + Collector: t.Collector, + } +} diff --git a/services/web/pkg/server/http/option.go b/services/web/pkg/server/http/option.go index 5ae833c36..8eec2f047 100644 --- a/services/web/pkg/server/http/option.go +++ b/services/web/pkg/server/http/option.go @@ -7,6 +7,7 @@ import ( "github.com/owncloud/ocis/v2/services/web/pkg/config" "github.com/owncloud/ocis/v2/services/web/pkg/metrics" "github.com/urfave/cli/v2" + "go.opentelemetry.io/otel/trace" ) // Option defines a single option function. @@ -14,12 +15,13 @@ type Option func(o *Options) // Options defines the available options for this package. type Options struct { - Logger log.Logger - Context context.Context - Config *config.Config - Metrics *metrics.Metrics - Flags []cli.Flag - Namespace string + Logger log.Logger + Context context.Context + Config *config.Config + Metrics *metrics.Metrics + Flags []cli.Flag + Namespace string + TraceProvider trace.TracerProvider } // newOptions initializes the available default options. @@ -74,3 +76,10 @@ func Namespace(val string) Option { o.Namespace = val } } + +// TraceProvider provides a function to set the TraceProvider option. +func TraceProvider(val trace.TracerProvider) Option { + return func(o *Options) { + o.TraceProvider = val + } +} diff --git a/services/web/pkg/server/http/server.go b/services/web/pkg/server/http/server.go index 8312f54b2..1815e5461 100644 --- a/services/web/pkg/server/http/server.go +++ b/services/web/pkg/server/http/server.go @@ -28,6 +28,7 @@ func Server(opts ...Option) (http.Service, error) { http.Address(options.Config.HTTP.Addr), http.Context(options.Context), http.Flags(options.Flags...), + http.TraceProvider(options.TraceProvider), ) if err != nil { options.Logger.Error(). @@ -71,7 +72,6 @@ func Server(opts ...Option) (http.Service, error) { { handle = svc.NewInstrument(handle, options.Metrics) handle = svc.NewLogging(handle, options.Logger) - handle = svc.NewTracing(handle) } if err := micro.RegisterHandler(service.Server(), handle); err != nil { diff --git a/services/web/pkg/service/v0/option.go b/services/web/pkg/service/v0/option.go index 8f6f261a4..96b626c5d 100644 --- a/services/web/pkg/service/v0/option.go +++ b/services/web/pkg/service/v0/option.go @@ -7,6 +7,7 @@ import ( "github.com/cs3org/reva/v2/pkg/rgrpc/todo/pool" "github.com/owncloud/ocis/v2/ocis-pkg/log" "github.com/owncloud/ocis/v2/services/web/pkg/config" + "go.opentelemetry.io/otel/trace" ) // Option defines a single option function. @@ -18,6 +19,7 @@ type Options struct { Config *config.Config Middleware []func(http.Handler) http.Handler GatewaySelector pool.Selectable[gateway.GatewayAPIClient] + TraceProvider trace.TracerProvider } // newOptions initializes the available default options. @@ -58,3 +60,10 @@ func GatewaySelector(gatewaySelector pool.Selectable[gateway.GatewayAPIClient]) o.GatewaySelector = gatewaySelector } } + +// TraceProvider provides a function to set the traceProvider option. +func TraceProvider(val trace.TracerProvider) Option { + return func(o *Options) { + o.TraceProvider = val + } +} diff --git a/services/web/pkg/service/v0/service.go b/services/web/pkg/service/v0/service.go index 3919ca4c6..003fe3c42 100644 --- a/services/web/pkg/service/v0/service.go +++ b/services/web/pkg/service/v0/service.go @@ -17,15 +17,15 @@ import ( "github.com/owncloud/ocis/v2/ocis-pkg/assetsfs" "github.com/owncloud/ocis/v2/ocis-pkg/log" "github.com/owncloud/ocis/v2/ocis-pkg/middleware" + "github.com/owncloud/ocis/v2/ocis-pkg/tracing" "github.com/owncloud/ocis/v2/services/web" "github.com/owncloud/ocis/v2/services/web/pkg/assets" "github.com/owncloud/ocis/v2/services/web/pkg/config" + "github.com/riandyrn/otelchi" ) -var ( - // ErrConfigInvalid is returned when the config parse is invalid. - ErrConfigInvalid = `Invalid or missing config` -) +// ErrConfigInvalid is returned when the config parse is invalid. +var ErrConfigInvalid = `Invalid or missing config` // Service defines the service handlers. type Service interface { @@ -42,6 +42,14 @@ func NewService(opts ...Option) Service { m := chi.NewMux() m.Use(options.Middleware...) + m.Use( + otelchi.Middleware( + "web", + otelchi.WithChiRoutes(m), + otelchi.WithTracerProvider(options.TraceProvider), + otelchi.WithPropagators(tracing.GetPropagator()), + ), + ) svc := Web{ logger: options.Logger, config: options.Config, @@ -86,7 +94,6 @@ func (p Web) ServeHTTP(w http.ResponseWriter, r *http.Request) { } func (p Web) getPayload() (payload []byte, err error) { - if p.config.Web.Path == "" { // render dynamically using config @@ -127,7 +134,6 @@ func (p Web) getPayload() (payload []byte, err error) { // Config implements the Service interface. func (p Web) Config(w http.ResponseWriter, _ *http.Request) { - payload, err := p.getPayload() if err != nil { http.Error(w, ErrConfigInvalid, http.StatusUnprocessableEntity) diff --git a/services/web/pkg/service/v0/tracing.go b/services/web/pkg/service/v0/tracing.go deleted file mode 100644 index 413257e9c..000000000 --- a/services/web/pkg/service/v0/tracing.go +++ /dev/null @@ -1,36 +0,0 @@ -package svc - -import ( - "net/http" -) - -// NewTracing returns a service that instruments traces. -func NewTracing(next Service) Service { - return tracing{ - next: next, - } -} - -type tracing struct { - next Service -} - -// ServeHTTP implements the Service interface. -func (t tracing) ServeHTTP(w http.ResponseWriter, r *http.Request) { - t.next.ServeHTTP(w, r) -} - -// Config implements the Service interface. -func (t tracing) Config(w http.ResponseWriter, r *http.Request) { - t.next.Config(w, r) -} - -// UploadLogo implements the Service interface. -func (t tracing) UploadLogo(w http.ResponseWriter, r *http.Request) { - t.next.UploadLogo(w, r) -} - -// ResetLogo implements the Service interface. -func (t tracing) ResetLogo(w http.ResponseWriter, r *http.Request) { - t.next.ResetLogo(w, r) -} diff --git a/services/web/pkg/tracing/tracing.go b/services/web/pkg/tracing/tracing.go deleted file mode 100644 index 378e1e0c3..000000000 --- a/services/web/pkg/tracing/tracing.go +++ /dev/null @@ -1,23 +0,0 @@ -package tracing - -import ( - pkgtrace "github.com/owncloud/ocis/v2/ocis-pkg/tracing" - "github.com/owncloud/ocis/v2/services/web/pkg/config" - "go.opentelemetry.io/otel/trace" -) - -var ( - // TraceProvider is the global trace provider for the web service. - TraceProvider = trace.NewNoopTracerProvider() -) - -func Configure(cfg *config.Config) error { - var err error - if cfg.Tracing.Enabled { - if TraceProvider, err = pkgtrace.GetTraceProvider(cfg.Tracing.Endpoint, cfg.Tracing.Collector, cfg.Service.Name, cfg.Tracing.Type); err != nil { - return err - } - } - - return nil -}