-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
168 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,8 +18,10 @@ package controllers | |
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"go.opentelemetry.io/otel/attribute" | ||
"go.opentelemetry.io/otel/metric/instrument" | ||
"go.opentelemetry.io/otel/trace" | ||
networkingv1 "k8s.io/api/networking/v1" | ||
k8serrors "k8s.io/apimachinery/pkg/api/errors" | ||
|
@@ -38,6 +40,20 @@ import ( | |
"github.com/prometheus/client_golang/prometheus" | ||
) | ||
|
||
var redirects, _ = observability.Meter.Int64UpDownCounter( | ||
"urlshortener.active_redirects", | ||
instrument.WithUnit("count"), | ||
instrument.WithDescription("Amount of redirects (redirect one URL to another)"), | ||
) | ||
|
||
var redirectCount int | ||
|
||
var redirectReconcileLatency, _ = observability.Meter.Int64Histogram( | ||
"urlshortener.redirect_controller.reconcile_latency", | ||
instrument.WithUnit("microseconds"), | ||
instrument.WithDescription("How long does the reconcile function run for"), | ||
) | ||
|
||
var activeRedirects = prometheus.NewGauge( | ||
prometheus.GaugeOpts{ | ||
Name: "urlshortener_active_redirects", | ||
|
@@ -82,15 +98,31 @@ func NewRedirectReconciler(client client.Client, rClient *redirectclient.Redirec | |
// | ||
// For more details, check Reconcile and its Result here: | ||
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/reconcile | ||
func (r *RedirectReconciler) Reconcile(c context.Context, req ctrl.Request) (ctrl.Result, error) { | ||
ctx, span := r.tracer.Start(c, "RedirectReconciler.Reconcile", trace.WithAttributes(attribute.String("redirect", req.Name))) | ||
defer span.End() | ||
func (r *RedirectReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { | ||
startTime := time.Now() | ||
|
||
defer func() { | ||
redirectReconcileLatency.Record(ctx, time.Since(startTime).Microseconds(), attribute.String("redirect", req.NamespacedName.String())) | ||
}() | ||
|
||
log := r.log.WithName("reconciler").WithValues("redirect", req.NamespacedName) | ||
|
||
span := trace.SpanFromContext(ctx) | ||
|
||
// Check if the span was sampled and is recording the data | ||
if !span.IsRecording() { | ||
ctx, span = r.tracer.Start(ctx, "RedirectReconciler.Reconcile") | ||
defer span.End() | ||
} | ||
|
||
span.SetAttributes(attribute.String("redirect", req.Name)) | ||
|
||
// Monitor the number of redirects | ||
if redirectList, err := r.rClient.List(ctx); redirectList != nil && err == nil { | ||
activeRedirects.Set(float64(len(redirectList.Items))) | ||
|
||
redirects.Add(ctx, int64(len(redirectList.Items)-redirectCount)) | ||
redirectCount = len(redirectList.Items) | ||
} | ||
|
||
// get Redirect from etcd | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,8 +18,10 @@ package controllers | |
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"go.opentelemetry.io/otel/attribute" | ||
"go.opentelemetry.io/otel/metric/instrument" | ||
"go.opentelemetry.io/otel/trace" | ||
|
||
"k8s.io/apimachinery/pkg/api/errors" | ||
|
@@ -35,6 +37,20 @@ import ( | |
"github.com/prometheus/client_golang/prometheus" | ||
) | ||
|
||
var shortlinks, _ = observability.Meter.Int64UpDownCounter( | ||
"urlshortener.active_shortlinks", | ||
instrument.WithUnit("count"), | ||
instrument.WithDescription("Amount of shortlinks (redirect a short-name to another URI)"), | ||
) | ||
|
||
var shortlinkCount int | ||
|
||
var shortlinkReconcileLatency, _ = observability.Meter.Int64Histogram( | ||
"urlshortener.shortlink_controller.reconcile_latency", | ||
instrument.WithUnit("microseconds"), | ||
instrument.WithDescription("How long does the reconcile function run for"), | ||
) | ||
|
||
var activeShortlinks = prometheus.NewGauge( | ||
prometheus.GaugeOpts{ | ||
Name: "urlshortener_active_shortlinks", | ||
|
@@ -82,24 +98,39 @@ func NewShortLinkReconciler(client *shortlinkclient.ShortlinkClient, scheme *run | |
// | ||
// For more details, check Reconcile and its Result here: | ||
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/reconcile | ||
func (r *ShortLinkReconciler) Reconcile(c context.Context, req ctrl.Request) (ctrl.Result, error) { | ||
ctx, span := r.tracer.Start(c, "ShortLinkReconciler.Reconcile", trace.WithAttributes(attribute.String("shortlink", req.Name))) | ||
defer span.End() | ||
func (r *ShortLinkReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { | ||
startTime := time.Now() | ||
|
||
defer func() { | ||
shortlinkReconcileLatency.Record(ctx, time.Since(startTime).Microseconds(), attribute.String("shortlink", req.NamespacedName.String())) | ||
}() | ||
|
||
log := r.log.WithName("reconciler").WithValues("shortlink", req.NamespacedName.String()) | ||
|
||
span := trace.SpanFromContext(ctx) | ||
|
||
// Check if the span was sampled and is recording the data | ||
if !span.IsRecording() { | ||
ctx, span = r.tracer.Start(ctx, "ShortLinkReconciler.Reconcile") | ||
defer span.End() | ||
} | ||
|
||
span.SetAttributes(attribute.String("shortlink", req.NamespacedName.String())) | ||
|
||
// Get ShortLink from etcd | ||
shortlink, err := r.client.GetNamespaced(ctx, req.NamespacedName) | ||
if err != nil || shortlink == nil { | ||
if errors.IsNotFound(err) { | ||
activeShortlinks.Dec() | ||
observability.RecordInfo(span, &log, "Shortlink resource not found. Ignoring since object must be deleted") | ||
} else { | ||
observability.RecordError(span, &log, err, "Failed to fetch ShortLink resource") | ||
} | ||
} | ||
|
||
if shortlinkList, err := r.client.ListNamespaced(ctx, req.Namespace); shortlinkList != nil && err == nil { | ||
shortlinks.Add(ctx, int64(len(shortlinkList.Items)-shortlinkCount)) | ||
shortlinkCount = len(shortlinkList.Items) | ||
|
||
activeShortlinks.Set(float64(len(shortlinkList.Items))) | ||
|
||
for _, shortlink := range shortlinkList.Items { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters