Skip to content

Commit

Permalink
Merge pull request #6695 from owncloud/ainmosni/tracing/graph
Browse files Browse the repository at this point in the history
Move graph service to service tracer.
  • Loading branch information
micbar authored Jul 17, 2023
2 parents 9245f00 + 2d1e504 commit 2fb2a1c
Show file tree
Hide file tree
Showing 13 changed files with 56 additions and 974 deletions.
6 changes: 6 additions & 0 deletions changelog/unreleased/move-graph-to-service-tracerprovider.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Enhancement: Move graph to service tracerprovider

This moves the graph to initialise a service tracer provider at service initialisation time,
instead of using a package global tracer provider.

https://github.com/owncloud/ocis/pull/6695
12 changes: 7 additions & 5 deletions services/graph/pkg/command/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ 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/graph/pkg/config"
"github.com/owncloud/ocis/v2/services/graph/pkg/config/parser"
"github.com/owncloud/ocis/v2/services/graph/pkg/logging"
"github.com/owncloud/ocis/v2/services/graph/pkg/metrics"
"github.com/owncloud/ocis/v2/services/graph/pkg/server/debug"
"github.com/owncloud/ocis/v2/services/graph/pkg/server/http"
"github.com/owncloud/ocis/v2/services/graph/pkg/tracing"
"github.com/urfave/cli/v2"
)

Expand All @@ -29,11 +29,15 @@ 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
}
err = ogrpc.Configure(ogrpc.GetClientOptions(cfg.GRPCClientTLS)...)
err = ogrpc.Configure(
append(
ogrpc.GetClientOptions(cfg.GRPCClientTLS),
ogrpc.WithTraceProvider(traceProvider),
)...)
if err != nil {
return err
}
Expand All @@ -58,7 +62,6 @@ func Server(cfg *config.Config) *cli.Command {
http.Config(cfg),
http.Metrics(mtrcs),
)

if err != nil {
logger.Error().Err(err).Str("transport", "http").Msg("Failed to initialize server")
return err
Expand All @@ -82,7 +85,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/graph/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;GRAPH_TRACING_ENABLED" desc:"Activates tracing."`
Type string `yaml:"type" env:"OCIS_TRACING_TYPE;GRAPH_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;GRAPH_TRACING_ENDPOINT" desc:"The endpoint of the tracing agent."`
Collector string `yaml:"collector" env:"OCIS_TRACING_COLLECTOR;GRAPH_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/graph/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/graph/pkg/config"
"github.com/owncloud/ocis/v2/services/graph/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
}
}
10 changes: 2 additions & 8 deletions services/graph/pkg/server/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
settingssvc "github.com/owncloud/ocis/v2/protogen/gen/ocis/services/settings/v0"
graphMiddleware "github.com/owncloud/ocis/v2/services/graph/pkg/middleware"
svc "github.com/owncloud/ocis/v2/services/graph/pkg/service/v0"
"github.com/owncloud/ocis/v2/services/graph/pkg/tracing"
"github.com/pkg/errors"
"go-micro.dev/v4"
"go-micro.dev/v4/events"
Expand Down Expand Up @@ -116,7 +115,7 @@ func Server(opts ...Option) (http.Service, error) {
var requireAdminMiddleware func(stdhttp.Handler) stdhttp.Handler
var roleService svc.RoleService
var gatewaySelector pool.Selectable[gateway.GatewayAPIClient]
grpcClient, err := grpc.NewClient(append(grpc.GetClientOptions(options.Config.GRPCClientTLS), grpc.WithTraceProvider(tracing.TraceProvider))...)
grpcClient, err := grpc.NewClient(append(grpc.GetClientOptions(options.Config.GRPCClientTLS), grpc.WithTraceProvider(options.TraceProvider))...)
if err != nil {
return http.Service{}, err
}
Expand Down Expand Up @@ -164,18 +163,13 @@ func Server(opts ...Option) (http.Service, error) {
svc.WithSearchService(searchsvc.NewSearchProviderService("com.owncloud.api.search", grpcClient)),
svc.KeycloakClient(keyCloakClient),
svc.EventHistoryClient(hClient),
svc.TraceProvider(options.TraceProvider),
)

if err != nil {
return http.Service{}, errors.New("could not initialize graph service")
}

{
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 {
return http.Service{}, err
}
Expand Down
5 changes: 1 addition & 4 deletions services/graph/pkg/service/v0/drives.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
v0 "github.com/owncloud/ocis/v2/protogen/gen/ocis/messages/settings/v0"
settingssvc "github.com/owncloud/ocis/v2/protogen/gen/ocis/services/settings/v0"
"github.com/owncloud/ocis/v2/services/graph/pkg/service/v0/errorcode"
gtracing "github.com/owncloud/ocis/v2/services/graph/pkg/tracing"
settingsServiceExt "github.com/owncloud/ocis/v2/services/settings/pkg/store/defaults"
"github.com/pkg/errors"
merrors "go-micro.dev/v4/errors"
Expand Down Expand Up @@ -161,7 +160,6 @@ func (g Graph) GetSingleDrive(w http.ResponseWriter, r *http.Request) {
logger := g.logger.SubloggerWithRequestID(r.Context())
logger.Info().Interface("query", r.URL.Query()).Msg("calling get drive")
driveID, err := url.PathUnescape(chi.URLParam(r, "driveID"))

if err != nil {
logger.Debug().Err(err).Str("driveID", chi.URLParam(r, "driveID")).Msg("could not get drive: unescaping drive id failed")
errorcode.InvalidRequest.Render(w, r, http.StatusBadRequest, "unescaping drive id failed")
Expand Down Expand Up @@ -602,7 +600,7 @@ func (g Graph) ListStorageSpacesWithFilters(ctx context.Context, filters []*stor
return nil, err
}

grpcClient, err := grpc.NewClient(append(grpc.GetClientOptions(g.config.GRPCClientTLS), grpc.WithTraceProvider(gtracing.TraceProvider))...)
grpcClient, err := grpc.NewClient(append(grpc.GetClientOptions(g.config.GRPCClientTLS), grpc.WithTraceProvider(g.traceProvider))...)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1099,7 +1097,6 @@ func (g Graph) DeleteDrive(w http.ResponseWriter, r *http.Request) {
errorcode.GeneralException.Render(w, r, http.StatusInternalServerError, "grpc error")
return
}

}

func sortSpaces(req *godata.GoDataRequest, spaces []*libregraph.Drive) ([]*libregraph.Drive, error) {
Expand Down
2 changes: 2 additions & 0 deletions services/graph/pkg/service/v0/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/owncloud/ocis/v2/services/graph/pkg/identity"
"go-micro.dev/v4/client"
mevents "go-micro.dev/v4/events"
"go.opentelemetry.io/otel/trace"
"google.golang.org/protobuf/types/known/emptypb"
)

Expand Down Expand Up @@ -73,6 +74,7 @@ type Graph struct {
searchService searchsvc.SearchProviderService
keycloakClient keycloak.Client
historyClient ehsvc.EventHistoryService
traceProvider trace.TracerProvider
}

// ServeHTTP implements the Service interface.
Expand Down
Loading

0 comments on commit 2fb2a1c

Please sign in to comment.