Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: metric integration for fsm #2155

Merged
merged 2 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
Comment on lines +230 to +232
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works for now, but we might want to consider combining this and the observability.Init() that's currently in ftl-controller/main ftl-runner/main and cmd_serve files now. We can iterate on that in the future.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will combine in a fast follow change that completes the fsm metric instrumentation


// Override some defaults during development mode.
_, devel := runnerScaling.(*localscaling.LocalScaling)
if devel {
Expand Down
4 changes: 4 additions & 0 deletions backend/controller/dal/fsm.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/TBD54566975/ftl/backend/controller/observability"
"time"

"github.com/alecthomas/types/optional"
Expand Down Expand Up @@ -57,6 +58,7 @@ func (d *DAL) StartFSMTransition(ctx context.Context, fsm schema.RefKey, executi
}
return fmt.Errorf("failed to start FSM transition: %w", err)
}
observability.FSMInstanceCreated(ctx, fsm)
return nil
}

Expand All @@ -67,11 +69,13 @@ func (d *DAL) FinishFSMTransition(ctx context.Context, fsm schema.RefKey, instan

func (d *DAL) FailFSMInstance(ctx context.Context, fsm schema.RefKey, instanceKey string) error {
_, err := d.db.FailFSMInstance(ctx, fsm, instanceKey)
observability.FSMInstanceCompleted(ctx, fsm)
return dalerrs.TranslatePGError(err)
}

func (d *DAL) SucceedFSMInstance(ctx context.Context, fsm schema.RefKey, instanceKey string) error {
_, err := d.db.SucceedFSMInstance(ctx, fsm, instanceKey)
observability.FSMInstanceCompleted(ctx, fsm)
return dalerrs.TranslatePGError(err)
}

Expand Down
52 changes: 52 additions & 0 deletions backend/controller/observability/fsm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package observability

import (
"context"
"fmt"
"github.com/TBD54566975/ftl/backend/schema"
"github.com/TBD54566975/ftl/internal/observability"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
)

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

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

var fsmCounters = struct {
instancesActive metric.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) {
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) {
if fsmCounters.instancesActive != nil {
fsmCounters.instancesActive.Add(ctx, -1, metric.WithAttributes(
attribute.String(observability.ModuleNameAttribute, fsm.Module),
attribute.String(fsmRefAttribute, fsm.String())))
}
}
11 changes: 11 additions & 0 deletions backend/controller/observability/observability.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package observability

import "fmt"

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

return nil
}
5 changes: 5 additions & 0 deletions internal/observability/attributes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package observability

const (
ModuleNameAttribute = "ftl.module.name"
)
18 changes: 0 additions & 18 deletions internal/observability/metrics/attributes.go

This file was deleted.

Loading