generated from xmidt-org/.go-template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: update based on pr feedback (move fx code into fx.go & test up…
…date) - move fx code into fx.go - update test defaults for baseClient_test.go
- Loading branch information
Showing
6 changed files
with
178 additions
and
159 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
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,87 @@ | ||
// SPDX-FileCopyrightText: 2025 Comcast Cable Communications Management, LLC | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package chrysom | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
"go.uber.org/fx" | ||
"go.uber.org/zap" | ||
) | ||
|
||
// GetLogger returns a logger from the given context. | ||
type GetLogger func(context.Context) *zap.Logger | ||
|
||
// SetLogger embeds the `Listener.logger` in outgoing request contexts for `Listener.Update` calls. | ||
type SetLogger func(context.Context, *zap.Logger) context.Context | ||
|
||
type BasicClientIn struct { | ||
fx.In | ||
|
||
// Ancla Client config. | ||
Config BasicClientConfig | ||
// GetLogger returns a logger from the given context. | ||
GetLogger GetLogger | ||
} | ||
|
||
// ProvideBasicClient provides a new BasicClient. | ||
func ProvideBasicClient(in BasicClientIn) (*BasicClient, error) { | ||
client, err := NewBasicClient(in.Config, in.GetLogger) | ||
if err != nil { | ||
return nil, errors.Join(errFailedConfig, err) | ||
} | ||
|
||
return client, nil | ||
} | ||
|
||
// ListenerConfig contains config data for polling the Argus client. | ||
type ListenerClientIn struct { | ||
fx.In | ||
|
||
// Listener fetches a copy of all items within a bucket on | ||
// an interval based on `BasicClientConfig.PullInterval`. | ||
// (Optional). If not provided, listening won't be enabled for this client. | ||
Listener Listener | ||
// Config configures the ancla client and its listeners. | ||
Config BasicClientConfig | ||
// PollsTotalCounter measures the number of polls (and their success/failure outcomes) to fetch new items. | ||
PollsTotalCounter *prometheus.CounterVec `name:"chrysom_polls_total"` | ||
// Reader is the DB interface used to fetch new items using `GeItems`. | ||
Reader Reader | ||
// GetLogger returns a logger from the given context. | ||
GetLogger GetLogger | ||
// SetLogger embeds the `Listener.logger` in outgoing request contexts for `Listener.Update` calls. | ||
SetLogger SetLogger | ||
} | ||
|
||
// ProvideListenerClient provides a new ListenerClient. | ||
func ProvideListenerClient(in ListenerClientIn) (*ListenerClient, error) { | ||
client, err := NewListenerClient(in.Listener, in.GetLogger, in.SetLogger, in.Config.PullInterval, in.PollsTotalCounter, in.Reader) | ||
if err != nil { | ||
return nil, errors.Join(err, errFailedConfig) | ||
} | ||
|
||
return client, nil | ||
} | ||
|
||
func ProvideDefaultListenerReader(client *BasicClient) Reader { | ||
return client | ||
} | ||
|
||
type StartListenerIn struct { | ||
fx.In | ||
|
||
Listener *ListenerClient | ||
LC fx.Lifecycle | ||
} | ||
|
||
// ProvideStartListenerClient starts the Argus listener client service. | ||
func ProvideStartListenerClient(in StartListenerIn) error { | ||
in.Listener.Start(context.Background()) | ||
in.LC.Append(fx.StopHook(in.Listener.Stop)) | ||
|
||
return nil | ||
} |
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 |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// SPDX-FileCopyrightText: 2025 Comcast Cable Communications Management, LLC | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package ancla | ||
|
||
import ( | ||
"github.com/xmidt-org/ancla/chrysom" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
"go.uber.org/fx" | ||
) | ||
|
||
type ServiceIn struct { | ||
fx.In | ||
|
||
// Ancla Client. | ||
BasicClient *chrysom.BasicClient | ||
} | ||
|
||
// ProvideService builds the Argus client service from the given configuration. | ||
func ProvideService(in ServiceIn) Service { | ||
return NewService(in.BasicClient) | ||
} | ||
|
||
// TODO: Refactor and move Watch and Listener related code to chrysom. | ||
type DefaultListenersIn struct { | ||
fx.In | ||
|
||
// Metric for webhook list size, used by the webhook list size watcher listener. | ||
WebhookListSizeGauge prometheus.Gauge `name:"webhook_list_size"` | ||
} | ||
|
||
type DefaultListenerOut struct { | ||
fx.Out | ||
|
||
Watchers []Watch `group:"watchers,flatten"` | ||
} | ||
|
||
func ProvideDefaultListenerWatchers(in DefaultListenersIn) DefaultListenerOut { | ||
var watchers []Watch | ||
|
||
watchers = append(watchers, webhookListSizeWatch(in.WebhookListSizeGauge)) | ||
|
||
return DefaultListenerOut{ | ||
Watchers: watchers, | ||
} | ||
} | ||
|
||
type ListenerIn struct { | ||
fx.In | ||
|
||
Shutdowner fx.Shutdowner | ||
// Watchers are called by the Listener when new webhooks are fetched. | ||
Watchers []Watch `group:"watchers"` | ||
} | ||
|
||
func ProvideListener(in ListenerIn) chrysom.Listener { | ||
return chrysom.ListenerFunc(func(items chrysom.Items) { | ||
iws, err := ItemsToInternalWebhooks(items) | ||
if err != nil { | ||
in.Shutdowner.Shutdown(fx.ExitCode(1)) | ||
|
||
return | ||
} | ||
|
||
for _, watch := range in.Watchers { | ||
watch.Update(iws) | ||
} | ||
}) | ||
} |
Oops, something went wrong.