@@ -803,58 +803,141 @@ func (pool *ConnPool[C]) StatsJSON() map[string]any {
803
803
}
804
804
}
805
805
806
- // RegisterStats registers this pool's metrics into a stats Exporter
807
- func (pool * ConnPool [C ]) RegisterStats (stats * servenv.Exporter , name string ) {
808
- if stats == nil || name == "" {
809
- return
810
- }
806
+ type StatsExporter [C Connection ] struct {
807
+ // The Pool for which this exporter is exporting stats.
808
+ // It is an atomic pointer so that it can be updated safely.
809
+ // The pointer is nil if the pool has not been registered yet.
810
+ pool atomic.Pointer [ConnPool [C ]]
811
+ }
811
812
812
- pool .Name = name
813
+ func NewStatsExporter [C Connection ](stats * servenv.Exporter , name string ) * StatsExporter [C ] {
814
+ se := & StatsExporter [C ]{}
813
815
814
816
stats .NewGaugeFunc (name + "Capacity" , "Tablet server conn pool capacity" , func () int64 {
817
+ pool := se .pool .Load ()
818
+ if pool == nil {
819
+ return 0
820
+ }
821
+
815
822
return pool .Capacity ()
816
823
})
817
824
stats .NewGaugeFunc (name + "Available" , "Tablet server conn pool available" , func () int64 {
825
+ pool := se .pool .Load ()
826
+ if pool == nil {
827
+ return 0
828
+ }
829
+
818
830
return pool .Available ()
819
831
})
820
832
stats .NewGaugeFunc (name + "Active" , "Tablet server conn pool active" , func () int64 {
833
+ pool := se .pool .Load ()
834
+ if pool == nil {
835
+ return 0
836
+ }
837
+
821
838
return pool .Active ()
822
839
})
823
840
stats .NewGaugeFunc (name + "InUse" , "Tablet server conn pool in use" , func () int64 {
841
+ pool := se .pool .Load ()
842
+ if pool == nil {
843
+ return 0
844
+ }
845
+
824
846
return pool .InUse ()
825
847
})
826
848
stats .NewGaugeFunc (name + "MaxCap" , "Tablet server conn pool max cap" , func () int64 {
849
+ pool := se .pool .Load ()
850
+ if pool == nil {
851
+ return 0
852
+ }
853
+
827
854
// the smartconnpool doesn't have a maximum capacity
828
855
return pool .Capacity ()
829
856
})
830
857
stats .NewGaugeFunc (name + "IdleAllowed" , "Tablet server conn pool idle allowed limit" , func () int64 {
858
+ pool := se .pool .Load ()
859
+ if pool == nil {
860
+ return 0
861
+ }
862
+
831
863
return pool .IdleCount ()
832
864
})
833
865
stats .NewCounterFunc (name + "WaitCount" , "Tablet server conn pool wait count" , func () int64 {
866
+ pool := se .pool .Load ()
867
+ if pool == nil {
868
+ return 0
869
+ }
870
+
834
871
return pool .Metrics .WaitCount ()
835
872
})
836
873
stats .NewCounterDurationFunc (name + "WaitTime" , "Tablet server wait time" , func () time.Duration {
874
+ pool := se .pool .Load ()
875
+ if pool == nil {
876
+ return 0
877
+ }
878
+
837
879
return pool .Metrics .WaitTime ()
838
880
})
839
881
stats .NewGaugeDurationFunc (name + "IdleTimeout" , "Tablet server idle timeout" , func () time.Duration {
882
+ pool := se .pool .Load ()
883
+ if pool == nil {
884
+ return 0
885
+ }
886
+
840
887
return pool .IdleTimeout ()
841
888
})
842
889
stats .NewCounterFunc (name + "IdleClosed" , "Tablet server conn pool idle closed" , func () int64 {
890
+ pool := se .pool .Load ()
891
+ if pool == nil {
892
+ return 0
893
+ }
894
+
843
895
return pool .Metrics .IdleClosed ()
844
896
})
845
897
stats .NewCounterFunc (name + "MaxLifetimeClosed" , "Tablet server conn pool refresh closed" , func () int64 {
898
+ pool := se .pool .Load ()
899
+ if pool == nil {
900
+ return 0
901
+ }
902
+
846
903
return pool .Metrics .MaxLifetimeClosed ()
847
904
})
848
905
stats .NewCounterFunc (name + "Get" , "Tablet server conn pool get count" , func () int64 {
906
+ pool := se .pool .Load ()
907
+ if pool == nil {
908
+ return 0
909
+ }
910
+
849
911
return pool .Metrics .GetCount ()
850
912
})
851
913
stats .NewCounterFunc (name + "GetSetting" , "Tablet server conn pool get with setting count" , func () int64 {
914
+ pool := se .pool .Load ()
915
+ if pool == nil {
916
+ return 0
917
+ }
918
+
852
919
return pool .Metrics .GetSettingCount ()
853
920
})
854
921
stats .NewCounterFunc (name + "DiffSetting" , "Number of times pool applied different setting" , func () int64 {
922
+ pool := se .pool .Load ()
923
+ if pool == nil {
924
+ return 0
925
+ }
926
+
855
927
return pool .Metrics .DiffSettingCount ()
856
928
})
857
929
stats .NewCounterFunc (name + "ResetSetting" , "Number of times pool reset the setting" , func () int64 {
930
+ pool := se .pool .Load ()
931
+ if pool == nil {
932
+ return 0
933
+ }
934
+
858
935
return pool .Metrics .ResetSettingCount ()
859
936
})
937
+
938
+ return se
939
+ }
940
+
941
+ func (se * StatsExporter [C ]) SetPool (pool * ConnPool [C ]) {
942
+ se .pool .Store (pool )
860
943
}
0 commit comments