Convert webfinger to use service trace provider

This commit is contained in:
Daniel Swärd
2023-08-02 13:08:41 +02:00
parent 28ee7730f7
commit a5f242f683
4 changed files with 19 additions and 29 deletions

View File

@@ -6,6 +6,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/webfinger/pkg/config"
"github.com/owncloud/ocis/v2/services/webfinger/pkg/config/parser"
@@ -15,7 +16,6 @@ import (
"github.com/owncloud/ocis/v2/services/webfinger/pkg/server/debug"
"github.com/owncloud/ocis/v2/services/webfinger/pkg/server/http"
"github.com/owncloud/ocis/v2/services/webfinger/pkg/service/v0"
"github.com/owncloud/ocis/v2/services/webfinger/pkg/tracing"
"github.com/urfave/cli/v2"
)
@@ -30,7 +30,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
}
@@ -68,7 +68,7 @@ func Server(cfg *config.Config) *cli.Command {
}
svc = service.NewInstrument(svc, metrics)
svc = service.NewLogging(svc, logger) // this logs service specific data
svc = service.NewTracing(svc)
svc = service.NewTracing(svc, traceProvider)
server, err := http.Server(
http.Logger(logger),

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;WEBFINGER_TRACING_ENABLED" desc:"Activates tracing."`
@@ -7,3 +9,13 @@ type Tracing struct {
Endpoint string `yaml:"endpoint" env:"OCIS_TRACING_ENDPOINT;WEBFINGER_TRACING_ENDPOINT" desc:"The endpoint of the tracing agent."`
Collector string `yaml:"collector" env:"OCIS_TRACING_COLLECTOR;WEBFINGER_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,
}
}

View File

@@ -4,21 +4,22 @@ import (
"context"
"net/url"
webfingertracing "github.com/owncloud/ocis/v2/services/webfinger/pkg/tracing"
"github.com/owncloud/ocis/v2/services/webfinger/pkg/webfinger"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
)
// NewTracing returns a service that instruments traces.
func NewTracing(next Service) Service {
func NewTracing(next Service, tp trace.TracerProvider) Service {
return tracing{
next: next,
tp: tp,
}
}
type tracing struct {
next Service
tp trace.TracerProvider
}
// Webfinger implements the Service interface.
@@ -30,7 +31,7 @@ func (t tracing) Webfinger(ctx context.Context, queryTarget *url.URL, rels []str
attribute.KeyValue{Key: "rels", Value: attribute.StringSliceValue(rels)},
),
}
ctx, span := webfingertracing.TraceProvider.Tracer("webfinger").Start(ctx, "Webfinger", spanOpts...)
ctx, span := t.tp.Tracer("webfinger").Start(ctx, "Webfinger", spanOpts...)
defer span.End()
return t.next.Webfinger(ctx, queryTarget, rels)

View File

@@ -1,23 +0,0 @@
package tracing
import (
pkgtrace "github.com/owncloud/ocis/v2/ocis-pkg/tracing"
"github.com/owncloud/ocis/v2/services/webfinger/pkg/config"
"go.opentelemetry.io/otel/trace"
)
var (
// TraceProvider is the global trace provider for the proxy 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
}