Skip to content

Commit

Permalink
Merge pull request #6956 from owncloud/ainmosni/tracing/web
Browse files Browse the repository at this point in the history
Convert web to service trace provider.
  • Loading branch information
Daniel Swärd authored Aug 7, 2023
2 parents d0cef04 + 9203478 commit 0c81ec5
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 76 deletions.
7 changes: 3 additions & 4 deletions services/web/pkg/command/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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
}
Expand Down Expand Up @@ -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).
Expand Down Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions services/web/pkg/config/tracing.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
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."`
Type string `yaml:"type" env:"OCIS_TRACING_TYPE;WEB_TRACING_TYPE" desc:"The type of tracing. Defaults to '', which is the same as 'jaeger'. Allowed tracing types are 'jaeger' and '' as of now."`
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,
}
}
21 changes: 15 additions & 6 deletions services/web/pkg/server/http/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,21 @@ 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.
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.
Expand Down Expand Up @@ -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
}
}
2 changes: 1 addition & 1 deletion services/web/pkg/server/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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().
Expand Down Expand Up @@ -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 {
Expand Down
9 changes: 9 additions & 0 deletions services/web/pkg/service/v0/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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
}
}
18 changes: 12 additions & 6 deletions services/web/pkg/service/v0/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
36 changes: 0 additions & 36 deletions services/web/pkg/service/v0/tracing.go

This file was deleted.

23 changes: 0 additions & 23 deletions services/web/pkg/tracing/tracing.go

This file was deleted.

0 comments on commit 0c81ec5

Please sign in to comment.