diff --git a/ocis-pkg/service/http/option.go b/ocis-pkg/service/http/option.go index 6f533c00dc..df2095f9ab 100644 --- a/ocis-pkg/service/http/option.go +++ b/ocis-pkg/service/http/option.go @@ -7,6 +7,7 @@ import ( "github.com/owncloud/ocis/v2/ocis-pkg/log" "github.com/owncloud/ocis/v2/ocis-pkg/shared" "github.com/urfave/cli/v2" + "go.opentelemetry.io/otel/trace" ) // Option defines a single option function. @@ -14,15 +15,16 @@ type Option func(o *Options) // Options defines the available options for this package. type Options struct { - Logger log.Logger - TLSConfig shared.HTTPServiceTLS - Namespace string - Name string - Version string - Address string - Handler http.Handler - Context context.Context - Flags []cli.Flag + Logger log.Logger + TLSConfig shared.HTTPServiceTLS + Namespace string + Name string + Version string + Address string + Handler http.Handler + Context context.Context + Flags []cli.Flag + TraceProvider trace.TracerProvider } // newOptions initializes the available default options. @@ -93,3 +95,10 @@ func TLSConfig(config shared.HTTPServiceTLS) Option { o.TLSConfig = config } } + +// TraceProvider provides a function to set the TraceProvider option. +func TraceProvider(tp trace.TracerProvider) Option { + return func(o *Options) { + o.TraceProvider = tp + } +} diff --git a/ocis-pkg/service/http/service.go b/ocis-pkg/service/http/service.go index cc7516ed17..8f4f669942 100644 --- a/ocis-pkg/service/http/service.go +++ b/ocis-pkg/service/http/service.go @@ -10,6 +10,7 @@ import ( "github.com/owncloud/ocis/v2/ocis-pkg/registry" mhttps "github.com/go-micro/plugins/v4/server/http" + mtracer "github.com/go-micro/plugins/v4/wrapper/trace/opentelemetry" ociscrypto "github.com/owncloud/ocis/v2/ocis-pkg/crypto" "go-micro.dev/v4" "go-micro.dev/v4/server" @@ -66,6 +67,15 @@ func NewService(opts ...Option) (Service, error) { micro.Registry(registry.GetRegistry()), micro.RegisterTTL(time.Second * 30), micro.RegisterInterval(time.Second * 10), + micro.WrapClient(mtracer.NewClientWrapper( + mtracer.WithTraceProvider(sopts.TraceProvider), + )), + micro.WrapHandler(mtracer.NewHandlerWrapper( + mtracer.WithTraceProvider(sopts.TraceProvider), + )), + micro.WrapSubscriber(mtracer.NewSubscriberWrapper( + mtracer.WithTraceProvider(sopts.TraceProvider), + )), } if sopts.TLSConfig.Enabled { wopts = append(wopts, micro.Metadata(map[string]string{"use_tls": "true"})) diff --git a/services/graph/mocks/ldapclient.go b/services/graph/mocks/ldapclient.go index a90b562032..291ca02f6f 100644 --- a/services/graph/mocks/ldapclient.go +++ b/services/graph/mocks/ldapclient.go @@ -45,8 +45,17 @@ func (_m *Client) Bind(username string, password string) error { } // Close provides a mock function with given fields: -func (_m *Client) Close() { - _m.Called() +func (_m *Client) Close() error { + ret := _m.Called() + + var r0 error + if rf, ok := ret.Get(0).(func() error); ok { + r0 = rf() + } else { + r0 = ret.Error(0) + } + + return r0 } // Compare provides a mock function with given fields: dn, attribute, value @@ -87,6 +96,32 @@ func (_m *Client) Del(_a0 *ldap.DelRequest) error { return r0 } +// DirSync provides a mock function with given fields: searchRequest, flags, maxAttrCount, cookie +func (_m *Client) DirSync(searchRequest *ldap.SearchRequest, flags int64, maxAttrCount int64, cookie []byte) (*ldap.SearchResult, error) { + ret := _m.Called(searchRequest, flags, maxAttrCount, cookie) + + var r0 *ldap.SearchResult + var r1 error + if rf, ok := ret.Get(0).(func(*ldap.SearchRequest, int64, int64, []byte) (*ldap.SearchResult, error)); ok { + return rf(searchRequest, flags, maxAttrCount, cookie) + } + if rf, ok := ret.Get(0).(func(*ldap.SearchRequest, int64, int64, []byte) *ldap.SearchResult); ok { + r0 = rf(searchRequest, flags, maxAttrCount, cookie) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*ldap.SearchResult) + } + } + + if rf, ok := ret.Get(1).(func(*ldap.SearchRequest, int64, int64, []byte) error); ok { + r1 = rf(searchRequest, flags, maxAttrCount, cookie) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + // ExternalBind provides a mock function with given fields: func (_m *Client) ExternalBind() error { ret := _m.Called() @@ -101,6 +136,20 @@ func (_m *Client) ExternalBind() error { return r0 } +// GetLastError provides a mock function with given fields: +func (_m *Client) GetLastError() error { + ret := _m.Called() + + var r0 error + if rf, ok := ret.Get(0).(func() error); ok { + r0 = rf() + } else { + r0 = ret.Error(0) + } + + return r0 +} + // IsClosing provides a mock function with given fields: func (_m *Client) IsClosing() bool { ret := _m.Called() diff --git a/services/idp/pkg/command/server.go b/services/idp/pkg/command/server.go index b8d299f3f0..b21f261685 100644 --- a/services/idp/pkg/command/server.go +++ b/services/idp/pkg/command/server.go @@ -16,6 +16,7 @@ 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/ocis-pkg/version" "github.com/owncloud/ocis/v2/services/idp/pkg/config" "github.com/owncloud/ocis/v2/services/idp/pkg/config/parser" @@ -23,7 +24,6 @@ import ( "github.com/owncloud/ocis/v2/services/idp/pkg/metrics" "github.com/owncloud/ocis/v2/services/idp/pkg/server/debug" "github.com/owncloud/ocis/v2/services/idp/pkg/server/http" - "github.com/owncloud/ocis/v2/services/idp/pkg/tracing" "github.com/urfave/cli/v2" ) @@ -50,7 +50,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 } @@ -75,8 +75,8 @@ func Server(cfg *config.Config) *cli.Command { http.Context(ctx), http.Config(cfg), http.Metrics(metrics), + http.TraceProvider(traceProvider), ) - if err != nil { logger.Info(). Err(err). @@ -104,7 +104,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 @@ -132,12 +131,12 @@ func ensureEncryptionSecretExists(path string) error { } dir := filepath.Dir(path) - err = os.MkdirAll(dir, 0700) + err = os.MkdirAll(dir, 0o700) if err != nil { return err } - f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0600) + f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0o600) if err != nil { return err } @@ -168,12 +167,12 @@ func ensureSigningPrivateKeyExists(paths []string) error { } dir := filepath.Dir(path) - err = os.MkdirAll(dir, 0700) + err = os.MkdirAll(dir, 0o700) if err != nil { return err } - f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0600) + f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0o600) if err != nil { return err } diff --git a/services/idp/pkg/config/tracing.go b/services/idp/pkg/config/tracing.go index d806380bca..8b1c8f70fb 100644 --- a/services/idp/pkg/config/tracing.go +++ b/services/idp/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;IDP_TRACING_ENABLED" desc:"Activates tracing."` @@ -7,3 +9,13 @@ type Tracing struct { Endpoint string `yaml:"endpoint" env:"OCIS_TRACING_ENDPOINT;IDP_TRACING_ENDPOINT" desc:"The endpoint of the tracing agent."` Collector string `yaml:"collector" env:"OCIS_TRACING_COLLECTOR;IDP_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/idp/pkg/middleware/static.go b/services/idp/pkg/middleware/static.go index 440219617c..1f8acb771f 100644 --- a/services/idp/pkg/middleware/static.go +++ b/services/idp/pkg/middleware/static.go @@ -4,14 +4,13 @@ import ( "net/http" "strings" - idpTracing "github.com/owncloud/ocis/v2/services/idp/pkg/tracing" "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/codes" "go.opentelemetry.io/otel/trace" ) // Static is a middleware that serves static assets. -func Static(root string, fs http.FileSystem) func(http.Handler) http.Handler { +func Static(root string, fs http.FileSystem, tp trace.TracerProvider) func(http.Handler) http.Handler { if !strings.HasSuffix(root, "/") { root = root + "/" } @@ -28,7 +27,7 @@ func Static(root string, fs http.FileSystem) func(http.Handler) http.Handler { spanOpts := []trace.SpanStartOption{ trace.WithSpanKind(trace.SpanKindServer), } - ctx, span := idpTracing.TraceProvider.Tracer("idp").Start(r.Context(), "serve static asset", spanOpts...) + ctx, span := tp.Tracer("idp").Start(r.Context(), "serve static asset", spanOpts...) defer span.End() r = r.WithContext(ctx) diff --git a/services/idp/pkg/server/http/option.go b/services/idp/pkg/server/http/option.go index 84074fb87e..2e0242e22b 100644 --- a/services/idp/pkg/server/http/option.go +++ b/services/idp/pkg/server/http/option.go @@ -7,6 +7,7 @@ import ( "github.com/owncloud/ocis/v2/services/idp/pkg/config" "github.com/owncloud/ocis/v2/services/idp/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 { - Namespace string - 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 + 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 trace provider option. +func TraceProvider(val trace.TracerProvider) Option { + return func(o *Options) { + o.TraceProvider = val + } +} diff --git a/services/idp/pkg/server/http/server.go b/services/idp/pkg/server/http/server.go index 68c424f4e5..74b248181a 100644 --- a/services/idp/pkg/server/http/server.go +++ b/services/idp/pkg/server/http/server.go @@ -44,6 +44,7 @@ func Server(opts ...Option) (http.Service, error) { Cert: options.Config.HTTP.TLSCert, Key: options.Config.HTTP.TLSKey, }), + http.TraceProvider(options.TraceProvider), ) if err != nil { options.Logger.Error(). @@ -69,6 +70,7 @@ func Server(opts ...Option) (http.Service, error) { options.Logger, ), ), + svc.TraceProvider(options.TraceProvider), ) { diff --git a/services/idp/pkg/service/v0/option.go b/services/idp/pkg/service/v0/option.go index e8ba550102..f336c73218 100644 --- a/services/idp/pkg/service/v0/option.go +++ b/services/idp/pkg/service/v0/option.go @@ -5,6 +5,7 @@ import ( "github.com/owncloud/ocis/v2/ocis-pkg/log" "github.com/owncloud/ocis/v2/services/idp/pkg/config" + "go.opentelemetry.io/otel/trace" ) // Option defines a single option function. @@ -12,9 +13,10 @@ type Option func(o *Options) // Options defines the available options for this package. type Options struct { - Logger log.Logger - Config *config.Config - Middleware []func(http.Handler) http.Handler + Logger log.Logger + Config *config.Config + Middleware []func(http.Handler) http.Handler + TraceProvider trace.TracerProvider } // newOptions initializes the available default options. @@ -48,3 +50,10 @@ func Middleware(val ...func(http.Handler) http.Handler) Option { o.Middleware = val } } + +// TraceProvider provides a function to set the trace provider option. +func TraceProvider(val trace.TracerProvider) Option { + return func(o *Options) { + o.TraceProvider = val + } +} diff --git a/services/idp/pkg/service/v0/service.go b/services/idp/pkg/service/v0/service.go index 871aa92f94..7aabdad7f8 100644 --- a/services/idp/pkg/service/v0/service.go +++ b/services/idp/pkg/service/v0/service.go @@ -20,10 +20,13 @@ import ( "github.com/libregraph/lico/server" "github.com/owncloud/ocis/v2/ocis-pkg/ldap" "github.com/owncloud/ocis/v2/ocis-pkg/log" + "github.com/owncloud/ocis/v2/ocis-pkg/tracing" "github.com/owncloud/ocis/v2/services/idp/pkg/assets" cs3BackendSupport "github.com/owncloud/ocis/v2/services/idp/pkg/backends/cs3/bootstrap" "github.com/owncloud/ocis/v2/services/idp/pkg/config" "github.com/owncloud/ocis/v2/services/idp/pkg/middleware" + "github.com/riandyrn/otelchi" + "go.opentelemetry.io/otel/trace" "gopkg.in/yaml.v2" "stash.kopano.io/kgol/rndm" ) @@ -81,7 +84,6 @@ func NewService(opts ...Option) Service { bs, err := bootstrap.Boot(ctx, &idpSettings, &licoconfig.Config{ Logger: log.LogrusWrap(logger), }) - if err != nil { logger.Fatal().Err(err).Msg("could not bootstrap idp") } @@ -94,6 +96,7 @@ func NewService(opts ...Option) Service { logger: options.Logger, config: options.Config, assets: assetVFS, + tp: options.TraceProvider, } svc.initMux(ctx, routes, handlers, options) @@ -106,10 +109,9 @@ type temporaryClientConfig struct { } func createTemporaryClientsConfig(filePath, ocisURL string, clients []config.Client) error { - folder := path.Dir(filePath) if _, err := os.Stat(folder); os.IsNotExist(err) { - if err := os.MkdirAll(folder, 0700); err != nil { + if err := os.MkdirAll(folder, 0o700); err != nil { return err } } @@ -141,18 +143,17 @@ func createTemporaryClientsConfig(filePath, ocisURL string, clients []config.Cli defer confOnDisk.Close() - err = os.WriteFile(filePath, conf, 0600) + err = os.WriteFile(filePath, conf, 0o600) if err != nil { return err } return nil - } // Init cs3 backend vars which are currently not accessible via idp api func initCS3EnvVars(cs3Addr, machineAuthAPIKey string) error { - var defaults = map[string]string{ + defaults := map[string]string{ "CS3_GATEWAY": cs3Addr, "CS3_MACHINE_AUTH_API_KEY": machineAuthAPIKey, } @@ -187,7 +188,7 @@ func initLicoInternalLDAPEnvVars(ldap *config.Ldap) error { filter = fmt.Sprintf("(&%s)", filter) } - var defaults = map[string]string{ + defaults := map[string]string{ "LDAP_URI": ldap.URI, "LDAP_BINDDN": ldap.BindDN, "LDAP_BINDPW": ldap.BindPassword, @@ -221,6 +222,7 @@ type IDP struct { config *config.Config mux *chi.Mux assets http.FileSystem + tp trace.TracerProvider } // initMux initializes the internal idp gorilla mux and mounts it in to a ocis chi-router @@ -244,8 +246,18 @@ func (idp *IDP) initMux(ctx context.Context, r []server.WithRoutes, h http.Handl assets.Logger(options.Logger), assets.Config(options.Config), ), + idp.tp, )) + idp.mux.Use( + otelchi.Middleware( + "idp", + otelchi.WithChiRoutes(idp.mux), + otelchi.WithTracerProvider(idp.tp), + otelchi.WithPropagators(tracing.GetPropagator()), + ), + ) + // handle / | index.html with a template that needs to have the BASE_PREFIX replaced idp.mux.Get("/signin/v1/identifier", idp.Index()) idp.mux.Get("/signin/v1/identifier/", idp.Index()) @@ -266,7 +278,6 @@ func (idp IDP) ServeHTTP(w http.ResponseWriter, r *http.Request) { // Index renders the static html with the func (idp IDP) Index() http.HandlerFunc { - f, err := idp.assets.Open("/identifier/index.html") if err != nil { idp.logger.Fatal().Err(err).Msg("Could not open index template") diff --git a/services/idp/pkg/service/v0/tracing.go b/services/idp/pkg/service/v0/tracing.go deleted file mode 100644 index 4a2c04f51f..0000000000 --- a/services/idp/pkg/service/v0/tracing.go +++ /dev/null @@ -1,23 +0,0 @@ -package svc - -import ( - "net/http" - - "github.com/owncloud/ocis/v2/ocis-pkg/middleware" -) - -// 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) { - middleware.TraceContext(t.next).ServeHTTP(w, r) -} diff --git a/services/idp/pkg/tracing/tracing.go b/services/idp/pkg/tracing/tracing.go deleted file mode 100644 index 339597b54a..0000000000 --- a/services/idp/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/idp/pkg/config" - "go.opentelemetry.io/otel/trace" -) - -var ( - // TraceProvider is the global trace provider for the idp 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 -} diff --git a/services/webfinger/mocks/ldapclient.go b/services/webfinger/mocks/ldapclient.go index a90b562032..291ca02f6f 100644 --- a/services/webfinger/mocks/ldapclient.go +++ b/services/webfinger/mocks/ldapclient.go @@ -45,8 +45,17 @@ func (_m *Client) Bind(username string, password string) error { } // Close provides a mock function with given fields: -func (_m *Client) Close() { - _m.Called() +func (_m *Client) Close() error { + ret := _m.Called() + + var r0 error + if rf, ok := ret.Get(0).(func() error); ok { + r0 = rf() + } else { + r0 = ret.Error(0) + } + + return r0 } // Compare provides a mock function with given fields: dn, attribute, value @@ -87,6 +96,32 @@ func (_m *Client) Del(_a0 *ldap.DelRequest) error { return r0 } +// DirSync provides a mock function with given fields: searchRequest, flags, maxAttrCount, cookie +func (_m *Client) DirSync(searchRequest *ldap.SearchRequest, flags int64, maxAttrCount int64, cookie []byte) (*ldap.SearchResult, error) { + ret := _m.Called(searchRequest, flags, maxAttrCount, cookie) + + var r0 *ldap.SearchResult + var r1 error + if rf, ok := ret.Get(0).(func(*ldap.SearchRequest, int64, int64, []byte) (*ldap.SearchResult, error)); ok { + return rf(searchRequest, flags, maxAttrCount, cookie) + } + if rf, ok := ret.Get(0).(func(*ldap.SearchRequest, int64, int64, []byte) *ldap.SearchResult); ok { + r0 = rf(searchRequest, flags, maxAttrCount, cookie) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*ldap.SearchResult) + } + } + + if rf, ok := ret.Get(1).(func(*ldap.SearchRequest, int64, int64, []byte) error); ok { + r1 = rf(searchRequest, flags, maxAttrCount, cookie) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + // ExternalBind provides a mock function with given fields: func (_m *Client) ExternalBind() error { ret := _m.Called() @@ -101,6 +136,20 @@ func (_m *Client) ExternalBind() error { return r0 } +// GetLastError provides a mock function with given fields: +func (_m *Client) GetLastError() error { + ret := _m.Called() + + var r0 error + if rf, ok := ret.Get(0).(func() error); ok { + r0 = rf() + } else { + r0 = ret.Error(0) + } + + return r0 +} + // IsClosing provides a mock function with given fields: func (_m *Client) IsClosing() bool { ret := _m.Called()