Skip to content

Commit

Permalink
incorporating PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanj-square committed Jul 25, 2024
1 parent 53136c3 commit adea7a1
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 22 deletions.
5 changes: 5 additions & 0 deletions backend/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"encoding/binary"
"errors"
"fmt"
"github.com/TBD54566975/ftl/backend/controller/observability"
"hash"
"io"
"math/rand"
Expand Down Expand Up @@ -226,6 +227,10 @@ func New(ctx context.Context, pool *pgxpool.Pool, config Config, runnerScaling s
}
config.SetDefaults()

if err := observability.InitControllerObservability(); err != nil {
log.FromContext(ctx).Warnf("failed to initialize controller observability: %v", err)
}

// Override some defaults during development mode.
_, devel := runnerScaling.(*localscaling.LocalScaling)
if devel {
Expand Down
5 changes: 0 additions & 5 deletions backend/controller/observability/attributes.go

This file was deleted.

36 changes: 25 additions & 11 deletions backend/controller/observability/fsm.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,49 @@ import (
"context"
"fmt"
"github.com/TBD54566975/ftl/backend/schema"
"github.com/TBD54566975/ftl/internal/observability/metrics"
"github.com/TBD54566975/ftl/internal/observability"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
)

const fsmMeterName = "ftl.fsm"
const (
fsmMeterName = "ftl.fsm"
fsmRefAttribute = "ftl.fsm.ref"
)

var fsmMeter = otel.Meter("ftl.fsm")

var fsmCounters = struct {
instancesActive metric.Int64UpDownCounter
}{}

// TODO error logging and handling
func InitFSMMetrics() {
fsmCounters.instancesActive, _ = fsmMeter.Int64UpDownCounter(
func InitFSMMetrics() error {
var err error

fsmCounters.instancesActive, err = fsmMeter.Int64UpDownCounter(
fmt.Sprintf("%s.instances.active", fsmMeterName),
metric.WithDescription("counts the number of active FSM instances"))

if err != nil {
return fmt.Errorf("could not initialize fsm metrics: %w", err)
}

return nil
}

func FSMInstanceCreated(ctx context.Context, fsm schema.RefKey) {
fsmCounters.instancesActive.Add(ctx, 1, metric.WithAttributes(
attribute.String(metrics.ModuleNameAttribute, fsm.Module),
attribute.String(fsmRefAttribute, fsm.String())))
if fsmCounters.instancesActive != nil {
fsmCounters.instancesActive.Add(ctx, 1, metric.WithAttributes(
attribute.String(observability.ModuleNameAttribute, fsm.Module),
attribute.String(fsmRefAttribute, fsm.String())))
}
}

func FSMInstanceCompleted(ctx context.Context, fsm schema.RefKey) {
fsmCounters.instancesActive.Add(ctx, -1, metric.WithAttributes(
attribute.String(metrics.ModuleNameAttribute, fsm.Module),
attribute.String(fsmRefAttribute, fsm.String())))
if fsmCounters.instancesActive != nil {
fsmCounters.instancesActive.Add(ctx, -1, metric.WithAttributes(
attribute.String(observability.ModuleNameAttribute, fsm.Module),
attribute.String(fsmRefAttribute, fsm.String())))
}
}
10 changes: 8 additions & 2 deletions backend/controller/observability/observability.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package observability

func Init() {
InitFSMMetrics()
import "fmt"

func InitControllerObservability() error {
if err := InitFSMMetrics(); err != nil {
return fmt.Errorf("could not initialize controller metrics: %w", err)
}

return nil
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package metrics
package observability

const (
ModuleNameAttribute = "ftl.module.name"
Expand Down
3 changes: 0 additions & 3 deletions internal/observability/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package observability
import (
"context"
"fmt"
"github.com/TBD54566975/ftl/backend/controller/observability"
"os"
"strings"

Expand Down Expand Up @@ -65,8 +64,6 @@ func Init(ctx context.Context, serviceName, serviceVersion string, config Config
meterProvider := metric.NewMeterProvider(metric.WithReader(metric.NewPeriodicReader(otelMetricExporter)), metric.WithResource(res))
otel.SetMeterProvider(meterProvider)

observability.Init()

otelTraceExporter, err := otlptracegrpc.New(ctx)
if err != nil {
return fmt.Errorf("failed to create OTEL trace exporter: %w", err)
Expand Down

0 comments on commit adea7a1

Please sign in to comment.