@@ -22,7 +22,11 @@ import (
2222 "math"
2323
2424 "github.com/apache/dubbo-admin/pkg/core/events"
25+ "github.com/apache/dubbo-admin/pkg/core/logger"
26+ meshresource "github.com/apache/dubbo-admin/pkg/core/resource/apis/mesh/v1alpha1"
27+ resmodel "github.com/apache/dubbo-admin/pkg/core/resource/model"
2528 "github.com/apache/dubbo-admin/pkg/core/runtime"
29+ "github.com/apache/dubbo-admin/pkg/core/store"
2630)
2731
2832const ComponentType runtime.ComponentType = "counter manager"
@@ -41,7 +45,7 @@ var _ ManagerComponent = &managerComponent{}
4145func (c * managerComponent ) RequiredDependencies () []runtime.ComponentType {
4246 return []runtime.ComponentType {
4347 runtime .ResourceStore ,
44- runtime .EventBus , // Counter depends on EventBus to subscribe to events
48+ runtime .EventBus ,
4549 }
4650}
4751
@@ -64,6 +68,19 @@ func (c *managerComponent) Init(runtime.BuilderContext) error {
6468}
6569
6670func (c * managerComponent ) Start (rt runtime.Runtime , _ <- chan struct {}) error {
71+ storeComponent , err := rt .GetComponent (runtime .ResourceStore )
72+ if err != nil {
73+ return err
74+ }
75+ storeRouter , ok := storeComponent .(store.Router )
76+ if ! ok {
77+ return fmt .Errorf ("component %s does not implement store.Router" , runtime .ResourceStore )
78+ }
79+
80+ if err := c .initializeCountsFromStore (storeRouter ); err != nil {
81+ logger .Warnf ("Failed to initialize counter manager from store: %v" , err )
82+ }
83+
6784 component , err := rt .GetComponent (runtime .EventBus )
6885 if err != nil {
6986 return err
@@ -75,6 +92,73 @@ func (c *managerComponent) Start(rt runtime.Runtime, _ <-chan struct{}) error {
7592 return c .manager .Bind (bus )
7693}
7794
95+ func (c * managerComponent ) initializeCountsFromStore (storeRouter store.Router ) error {
96+ if err := c .initializeResourceCount (storeRouter , meshresource .InstanceKind ); err != nil {
97+ return fmt .Errorf ("failed to initialize instance count: %w" , err )
98+ }
99+
100+ if err := c .initializeResourceCount (storeRouter , meshresource .ApplicationKind ); err != nil {
101+ return fmt .Errorf ("failed to initialize application count: %w" , err )
102+ }
103+
104+ if err := c .initializeResourceCount (storeRouter , meshresource .ServiceProviderMetadataKind ); err != nil {
105+ return fmt .Errorf ("failed to initialize service provider metadata count: %w" , err )
106+ }
107+
108+ return nil
109+ }
110+
111+ func (c * managerComponent ) initializeResourceCount (storeRouter store.Router , kind resmodel.ResourceKind ) error {
112+ resourceStore , err := storeRouter .ResourceKindRoute (kind )
113+ if err != nil {
114+ return err
115+ }
116+
117+ allResources := resourceStore .List ()
118+ cm := c .manager .(* counterManager )
119+
120+ for _ , obj := range allResources {
121+ resource , ok := obj .(resmodel.Resource )
122+ if ! ok {
123+ continue
124+ }
125+
126+ mesh := resource .ResourceMesh ()
127+ if mesh == "" {
128+ mesh = "default"
129+ }
130+
131+ if counter , exists := cm .simpleCounters [kind ]; exists {
132+ counter .Increment (mesh )
133+ }
134+
135+ if kind == meshresource .InstanceKind {
136+ instance , ok := resource .(* meshresource.InstanceResource )
137+ if ok && instance .Spec != nil {
138+ protocol := instance .Spec .GetProtocol ()
139+ if protocol != "" {
140+ if cfg := cm .getDistributionConfig (kind , ProtocolCounter ); cfg != nil {
141+ cfg .counter .Increment (mesh , protocol )
142+ }
143+ }
144+
145+ releaseVersion := instance .Spec .GetReleaseVersion ()
146+ if releaseVersion != "" {
147+ if cfg := cm .getDistributionConfig (kind , ReleaseCounter ); cfg != nil {
148+ cfg .counter .Increment (mesh , releaseVersion )
149+ }
150+ }
151+
152+ if cfg := cm .getDistributionConfig (kind , DiscoveryCounter ); cfg != nil {
153+ cfg .counter .Increment (mesh , mesh )
154+ }
155+ }
156+ }
157+ }
158+
159+ return nil
160+ }
161+
78162func (c * managerComponent ) CounterManager () CounterManager {
79163 return c .manager
80164}
0 commit comments