Skip to content

Commit

Permalink
Solve a few issues picked up by linter and simplify the logic in main…
Browse files Browse the repository at this point in the history
… (flush traces)
  • Loading branch information
Ricardo Osorio committed Oct 18, 2023
1 parent bc11bb6 commit 7d3c29e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 33 deletions.
49 changes: 24 additions & 25 deletions cmd/provider/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,17 @@ func main() {
leaderElection = app.Flag("leader-election", "Use leader election for the controller manager.").Short('l').Default("false").OverrideDefaultFromEnvar("LEADER_ELECTION").Bool()
leaderRenew = app.Flag("leader-renew", "Set leader election renewal.").Short('r').Default("10s").OverrideDefaultFromEnvar("LEADER_ELECTION_RENEW").Duration()

syncInterval = app.Flag("sync", "How often all resources will be double-checked for drift from the desired state.").Short('s').Default("1h").Duration()
pollInterval = app.Flag("poll", "How often individual resources will be checked for drift from the desired state").Short('p').Default("30m").Duration()
bucketExistsCache = app.Flag("bucket-exists-cache", "How long the provider caches bucket exists result").Short('c').Default("5s").Duration()
reconcileConcurrency = app.Flag("reconcile-concurrency", "Set number of reconciliation loops.").Default("100").Int()
maxReconcileRate = app.Flag("max-reconcile-rate", "The global maximum rate per second at which resources may checked for drift from the desired state.").Default("1000").Int()
reconcileTimeout = app.Flag("reconcile-timeout", "Object reconciliation timeout").Short('t').Default("3s").Duration()
creationGracePeriod = app.Flag("creation-grace-period", "Duration to wait for the external API to report that a newly created external resource exists.").Default("10s").Duration()
metricsExportTimeout = app.Flag("otel-metrics-export-timeout", "Timeout when exporting metrics").Default("2s").Duration()
metricsExportInterval = app.Flag("otel-metrics-export-interval", "Interval at which metrics are exported").Default("5s").Duration()
metricsExportAddress = app.Flag("otel-metrics-export-address", "Address of otel collector").Default("opentelemetry-collector.opentelemetry:4317").String()
syncInterval = app.Flag("sync", "How often all resources will be double-checked for drift from the desired state.").Short('s').Default("1h").Duration()
pollInterval = app.Flag("poll", "How often individual resources will be checked for drift from the desired state").Short('p').Default("30m").Duration()
bucketExistsCache = app.Flag("bucket-exists-cache", "How long the provider caches bucket exists result").Short('c').Default("5s").Duration()
reconcileConcurrency = app.Flag("reconcile-concurrency", "Set number of reconciliation loops.").Default("100").Int()
maxReconcileRate = app.Flag("max-reconcile-rate", "The global maximum rate per second at which resources may checked for drift from the desired state.").Default("1000").Int()
reconcileTimeout = app.Flag("reconcile-timeout", "Object reconciliation timeout").Short('t').Default("3s").Duration()
creationGracePeriod = app.Flag("creation-grace-period", "Duration to wait for the external API to report that a newly created external resource exists.").Default("10s").Duration()
tracesEnabled = app.Flag("otel-enable-tracing", "").Default("false").Bool()
tracesExportTimeout = app.Flag("otel-traces-export-timeout", "Timeout when exporting metrics").Default("2s").Duration()
tracesExportInterval = app.Flag("otel-traces-export-interval", "Interval at which traces are exported").Default("5s").Duration()
tracesExportAddress = app.Flag("otel-traces-export-address", "Address of otel collector").Default("opentelemetry-collector.opentelemetry:4317").String()

kubeClientRate = app.Flag("kube-client-rate", "The global maximum rate per second at how many requests the client can do.").Default("1000").Int()

Expand Down Expand Up @@ -151,27 +152,25 @@ func main() {

log := logging.NewLogrLogger(zl.WithName("provider-ceph"))

// Init otel tracer provider
tp, err := traces.InitTracerProvider(*metricsExportAddress, *metricsExportTimeout, *metricsExportInterval)
if err != nil {
log.Debug("error starting tracer provider", "error", err.Error())
}
// Init otel tracer provider if the user sets the flag
if *tracesEnabled {
tp, err := traces.InitTracerProvider(*tracesExportAddress, *tracesExportTimeout, *tracesExportInterval)
kingpin.FatalIfError(err, "Cannot start tracer provider")

// overwrite the default terminate function called on FatalIfError()
app.Terminate(func(i int) {
// default behavior
defer os.Exit(i)

// overwrite the default terminate function called on FatalIfError()
app.Terminate(func(i int) {
// flush traces
if tp != nil {
ctx, cancel := context.WithTimeout(context.Background(), *metricsExportTimeout)
// flush traces
ctx, cancel := context.WithTimeout(context.Background(), *tracesExportTimeout)
defer cancel()

if err := tp.Shutdown(ctx); err != nil {
log.Debug("failed to shutdown tracer provider and flush in-memory records", "error", err.Error())
}
}

// default behavior
os.Exit(i)
})
})
}

cfg, err := ctrl.GetConfig()
kingpin.FatalIfError(err, "Cannot get API server rest config")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ func (r *HealthCheckReconciler) doHealthCheck(ctx context.Context, providerConfi
if s3BackendClient == nil {
err := errors.New(errBackendNotStored)
traces.SetAndRecordError(span, err)

return err
}

Expand Down
7 changes: 5 additions & 2 deletions internal/otel/otel.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ import (
semconv "go.opentelemetry.io/otel/semconv/v1.21.0"
)

const ServiceName = "provider-ceph"
const (
ServiceName = "provider-ceph"
TimeoutGatherHostResources = time.Millisecond * 500
)

// RuntimeResources creates an otel sdk resource struct describing the service
// and runtime (host, process, runtime, etc). When used together with a
// TracerProvider this data will be included in all traces created from it.
func RuntimeResources() (*otelresource.Resource, error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*500)
ctx, cancel := context.WithTimeout(context.Background(), TimeoutGatherHostResources)
defer cancel()
runtimeResources, err := otelresource.New(
ctx,
Expand Down
6 changes: 0 additions & 6 deletions internal/otel/traces/traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,6 @@ import (
// Failing in doing so returns an error since service actively export their traces and
// require the Collector to be up.
func InitTracerProvider(otelCollectorAddress string, dialTimeout, exportInterval time.Duration) (*otelsdktrace.TracerProvider, error) {
if otelCollectorAddress == "" {
// feature disabled
// calls to the global tracer provider will return a no-op implementation
return nil, nil
}

runtimeResources, err := otel.RuntimeResources()
if err != nil {
return nil, fmt.Errorf("failed to gather runtime resources for traces provider: %w", err)
Expand Down

0 comments on commit 7d3c29e

Please sign in to comment.