Skip to content

Commit

Permalink
single fsm metric instrumentation
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanj-square committed Jul 24, 2024
1 parent 4a34226 commit d42140f
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 15 deletions.
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
5 changes: 5 additions & 0 deletions backend/controller/observability/attributes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package observability

const (
fsmRefAttribute string = "ftl.fsm.ref"
)
38 changes: 38 additions & 0 deletions backend/controller/observability/fsm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package observability

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

const fsmMeterName = "ftl.fsm"

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

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

// TODO error logging and handling

Check warning on line 21 in backend/controller/observability/fsm.go

View workflow job for this annotation

GitHub Actions / Lint

exported: comment on exported function InitFSMMetrics should be of the form "InitFSMMetrics ..." (revive)
func InitFSMMetrics() {
fsmCounters.instancesActive, _ = fsmMeter.Int64UpDownCounter(

Check failure on line 23 in backend/controller/observability/fsm.go

View workflow job for this annotation

GitHub Actions / Lint

Error return value of `fsmMeter.Int64UpDownCounter` is not checked (errcheck)
fmt.Sprintf("%s.instances.active", fsmMeterName),
metric.WithDescription("counts the number of active FSM instances"))
}

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())))
}

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())))
}
5 changes: 5 additions & 0 deletions backend/controller/observability/observability.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package observability

func Init() {
InitFSMMetrics()
}
3 changes: 3 additions & 0 deletions internal/observability/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package observability
import (
"context"
"fmt"
"github.com/TBD54566975/ftl/backend/controller/observability"
"os"
"strings"

Expand Down Expand Up @@ -64,6 +65,8 @@ 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
17 changes: 2 additions & 15 deletions internal/observability/metrics/attributes.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
package metrics

import (
"github.com/TBD54566975/ftl/backend/schema"
"go.opentelemetry.io/otel/attribute"
const (
ModuleNameAttribute = "ftl.module.name"
)

// ModuleNameAttribute identifies the name of the module that the associated
// metric originates from.
func ModuleNameAttribute(name string) attribute.KeyValue {
return attribute.String("ftl.module.name", name)
}

// VerbRefAttribute identifies the verb that the associated metric originates
// from. The entire module qualified name is used: e.g. {module.verb}
func VerbRefAttribute(ref schema.Ref) attribute.KeyValue {
return attribute.String("ftl.verb.ref", ref.Name)
}

0 comments on commit d42140f

Please sign in to comment.