Skip to content

Commit

Permalink
fix duplicate queue metric
Browse files Browse the repository at this point in the history
  • Loading branch information
npetchapan committed Apr 27, 2020
1 parent adc5870 commit 4754eda
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 38 deletions.
2 changes: 1 addition & 1 deletion cmd/staging/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ func startCtrlCmd() *cobra.Command {
samsahaiClient := rpc.NewRPCProtobufClient(viper.GetString(s2h.VKS2HServerURL), &http.Client{})
configCtrl := configctrl.New(mgr)
queueCtrl := queue.New(namespace, runtimeClient)
desiredctrl.New(teamName, mgr, queueCtrl)
authToken := viper.GetString(s2h.VKS2HAuthToken)
desiredctrl.New(teamName, mgr, queueCtrl, authToken, samsahaiClient)
tcBaseURL := viper.GetString(s2h.VKTeamcityURL)
tcUsername := viper.GetString(s2h.VKTeamcityUsername)
tcPassword := viper.GetString(s2h.VKTeamcityPassword)
Expand Down
33 changes: 24 additions & 9 deletions internal/desiredcomponent/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package desiredcomponent
import (
"context"
"fmt"
"net/http"

"github.com/twitchtv/twirp"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
crctrl "sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/handler"
Expand All @@ -19,7 +20,7 @@ import (
s2herrors "github.com/agoda-com/samsahai/internal/errors"
s2hlog "github.com/agoda-com/samsahai/internal/log"
"github.com/agoda-com/samsahai/internal/queue"
"github.com/agoda-com/samsahai/internal/samsahai/exporter"
samsahairpc "github.com/agoda-com/samsahai/pkg/samsahai/rpc"
)

const (
Expand All @@ -32,6 +33,8 @@ type controller struct {
teamName string
queueCtrl internal.QueueController
client client.Client
authToken string
s2hClient samsahairpc.RPC
}

var _ internal.DesiredComponentController = &controller{}
Expand All @@ -40,6 +43,8 @@ func New(
teamName string,
mgr manager.Manager,
queueCtrl internal.QueueController,
authToken string,
s2hClient samsahairpc.RPC,
) internal.DesiredComponentController {
if queueCtrl == nil {
logger.Error(s2herrors.ErrInternalError, "queue ctrl cannot be nil")
Expand All @@ -50,6 +55,8 @@ func New(
teamName: teamName,
queueCtrl: queueCtrl,
client: mgr.GetClient(),
authToken: authToken,
s2hClient: s2hClient,
}

if err := add(mgr, c); err != nil {
Expand Down Expand Up @@ -110,13 +117,21 @@ func (c *controller) Reconcile(req reconcile.Request) (reconcile.Result, error)
return reconcile.Result{}, err
}

queue := &s2hv1beta1.Queue{}
if err = c.client.Get(ctx, types.NamespacedName{
Name: q.Name,
Namespace: req.Namespace}, queue); err != nil {
logger.Error(err, "cannot get the queue")
} else {
exporter.SetQueueMetric(queue)
headers := make(http.Header)
headers.Set(internal.SamsahaiAuthHeader, c.authToken)
ctx, err = twirp.WithHTTPRequestHeaders(ctx, headers)
if err != nil {
logger.Error(err, "cannot set request header")
}

rpcComp := &samsahairpc.ComponentUpgrade{
Name: q.Spec.Name,
Namespace: q.Namespace,
}
if c.s2hClient != nil {
if _, err := c.s2hClient.SendUpdateStateQueueMetric(ctx, rpcComp); err != nil {
logger.Error(err, "cannot send updateQueueWithState queue metric")
}
}

comp.Status.UpdatedAt = &now
Expand Down
45 changes: 27 additions & 18 deletions internal/samsahai/exporter/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@ import (
type ActivePromotionMetricState string

const (
stateWaiting ActivePromotionMetricState = "waiting"
stateDeploying ActivePromotionMetricState = "deploying"
stateTesting ActivePromotionMetricState = "testing"
statePromoting ActivePromotionMetricState = "promoting"
stateDestroying ActivePromotionMetricState = "destroying"
stateWaiting ActivePromotionMetricState = "waiting"
stateDeploying ActivePromotionMetricState = "deploying"
stateTesting ActivePromotionMetricState = "testing"
statePromoting ActivePromotionMetricState = "promoting"
stateDestroying ActivePromotionMetricState = "destroying"
queueStateWaiting string = "waiting"
queueStateDeploying string = "deploying"
queueStateTesting string = "testing"
queueStateCleaning string = "cleaning"
)

var logger = s2hlog.S2HLog.WithName("exporter")
Expand Down Expand Up @@ -56,20 +60,31 @@ func SetTeamNameMetric(teamList *s2hv1beta1.TeamList) {
}
}

func SetHealthStatusMetric(version, gitCommit string, ts float64) {
HealthStatusMetric.WithLabelValues(
version,
gitCommit).Set(ts)
}

func SetQueueMetric(queue *s2hv1beta1.Queue) {
var queueState string
switch queue.Status.State {
case s2hv1beta1.Waiting:
queueState = "waiting"
queueState = queueStateWaiting
case s2hv1beta1.Testing, s2hv1beta1.Collecting:
queueState = "testing"
case s2hv1beta1.Finished:
queueState = "finished"
queueState = queueStateTesting
case s2hv1beta1.DetectingImageMissing, s2hv1beta1.Creating:
queueState = "deploying"
case s2hv1beta1.CleaningBefore, s2hv1beta1.CleaningAfter:
queueState = "cleaning"
queueState = queueStateDeploying
case s2hv1beta1.CleaningBefore:
queueState = queueStateCleaning
case s2hv1beta1.CleaningAfter:
q, err := QueueMetric.CurryWith(prometheus.Labels{"component": queue.Name, "version": queue.Spec.Version})
if err != nil {
logger.Error(err, "cannot get finished queue metric")
}
q.Reset()
}

QueueMetric.WithLabelValues(
queue.Spec.TeamName,
queue.Name,
Expand All @@ -79,12 +94,6 @@ func SetQueueMetric(queue *s2hv1beta1.Queue) {
strconv.Itoa(queue.Status.NoOfProcessed)).Set(float64(time.Now().Unix()))
}

func SetHealthStatusMetric(version, gitCommit string, ts float64) {
HealthStatusMetric.WithLabelValues(
version,
gitCommit).Set(ts)
}

func SetActivePromotionMetric(atpComp *s2hv1beta1.ActivePromotion) {
atpStateList := map[ActivePromotionMetricState]float64{stateWaiting: 0, stateDeploying: 0, stateTesting: 0, statePromoting: 0, stateDestroying: 0}
atpState := atpComp.Status.State
Expand Down
10 changes: 0 additions & 10 deletions internal/samsahai/internal_process.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,16 +259,6 @@ func (c *controller) updateTeamDesiredComponent(updateInfo updateTeamDesiredComp
return err
}

// Add metric updateQueueMetric
//queue := &s2hv1beta1.Queue{}
//if err = c.client.Get(ctx, types.NamespacedName{
// Name: compName,
// Namespace: compNs}, queue); err != nil {
// logger.Error(err, "cannot get the queue")
//} else {
// exporter.SetQueueMetric(queue)
//}

return nil
}

Expand Down

0 comments on commit 4754eda

Please sign in to comment.