Skip to content

Commit

Permalink
fix: do not unregister Kong DPs metrics when Konnect integration is e…
Browse files Browse the repository at this point in the history
…nabled (#6881)

(cherry picked from commit 2c72b40)
  • Loading branch information
czeslavo committed Dec 23, 2024
1 parent 67fa9b5 commit 6e2d40d
Show file tree
Hide file tree
Showing 12 changed files with 243 additions and 138 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ require (
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/power-devops/perfstat v0.0.0-20221212215047-62379fc7944b // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/client_model v0.6.1
github.com/prometheus/procfs v0.15.1 // indirect
github.com/puzpuzpuz/xsync/v2 v2.5.1 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
Expand Down
31 changes: 16 additions & 15 deletions internal/dataplane/kong_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ type KongClient struct {
// information during data-plane update runtime.
diagnostic diagnostics.ConfigDumpDiagnostic

// prometheusMetrics is the client for shipping metrics information
// metricsRecorder is the client for shipping metrics information
// updates to the prometheus exporter.
prometheusMetrics *metrics.CtrlFuncMetrics
metricsRecorder metrics.Recorder

// kubernetesObjectReportLock is a mutex for thread-safety of
// kubernetes object reporting functionality.
Expand Down Expand Up @@ -201,12 +201,13 @@ func NewKongClient(
kongConfigBuilder KongConfigBuilder,
cacheStores *store.CacheStores,
fallbackConfigGenerator FallbackConfigGenerator,
metricsRecorder metrics.Recorder,
) (*KongClient, error) {
c := &KongClient{
logger: logger,
requestTimeout: timeout,
diagnostic: diagnostic,
prometheusMetrics: metrics.NewCtrlFuncMetrics(),
metricsRecorder: metricsRecorder,
cache: cacheStores,
kongConfig: kongConfig,
eventRecorder: eventRecorder,
Expand Down Expand Up @@ -455,9 +456,9 @@ func (c *KongClient) Update(ctx context.Context) error {
}
hasNewSnapshotToBeProcessed := newSnapshotHash != store.SnapshotHashEmpty
if !hasNewSnapshotToBeProcessed {
c.prometheusMetrics.RecordProcessedConfigSnapshotCacheHit()
c.metricsRecorder.RecordProcessedConfigSnapshotCacheHit()
} else {
c.prometheusMetrics.RecordProcessedConfigSnapshotCacheMiss()
c.metricsRecorder.RecordProcessedConfigSnapshotCacheMiss()
}
if hasNewSnapshotToBeProcessed {
c.logger.V(logging.DebugLevel).Info("New configuration snapshot detected", "hash", newSnapshotHash)
Expand All @@ -479,13 +480,13 @@ func (c *KongClient) Update(ctx context.Context) error {
translationDuration := time.Since(translationStart)

if failuresCount := len(parsingResult.TranslationFailures); failuresCount > 0 {
c.prometheusMetrics.RecordTranslationFailure(translationDuration)
c.prometheusMetrics.RecordTranslationBrokenResources(failuresCount)
c.metricsRecorder.RecordTranslationFailure(translationDuration)
c.metricsRecorder.RecordTranslationBrokenResources(failuresCount)
c.recordResourceFailureEvents(parsingResult.TranslationFailures, KongConfigurationTranslationFailedEventReason)
c.logger.V(logging.DebugLevel).Info("Translation failures occurred when building data-plane configuration", "count", failuresCount)
} else {
c.prometheusMetrics.RecordTranslationSuccess(translationDuration)
c.prometheusMetrics.RecordTranslationBrokenResources(0)
c.metricsRecorder.RecordTranslationSuccess(translationDuration)
c.metricsRecorder.RecordTranslationBrokenResources(0)
c.logger.V(logging.DebugLevel).Info("Successfully built data-plane configuration", "duration", translationDuration.String())
}

Expand Down Expand Up @@ -617,12 +618,12 @@ func (c *KongClient) tryRecoveringWithFallbackConfiguration(

if failuresCount := len(fallbackParsingResult.TranslationFailures); failuresCount > 0 {
c.recordResourceFailureEvents(fallbackParsingResult.TranslationFailures, FallbackKongConfigurationTranslationFailedEventReason)
c.prometheusMetrics.RecordFallbackTranslationBrokenResources(failuresCount)
c.prometheusMetrics.RecordFallbackTranslationFailure(translationDuration)
c.metricsRecorder.RecordFallbackTranslationBrokenResources(failuresCount)
c.metricsRecorder.RecordFallbackTranslationFailure(translationDuration)
c.logger.V(logging.DebugLevel).Info("Translation failures occurred when building fallback data-plane configuration", "count", failuresCount, "duration", translationDuration.String())
} else {
c.prometheusMetrics.RecordFallbackTranslationBrokenResources(0)
c.prometheusMetrics.RecordFallbackTranslationSuccess(translationDuration)
c.metricsRecorder.RecordFallbackTranslationBrokenResources(0)
c.metricsRecorder.RecordFallbackTranslationSuccess(translationDuration)
c.logger.V(logging.DebugLevel).Info("Successfully built fallback configuration from caches", "duration", translationDuration.String())
}

Expand All @@ -647,7 +648,7 @@ func (c *KongClient) generateFallbackCache(
) (s store.CacheStores, metadata fallback.GeneratedCacheMetadata, err error) {
start := time.Now()
defer func() {
c.prometheusMetrics.RecordFallbackCacheGenerationDuration(time.Since(start), err)
c.metricsRecorder.RecordFallbackCacheGenerationDuration(time.Since(start), err)
}()
if c.kongConfig.UseLastValidConfigForFallback {
return c.fallbackConfigGenerator.GenerateBackfillingBrokenObjects(
Expand Down Expand Up @@ -798,7 +799,7 @@ func (c *KongClient) sendToClient(
config,
targetContent,
customEntities,
c.prometheusMetrics,
c.metricsRecorder,
c.updateStrategyResolver,
c.configChangeDetector,
isFallback,
Expand Down
1 change: 1 addition & 0 deletions internal/dataplane/kong_client_golden_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ func runKongClientGoldenTest(t *testing.T, tc kongClientGoldenTestCase) {
p,
&cacheStores,
fallbackConfigGenerator,
mocks.MetricsRecorder{},
)
require.NoError(t, err)

Expand Down
7 changes: 7 additions & 0 deletions internal/dataplane/kong_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1048,6 +1048,7 @@ func setupTestKongClient(
configBuilder,
&cacheStores,
newMockFallbackConfigGenerator(),
mocks.MetricsRecorder{},
)
require.NoError(t, err)
return kongClient
Expand Down Expand Up @@ -1078,6 +1079,7 @@ func attachKonnectConfigSynchronizer(
updateStrategyResolver,
configChangeDetector,
configStatusNotifier,
mocks.MetricsRecorder{},
)
kc.SetKonnectConfigSynchronizer(konnectConfigSynchronizer)
err := konnectConfigSynchronizer.Start(ctx)
Expand Down Expand Up @@ -1355,6 +1357,7 @@ func TestKongClient_FallbackConfiguration_SuccessfulRecovery(t *testing.T) {
configBuilder,
&originalCache,
fallbackConfigGenerator,
mocks.MetricsRecorder{},
)
require.NoError(t, err)

Expand Down Expand Up @@ -1490,6 +1493,7 @@ func TestKongClient_FallbackConfiguration_SkipsUpdateWhenInSync(t *testing.T) {
configBuilder,
&originalCache,
fallbackConfigGenerator,
mocks.MetricsRecorder{},
)
require.NoError(t, err)

Expand Down Expand Up @@ -1636,6 +1640,7 @@ func TestKongClient_FallbackConfiguration_FailedRecovery(t *testing.T) {
configBuilder,
&originalCache,
fallbackConfigGenerator,
mocks.MetricsRecorder{},
)
require.NoError(t, err)

Expand Down Expand Up @@ -1745,6 +1750,7 @@ func TestKongClient_LastValidCacheSnapshot(t *testing.T) {
configBuilder,
&originalCache,
fallbackConfigGenerator,
mocks.MetricsRecorder{},
)
require.NoError(t, err)

Expand Down Expand Up @@ -1967,6 +1973,7 @@ func TestKongClient_RecoveringFromGatewaySyncError(t *testing.T) {
configBuilder,
&originalCache,
fallbackConfigGenerator,
mocks.MetricsRecorder{},
)
require.NoError(t, err)

Expand Down
2 changes: 1 addition & 1 deletion internal/dataplane/sendconfig/sendconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func PerformUpdate(
config Config,
targetContent *file.Content,
customEntities CustomEntitiesByType,
promMetrics *metrics.CtrlFuncMetrics,
promMetrics metrics.Recorder,
updateStrategyResolver UpdateStrategyResolver,
configChangeDetector ConfigurationChangeDetector,
isFallback bool,
Expand Down
7 changes: 4 additions & 3 deletions internal/konnect/config_synchronizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type ConfigSynchronizer struct {
syncTicker *time.Ticker
kongConfig sendconfig.Config
clientsProvider clients.AdminAPIClientsProvider
prometheusMetrics *metrics.CtrlFuncMetrics
metricsRecorder metrics.Recorder
updateStrategyResolver sendconfig.UpdateStrategyResolver
configChangeDetector sendconfig.ConfigurationChangeDetector
configStatusNotifier clients.ConfigStatusNotifier
Expand All @@ -49,13 +49,14 @@ func NewConfigSynchronizer(
updateStrategyResolver sendconfig.UpdateStrategyResolver,
configChangeDetector sendconfig.ConfigurationChangeDetector,
configStatusNotifier clients.ConfigStatusNotifier,
metricsRecorder metrics.Recorder,
) *ConfigSynchronizer {
return &ConfigSynchronizer{
logger: logger,
syncTicker: time.NewTicker(configUploadPeriod),
kongConfig: kongConfig,
clientsProvider: clientsProvider,
prometheusMetrics: metrics.NewCtrlFuncMetrics(),
metricsRecorder: metricsRecorder,
updateStrategyResolver: updateStrategyResolver,
configChangeDetector: configChangeDetector,
configStatusNotifier: configStatusNotifier,
Expand Down Expand Up @@ -135,7 +136,7 @@ func (s *ConfigSynchronizer) uploadConfig(ctx context.Context, client *adminapi.
targetContent,
// Konnect client does not upload custom entities.
sendconfig.CustomEntitiesByType{},
s.prometheusMetrics,
s.metricsRecorder,
s.updateStrategyResolver,
s.configChangeDetector,
isFallback,
Expand Down
3 changes: 2 additions & 1 deletion internal/konnect/config_synchronizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
dpconf "github.com/kong/kubernetes-ingress-controller/v3/internal/dataplane/config"
"github.com/kong/kubernetes-ingress-controller/v3/internal/dataplane/sendconfig"
"github.com/kong/kubernetes-ingress-controller/v3/internal/metrics"
"github.com/kong/kubernetes-ingress-controller/v3/test/mocks"
)

func TestConfigSynchronizer_GetTargetContentCopy(t *testing.T) {
Expand Down Expand Up @@ -184,7 +185,7 @@ func TestConfigSynchronizer_RunKonnectUpdateServer(t *testing.T) {
clientsProvider: &mockGatewayClientsProvider{
konnectClient: testKonnectClient,
},
prometheusMetrics: metrics.NewCtrlFuncMetrics(),
metricsRecorder: mocks.MetricsRecorder{},
updateStrategyResolver: resolver,
configChangeDetector: mockConfigurationChangeDetector{},
configStatusNotifier: clients.NoOpConfigStatusNotifier{},
Expand Down
4 changes: 4 additions & 0 deletions internal/manager/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"github.com/kong/kubernetes-ingress-controller/v3/internal/manager/metadata"
"github.com/kong/kubernetes-ingress-controller/v3/internal/manager/telemetry"
"github.com/kong/kubernetes-ingress-controller/v3/internal/manager/utils/kongconfig"
"github.com/kong/kubernetes-ingress-controller/v3/internal/metrics"
"github.com/kong/kubernetes-ingress-controller/v3/internal/store"
"github.com/kong/kubernetes-ingress-controller/v3/internal/util"
"github.com/kong/kubernetes-ingress-controller/v3/internal/util/kubernetes/object/status"
Expand Down Expand Up @@ -195,6 +196,7 @@ func Run(
configurationChangeDetector := sendconfig.NewDefaultConfigurationChangeDetector(logger)
kongConfigFetcher := configfetcher.NewDefaultKongLastGoodConfigFetcher(translatorFeatureFlags.FillIDs, c.KongWorkspace)
fallbackConfigGenerator := fallback.NewGenerator(fallback.NewDefaultCacheGraphProvider(), logger)
metricsRecorder := metrics.NewGlobalCtrlRuntimeMetricsRecorder()
dataplaneClient, err := dataplane.NewKongClient(
logger,
time.Duration(c.ProxyTimeoutSeconds*float32(time.Second)),
Expand All @@ -209,6 +211,7 @@ func Run(
configTranslator,
&cache,
fallbackConfigGenerator,
metricsRecorder,
)
if err != nil {
return fmt.Errorf("failed to initialize kong data-plane client: %w", err)
Expand Down Expand Up @@ -288,6 +291,7 @@ func Run(
clientsManager,
updateStrategyResolver,
configStatusNotifier,
metricsRecorder,
)
if err != nil {
setupLog.Error(err, "Failed to setup Konnect configuration synchronizer with manager, skipping")
Expand Down
3 changes: 3 additions & 0 deletions internal/manager/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
"github.com/kong/kubernetes-ingress-controller/v3/internal/license"
"github.com/kong/kubernetes-ingress-controller/v3/internal/logging"
"github.com/kong/kubernetes-ingress-controller/v3/internal/manager/scheme"
"github.com/kong/kubernetes-ingress-controller/v3/internal/metrics"
"github.com/kong/kubernetes-ingress-controller/v3/internal/store"
"github.com/kong/kubernetes-ingress-controller/v3/internal/util/kubernetes/object/status"
)
Expand Down Expand Up @@ -495,6 +496,7 @@ func setupKonnectConfigSynchronizer(
clientsProvider clients.AdminAPIClientsProvider,
updateStrategyResolver sendconfig.UpdateStrategyResolver,
configStatusNotifier clients.ConfigStatusNotifier,
metricsRecorder metrics.Recorder,
) (*konnect.ConfigSynchronizer, error) {
logger := ctrl.LoggerFrom(ctx).WithName("konnect-config-synchronizer")
s := konnect.NewConfigSynchronizer(
Expand All @@ -505,6 +507,7 @@ func setupKonnectConfigSynchronizer(
updateStrategyResolver,
sendconfig.NewDefaultConfigurationChangeDetector(logger),
configStatusNotifier,
metricsRecorder,
)
err := mgr.Add(s)
if err != nil {
Expand Down
Loading

0 comments on commit 6e2d40d

Please sign in to comment.