-
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
184 additions
and
16 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,11 @@ package controllers | |
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"go.opentelemetry.io/otel/attribute" | ||
"go.opentelemetry.io/otel/metric" | ||
"go.opentelemetry.io/otel/metric/instrument" | ||
"go.opentelemetry.io/otel/trace" | ||
networkingv1 "k8s.io/api/networking/v1" | ||
k8serrors "k8s.io/apimachinery/pkg/api/errors" | ||
|
@@ -57,16 +60,36 @@ type RedirectReconciler struct { | |
scheme *runtime.Scheme | ||
log *logr.Logger | ||
tracer trace.Tracer | ||
|
||
redirectCount int | ||
redirects instrument.Int64UpDownCounter | ||
latency instrument.Int64Histogram | ||
} | ||
|
||
// NewRedirectReconciler returns a new RedirectReconciler | ||
func NewRedirectReconciler(client client.Client, rClient *redirectclient.RedirectClient, scheme *runtime.Scheme, log *logr.Logger, tracer trace.Tracer) *RedirectReconciler { | ||
func NewRedirectReconciler(client client.Client, rClient *redirectclient.RedirectClient, scheme *runtime.Scheme, log *logr.Logger, tracer trace.Tracer, meter metric.Meter) *RedirectReconciler { | ||
var redirects, _ = meter.Int64UpDownCounter( | ||
"urlshortener.active_redirects", | ||
instrument.WithUnit("count"), | ||
instrument.WithDescription("Amount of redirects (redirect one URL to another)"), | ||
) | ||
|
||
var redirectReconcileLatency, _ = meter.Int64Histogram( | ||
"urlshortener.redirect_controller.reconcile_latency", | ||
instrument.WithUnit("microseconds"), | ||
instrument.WithDescription("How long does the reconcile function run for"), | ||
) | ||
|
||
return &RedirectReconciler{ | ||
client: client, | ||
rClient: rClient, | ||
scheme: scheme, | ||
log: log, | ||
tracer: tracer, | ||
|
||
redirectCount: 0, | ||
redirects: redirects, | ||
latency: redirectReconcileLatency, | ||
} | ||
} | ||
|
||
|
@@ -82,15 +105,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() { | ||
r.latency.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))) | ||
|
||
r.redirects.Add(ctx, int64(len(redirectList.Items)-r.redirectCount)) | ||
r.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,11 @@ package controllers | |
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"go.opentelemetry.io/otel/attribute" | ||
"go.opentelemetry.io/otel/metric" | ||
"go.opentelemetry.io/otel/metric/instrument" | ||
"go.opentelemetry.io/otel/trace" | ||
|
||
"k8s.io/apimachinery/pkg/api/errors" | ||
|
@@ -61,15 +64,35 @@ type ShortLinkReconciler struct { | |
scheme *runtime.Scheme | ||
log *logr.Logger | ||
tracer trace.Tracer | ||
|
||
shortlinkCount int | ||
shortlinks instrument.Int64UpDownCounter | ||
latency instrument.Int64Histogram | ||
} | ||
|
||
// NewShortLinkReconciler returns a new ShortLinkReconciler | ||
func NewShortLinkReconciler(client *shortlinkclient.ShortlinkClient, scheme *runtime.Scheme, log *logr.Logger, tracer trace.Tracer) *ShortLinkReconciler { | ||
func NewShortLinkReconciler(client *shortlinkclient.ShortlinkClient, scheme *runtime.Scheme, log *logr.Logger, tracer trace.Tracer, meter metric.Meter) *ShortLinkReconciler { | ||
var shortlinks, _ = meter.Int64UpDownCounter( | ||
"urlshortener.active_shortlinks", | ||
instrument.WithUnit("count"), | ||
instrument.WithDescription("Amount of shortlinks (redirect a short-name to another URI)"), | ||
) | ||
|
||
var shortlinkReconcileLatency, _ = meter.Int64Histogram( | ||
"urlshortener.shortlink_controller.reconcile_latency", | ||
instrument.WithUnit("microseconds"), | ||
instrument.WithDescription("How long does the reconcile function run for"), | ||
) | ||
|
||
return &ShortLinkReconciler{ | ||
client: client, | ||
scheme: scheme, | ||
log: log, | ||
tracer: tracer, | ||
|
||
shortlinkCount: 0, | ||
shortlinks: shortlinks, | ||
latency: shortlinkReconcileLatency, | ||
} | ||
} | ||
|
||
|
@@ -82,24 +105,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() { | ||
r.latency.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 { | ||
r.shortlinks.Add(ctx, int64(len(shortlinkList.Items)-r.shortlinkCount)) | ||
r.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
Oops, something went wrong.