Add tracing to settings service.

This adds tracing to the settings service. It uses the otelchi package
and passes the tracing provider to the grpc and http servers.
This commit is contained in:
Daniël Franke
2023-06-27 12:31:19 +02:00
parent cd304b4df8
commit 96557a95ae
36 changed files with 2230 additions and 81 deletions
+7 -3
View File
@@ -7,6 +7,7 @@ import (
"github.com/oklog/run"
"github.com/owncloud/ocis/v2/ocis-pkg/config/configlog"
ogrpc "github.com/owncloud/ocis/v2/ocis-pkg/service/grpc"
"github.com/owncloud/ocis/v2/ocis-pkg/tracing"
"github.com/owncloud/ocis/v2/ocis-pkg/version"
"github.com/owncloud/ocis/v2/services/settings/pkg/config"
"github.com/owncloud/ocis/v2/services/settings/pkg/config/parser"
@@ -16,7 +17,6 @@ import (
"github.com/owncloud/ocis/v2/services/settings/pkg/server/grpc"
"github.com/owncloud/ocis/v2/services/settings/pkg/server/http"
svc "github.com/owncloud/ocis/v2/services/settings/pkg/service/v0"
"github.com/owncloud/ocis/v2/services/settings/pkg/tracing"
"github.com/urfave/cli/v2"
)
@@ -31,11 +31,13 @@ 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)
tracingProvider, err := tracing.GetServiceTraceProvider(cfg.Tracing, cfg.Service.Name)
if err != nil {
return err
}
err = ogrpc.Configure(ogrpc.GetClientOptions(cfg.GRPCClientTLS)...)
err = ogrpc.Configure(
append(ogrpc.GetClientOptions(cfg.GRPCClientTLS), ogrpc.WithTraceProvider(tracingProvider))...,
)
if err != nil {
return err
}
@@ -62,6 +64,7 @@ func Server(cfg *config.Config) *cli.Command {
http.Config(cfg),
http.Metrics(mtrcs),
http.ServiceHandler(handle),
http.TraceProvider(tracingProvider),
)
if err != nil {
logger.Error().
@@ -82,6 +85,7 @@ func Server(cfg *config.Config) *cli.Command {
grpc.Config(cfg),
grpc.Metrics(mtrcs),
grpc.ServiceHandler(handle),
grpc.TraceProvider(tracingProvider),
)
servers.Add(grpcServer.Run, func(_ error) {
logger.Info().Str("server", "grpc").Msg("Shutting down server")
+12
View File
@@ -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;SETTINGS_TRACING_ENABLED" desc:"Activates tracing."`
@@ -7,3 +9,13 @@ type Tracing struct {
Endpoint string `yaml:"endpoint" env:"OCIS_TRACING_ENDPOINT;SETTINGS_TRACING_ENDPOINT" desc:"The endpoint of the tracing agent."`
Collector string `yaml:"collector" env:"OCIS_TRACING_COLLECTOR;SETTINGS_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,
}
}
@@ -8,6 +8,7 @@ import (
"github.com/owncloud/ocis/v2/services/settings/pkg/metrics"
svc "github.com/owncloud/ocis/v2/services/settings/pkg/service/v0"
"github.com/urfave/cli/v2"
"go.opentelemetry.io/otel/trace"
)
// Option defines a single option function.
@@ -22,6 +23,7 @@ type Options struct {
Metrics *metrics.Metrics
ServiceHandler svc.Service
Flags []cli.Flag
TraceProvider trace.TracerProvider
}
// newOptions initializes the available default options.
@@ -83,3 +85,10 @@ func ServiceHandler(val svc.Service) Option {
o.ServiceHandler = val
}
}
// TraceProvider provides a function to set the TraceProvider option
func TraceProvider(val trace.TracerProvider) Option {
return func(o *Options) {
o.TraceProvider = val
}
}
@@ -28,6 +28,7 @@ func Server(opts ...Option) grpc.Service {
grpc.Namespace(options.Config.GRPC.Namespace),
grpc.Context(options.Context),
grpc.Flags(options.Flags...),
grpc.TraceProvider(options.TraceProvider),
)
if err != nil {
options.Logger.Fatal().Err(err).Msg("Error creating settings service")
@@ -8,6 +8,7 @@ import (
"github.com/owncloud/ocis/v2/services/settings/pkg/metrics"
svc "github.com/owncloud/ocis/v2/services/settings/pkg/service/v0"
"github.com/urfave/cli/v2"
"go.opentelemetry.io/otel/trace"
)
// Option defines a single option function.
@@ -22,6 +23,7 @@ type Options struct {
Metrics *metrics.Metrics
ServiceHandler svc.Service
Flags []cli.Flag
TraceProvider trace.TracerProvider
}
// newOptions initializes the available default options.
@@ -83,3 +85,10 @@ func ServiceHandler(val svc.Service) Option {
o.ServiceHandler = val
}
}
// TraceProvider provides a function to set the TraceProvider option
func TraceProvider(val trace.TracerProvider) Option {
return func(o *Options) {
o.TraceProvider = val
}
}
+11 -7
View File
@@ -10,9 +10,10 @@ import (
"github.com/owncloud/ocis/v2/ocis-pkg/cors"
"github.com/owncloud/ocis/v2/ocis-pkg/middleware"
ohttp "github.com/owncloud/ocis/v2/ocis-pkg/service/http"
"github.com/owncloud/ocis/v2/ocis-pkg/tracing"
"github.com/owncloud/ocis/v2/ocis-pkg/version"
settingssvc "github.com/owncloud/ocis/v2/protogen/gen/ocis/services/settings/v0"
svc "github.com/owncloud/ocis/v2/services/settings/pkg/service/v0"
"github.com/riandyrn/otelchi"
"go-micro.dev/v4"
)
@@ -39,12 +40,6 @@ func Server(opts ...Option) (ohttp.Service, error) {
handle := options.ServiceHandler
{
handle = svc.NewInstrument(handle, options.Metrics)
handle = svc.NewLogging(handle, options.Logger)
handle = svc.NewTracing(handle)
}
mux := chi.NewMux()
mux.Use(chimiddleware.RealIP)
@@ -72,6 +67,15 @@ func Server(opts ...Option) (ohttp.Service, error) {
options.Logger,
))
mux.Use(
otelchi.Middleware(
options.Name,
otelchi.WithChiRoutes(mux),
otelchi.WithTracerProvider(options.TraceProvider),
otelchi.WithPropagators(tracing.GetPropagator()),
),
)
mux.Route(options.Config.HTTP.Root, func(r chi.Router) {
settingssvc.RegisterBundleServiceWeb(r, handle)
settingssvc.RegisterValueServiceWeb(r, handle)
@@ -1,13 +0,0 @@
package svc
import (
"github.com/owncloud/ocis/v2/services/settings/pkg/metrics"
)
// NewInstrument returns a service that instruments metrics.
func NewInstrument(next Service, metrics *metrics.Metrics) Service {
return Service{
manager: next.manager,
config: next.config,
}
}
@@ -1,13 +0,0 @@
package svc
import (
"github.com/owncloud/ocis/v2/ocis-pkg/log"
)
// NewLogging returns a service that logs messages.
func NewLogging(next Service, logger log.Logger) Service {
return Service{
manager: next.manager,
config: next.config,
}
}
@@ -1,9 +0,0 @@
package svc
// NewTracing returns a service that instruments traces.
func NewTracing(next Service) Service {
return Service{
manager: next.manager,
config: next.config,
}
}
-23
View File
@@ -1,23 +0,0 @@
package tracing
import (
pkgtrace "github.com/owncloud/ocis/v2/ocis-pkg/tracing"
"github.com/owncloud/ocis/v2/services/settings/pkg/config"
"go.opentelemetry.io/otel/trace"
)
var (
// TraceProvider is the global trace provider for the settings 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
}