Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tenant id to logs #856

Closed
wants to merge 13 commits into from
Closed
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ internal_image_registry:=image-registry.openshift-image-registry.svc:5000
test_image:=test/$(IMAGE_NAME)

DOCKER ?= docker
DOCKER_CONFIG ?= "${PWD}/.docker"
DOCKER_CONFIG ?= "${HOME}/.docker"

# Default Variables
ACSMS_NAMESPACE ?= acsms
Expand Down
14 changes: 11 additions & 3 deletions fleetshard/pkg/central/reconciler/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ func (r *CentralReconciler) Reconcile(ctx context.Context, remoteCentral private
return nil, errors.Wrapf(err, "checking if central changed")
}

glog.Infof(r.getLogMessage("start reconcile central"))

remoteCentralName := remoteCentral.Metadata.Name
remoteCentralNamespace := remoteCentral.Metadata.Namespace
if !changed && r.wantsAuthProvider == r.hasAuthProvider && isRemoteCentralReady(remoteCentral) {
Expand Down Expand Up @@ -201,7 +203,7 @@ func (r *CentralReconciler) Reconcile(ctx context.Context, remoteCentral private
// If sso.redhat.com auth provider exists, there is no need for admin/password login.
// We also store whether auth provider exists within reconciler instance to avoid polluting network.
if exists {
glog.Infof("Auth provider for %s/%s already exists", remoteCentralNamespace, remoteCentralName)
glog.Infof("Auth provider for %s/%s already exists. Id: %s", remoteCentralNamespace, remoteCentralName, remoteCentral.Id)
r.hasAuthProvider = true
}
}
Expand All @@ -226,7 +228,7 @@ func (r *CentralReconciler) Reconcile(ctx context.Context, remoteCentral private
tenantIDLabelKey: remoteCentral.Id,
}
if err := r.ensureNamespaceExists(remoteCentralNamespace, namespaceLabels); err != nil {
return nil, errors.Wrapf(err, "unable to ensure that namespace %s exists", remoteCentralNamespace)
return nil, errors.Wrapf(err, "unable to ensure that namespace %s exists. ID: %s", remoteCentralNamespace, remoteCentral.Id)
}

if err := r.ensureChartResourcesExist(ctx, remoteCentral); err != nil {
Expand Down Expand Up @@ -267,7 +269,7 @@ func (r *CentralReconciler) Reconcile(ctx context.Context, remoteCentral private
err = r.client.Get(ctx, ctrlClient.ObjectKey{Namespace: remoteCentralNamespace, Name: remoteCentralName}, &existingCentral)
if err != nil {
if !apiErrors.IsNotFound(err) {
return nil, errors.Wrapf(err, "unable to check the existence of central %s/%s", central.GetNamespace(), central.GetName())
return nil, errors.Wrapf(err, "unable to check the existence of central %s/%s. Id: %s", central.GetNamespace(), central.GetName(), remoteCentral.Id)
}
centralExists = false
}
Expand Down Expand Up @@ -354,6 +356,12 @@ func (r *CentralReconciler) Reconcile(ctx context.Context, remoteCentral private
return status, nil
}

func (c CentralReconciler) getLogMessage(msg string, args ...string) string {
idPrefixMsg := fmt.Sprintf("[tenant-id: %s, namespace: %s]", c.central.Id, c.central.Metadata.Namespace)
message := fmt.Sprintf(msg, args)
return fmt.Sprintf("%s %s", idPrefixMsg, message)
}

func isRemoteCentralProvisioning(remoteCentral private.ManagedCentral) bool {
return remoteCentral.RequestStatus == centralConstants.CentralRequestStatusProvisioning.String()
}
Expand Down
7 changes: 5 additions & 2 deletions fleetshard/pkg/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package runtime
import (
"context"
"fmt"
"github.com/stackrox/acs-fleet-manager/pkg/logger"
"time"

"github.com/golang/glog"
Expand All @@ -27,6 +28,8 @@ import (
// TODO(SimonBaeumer): set a unique identifier for the map key, currently the instance name is used
type reconcilerRegistry map[string]*centralReconciler.CentralReconciler

var reconciledCentralCountCache int32

var backoff = wait.Backoff{
Duration: 1 * time.Second,
Factor: 1.5,
Expand Down Expand Up @@ -119,7 +122,8 @@ func (r *Runtime) Start() error {
}

// Start for each Central its own reconciler which can be triggered by sending a central to the receive channel.
glog.Infof("Received %d centrals", len(list.Items))
reconciledCentralCountCache = int32(len(list.Items))
logger.InfoChangedInt32(&reconciledCentralCountCache, "Received central count changed: received %d centrals", reconciledCentralCountCache)
for _, central := range list.Items {
if _, ok := r.reconcilers[central.Id]; !ok {
r.reconcilers[central.Id] = centralReconciler.NewCentralReconciler(r.k8sClient, central,
Expand All @@ -135,7 +139,6 @@ func (r *Runtime) Start() error {
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Minute)
defer cancel()

glog.Infof("Start reconcile central %s/%s", central.Metadata.Namespace, central.Metadata.Name)
status, err := reconciler.Reconcile(ctx, central)
fleetshardmetrics.MetricsInstance().IncCentralReconcilations()
r.handleReconcileResult(central, status, err)
Expand Down
14 changes: 8 additions & 6 deletions internal/dinosaur/pkg/workers/clusters_mgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ package workers

import (
"fmt"

dinosaurConstants "github.com/stackrox/acs-fleet-manager/internal/dinosaur/constants"
"github.com/stackrox/acs-fleet-manager/internal/dinosaur/pkg/clusters/types"
"github.com/stackrox/acs-fleet-manager/internal/dinosaur/pkg/config"
"github.com/stackrox/acs-fleet-manager/pkg/client/observatorium"
"github.com/stackrox/acs-fleet-manager/pkg/client/ocm"
"github.com/stackrox/acs-fleet-manager/pkg/constants"
"github.com/stackrox/acs-fleet-manager/pkg/logger"

"strings"
"sync"

Expand Down Expand Up @@ -59,6 +57,10 @@ const (
clusterAdminRoleName = "cluster-admin"
)

var (
readyClusterCount int32
)

var clusterMetricsStatuses = []api.ClusterStatus{
api.ClusterAccepted,
api.ClusterProvisioning,
Expand Down Expand Up @@ -322,9 +324,9 @@ func (c *ClusterManager) processReadyClusters() []error {
errs = append(errs, errors.Wrap(listErr, "failed to list ready clusters"))
return errs
}
if len(readyClusters) > 0 {
glog.V(10).Infof("ready clusters count = %d", len(readyClusters))
}

readyClusterCount = int32(len(readyClusters))
logger.InfoChangedInt32(&readyClusterCount, "ready clusters count = %d", readyClusterCount)

for _, readyCluster := range readyClusters {
emptyClusterReconciled := false
Expand Down Expand Up @@ -472,9 +474,9 @@ func (c *ClusterManager) reconcileClusterInstanceType(cluster api.Cluster) error
if err != nil {
return errors.Wrapf(err, "failed to update instance type in database for cluster %s", cluster.ClusterID)
}
logger.Logger.V(10).Infof("supported instance type for cluster = %s successful updated", cluster.ClusterID)
}

logger.Logger.V(10).Infof("supported instance type for cluster = %s successful updated", cluster.ClusterID)
return nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ func (k *AcceptedCentralManager) Stop() {
k.StopWorker(k)
}

var acceptedCentralCount int32

// Reconcile ...
func (k *AcceptedCentralManager) Reconcile() []error {
var encounteredErrors []error
Expand All @@ -68,9 +70,9 @@ func (k *AcceptedCentralManager) Reconcile() []error {
if serviceErr != nil {
encounteredErrors = append(encounteredErrors, errors.Wrap(serviceErr, "failed to list accepted centrals"))
}
if len(acceptedCentralRequests) > 0 {
glog.Infof("accepted centrals count = %d", len(acceptedCentralRequests))
}

acceptedCentralCount = int32(len(acceptedCentralRequests))
logger.InfoChangedInt32(&acceptedCentralCount, "accepted centrals count = %d", acceptedCentralCount)

for _, centralRequest := range acceptedCentralRequests {
glog.V(10).Infof("accepted central id = %s", centralRequest.ID)
Expand Down
17 changes: 6 additions & 11 deletions internal/dinosaur/pkg/workers/dinosaurmgrs/ready_dinosaurs_mgr.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package dinosaurmgrs

import (
"github.com/golang/glog"
"github.com/google/uuid"
"github.com/pkg/errors"
constants2 "github.com/stackrox/acs-fleet-manager/internal/dinosaur/constants"
"github.com/stackrox/acs-fleet-manager/internal/dinosaur/pkg/services"
"github.com/stackrox/acs-fleet-manager/pkg/client/iam"
"github.com/stackrox/acs-fleet-manager/pkg/logger"
"github.com/stackrox/acs-fleet-manager/pkg/metrics"
"github.com/stackrox/acs-fleet-manager/pkg/services/sso"
"github.com/stackrox/acs-fleet-manager/pkg/workers"
)

const readyCentralWorkerType = "ready_dinosaur"

var readyCentralCountCache int32

// ReadyDinosaurManager represents a dinosaur manager that periodically reconciles dinosaur requests
type ReadyDinosaurManager struct {
workers.BaseWorker
Expand Down Expand Up @@ -49,21 +51,14 @@ func (k *ReadyDinosaurManager) Stop() {

// Reconcile ...
func (k *ReadyDinosaurManager) Reconcile() []error {

var encounteredErrors []error

readyDinosaurs, serviceErr := k.dinosaurService.ListByStatus(constants2.CentralRequestStatusReady)
readyCentrals, serviceErr := k.dinosaurService.ListByStatus(constants2.CentralRequestStatusReady)
if serviceErr != nil {
encounteredErrors = append(encounteredErrors, errors.Wrap(serviceErr, "failed to list ready centrals"))
}
if len(readyDinosaurs) > 0 {
glog.Infof("ready centrals count = %d", len(readyDinosaurs))
}

for _, dinosaur := range readyDinosaurs {
glog.V(10).Infof("ready central id = %s", dinosaur.ID)
// TODO implement reconciliation logic for ready dinosaurs
}
readyCentralCountCache = int32(len(readyCentrals))
logger.InfoChangedInt32(&readyCentralCountCache, "ready centrals count = %d", readyCentralCountCache)

return encounteredErrors
}
16 changes: 14 additions & 2 deletions pkg/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ package logger
import (
"context"
"fmt"
"strings"

sentry "github.com/getsentry/sentry-go"
"strings"
"sync"

"github.com/golang-jwt/jwt/v4"
"github.com/golang/glog"
Expand Down Expand Up @@ -189,6 +189,18 @@ func (l *logger) InfoDepth(depth int, args ...interface{}) {
glog.InfoDepth(logDepth+depth, args...)
}

var cache = sync.Map{}

// InfoChangedInt32 logs whenever the value of the passed counter reference changes when it is called.
func InfoChangedInt32(counter *int32, msg string, args ...interface{}) {
val, loaded := cache.LoadOrStore(counter, *counter)
if loaded && val == *counter {
return // counter not changed, do not log
}

glog.InfoDepth(logDepth, fmt.Sprintf(msg, args...))
}

// Warningf ...
func (l *logger) Warningf(format string, args ...interface{}) {
prefixed := l.prepareLogPrefix(format, args...)
Expand Down