generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add topic and subscription to echo for testing make mergeable desc minor refactor final
- Loading branch information
Showing
7 changed files
with
175 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,31 @@ | ||
package observability | ||
|
||
import "fmt" | ||
import ( | ||
"errors" | ||
"fmt" | ||
) | ||
|
||
var ( | ||
PubSub *PubSubMetrics | ||
) | ||
|
||
// To be migrated to init() in https://github.com/TBD54566975/ftl/pull/2177/files | ||
func InitControllerObservability() error { | ||
if err := InitFSMMetrics(); err != nil { | ||
return fmt.Errorf("could not initialize controller metrics: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func init() { | ||
var errs error | ||
var err error | ||
|
||
PubSub, err = initPubSubMetrics() | ||
errs = errors.Join(errs, err) | ||
|
||
if errs != nil { | ||
panic(fmt.Errorf("could not initialize controller metrics: %w\n", errs)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package observability | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"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" | ||
"go.opentelemetry.io/otel/metric/noop" | ||
) | ||
|
||
const ( | ||
pubsubMeterName = "ftl.pubsub" | ||
pubsubTopicNameAttribute = "ftl.pubsub.topic.name" | ||
pubsubSubscriptionRefAttribute = "ftl.pubsub.subscription.ref" | ||
pubsubSubscriberRefAttribute = "ftl.pubsub.subscriber.sink.ref" | ||
) | ||
|
||
type PubSubMetrics struct { | ||
meter metric.Meter | ||
published metric.Int64Counter | ||
subscriberCalled metric.Int64Counter | ||
} | ||
|
||
func initPubSubMetrics() (*PubSubMetrics, error) { | ||
result := &PubSubMetrics{} | ||
var errs error | ||
var err error | ||
|
||
result.meter = otel.Meter(pubsubMeterName) | ||
|
||
counterName := fmt.Sprintf("%s.published", pubsubMeterName) | ||
if result.published, err = result.meter.Int64Counter( | ||
counterName, | ||
metric.WithDescription("the number of times that an event is published to a topic")); err != nil { | ||
result.published, errs = handleInitCounterError(errs, err, counterName) | ||
} | ||
|
||
counterName = fmt.Sprintf("%s.subscriber.called", pubsubMeterName) | ||
if result.subscriberCalled, err = result.meter.Int64Counter( | ||
counterName, | ||
metric.WithDescription("the number of times that a pubsub event has been enqueued to asynchronously send to a subscriber")); err != nil { | ||
result.subscriberCalled, errs = handleInitCounterError(errs, err, counterName) | ||
} | ||
|
||
return result, nil | ||
} | ||
|
||
func handleInitCounterError(errs error, err error, counterName string) (metric.Int64Counter, error) { | ||
return noop.Int64Counter{}, errors.Join(errs, fmt.Errorf("%q counter init failed; falling back to noop: %w", counterName, err)) | ||
} | ||
|
||
func (m *PubSubMetrics) Published(ctx context.Context, module, topic string) { | ||
m.published.Add(ctx, 1, metric.WithAttributes( | ||
attribute.String(observability.ModuleNameAttribute, module), | ||
attribute.String(pubsubTopicNameAttribute, topic), | ||
)) | ||
} | ||
|
||
func (m *PubSubMetrics) SubscriberCalled(ctx context.Context, topic string, subscription, sink schema.RefKey) { | ||
m.subscriberCalled.Add(ctx, 1, metric.WithAttributes( | ||
attribute.String(observability.ModuleNameAttribute, sink.Module), | ||
attribute.String(pubsubTopicNameAttribute, topic), | ||
attribute.String(pubsubSubscriptionRefAttribute, subscription.String()), | ||
attribute.String(pubsubSubscriberRefAttribute, sink.String()), | ||
)) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters