Skip to content

Commit

Permalink
feat: add extra info to span via default decorators
Browse files Browse the repository at this point in the history
Signed-off-by: Mateusz Urbanek <[email protected]>
  • Loading branch information
shanduur-akamai committed Jul 29, 2024
1 parent 4914dae commit 5c13b82
Show file tree
Hide file tree
Showing 15 changed files with 463 additions and 54 deletions.
1 change: 1 addition & 0 deletions api/v1alpha1/webhook_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var (
// defaultLinodeClient is an unauthenticated Linode client
defaultLinodeClient = linodeclient.NewLinodeClientWithTracing(
ptr.To(linodego.NewClient(&http.Client{Timeout: defaultClientTimeout})),
linodeclient.DefaultDecorator(),
)
)

Expand Down
1 change: 1 addition & 0 deletions api/v1alpha2/webhook_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ var (
// defaultLinodeClient is an unauthenticated Linode client
defaultLinodeClient = linodeclient.NewLinodeClientWithTracing(
ptr.To(linodego.NewClient(&http.Client{Timeout: defaultClientTimeout})),
linodeclient.DefaultDecorator(),
)
)

Expand Down
1 change: 1 addition & 0 deletions cloud/scope/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func CreateLinodeClient(apiKey string, timeout time.Duration, opts ...Option) (L

return linodeclient.NewLinodeClientWithTracing(
&linodeClient,
linodeclient.DefaultDecorator(),
), nil
}

Expand Down
4 changes: 2 additions & 2 deletions controller/linodecluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ func (r *LinodeClusterReconciler) SetupWithManager(mgr ctrl.Manager, options crc
kutil.ClusterToInfrastructureMapFunc(context.TODO(), infrav1alpha2.GroupVersion.WithKind("LinodeCluster"), mgr.GetClient(), &infrav1alpha2.LinodeCluster{}),
),
builder.WithPredicates(predicates.ClusterUnpausedAndInfrastructureReady(mgr.GetLogger())),
).Complete(wrappedruntimereconciler.NewRuntimeReconcilerWithTracing(r))
).Complete(wrappedruntimereconciler.NewRuntimeReconcilerWithTracing(r, wrappedruntimereconciler.DefaultDecorator()))
if err != nil {
return fmt.Errorf("failed to build controller: %w", err)
}
Expand All @@ -311,5 +311,5 @@ func (r *LinodeClusterReconciler) SetupWithManager(mgr ctrl.Manager, options crc
}

func (r *LinodeClusterReconciler) TracedClient() client.Client {
return wrappedruntimeclient.NewRuntimeClientWithTracing(r.Client)
return wrappedruntimeclient.NewRuntimeClientWithTracing(r.Client, wrappedruntimereconciler.DefaultDecorator())
}
4 changes: 2 additions & 2 deletions controller/linodemachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,7 @@ func (r *LinodeMachineReconciler) SetupWithManager(mgr ctrl.Manager, options crc
builder.WithPredicates(predicates.ClusterUnpausedAndInfrastructureReady(mgr.GetLogger())),
).
WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(mgr.GetLogger(), r.WatchFilterValue)).
Complete(wrappedruntimereconciler.NewRuntimeReconcilerWithTracing(r))
Complete(wrappedruntimereconciler.NewRuntimeReconcilerWithTracing(r, wrappedruntimereconciler.DefaultDecorator()))
if err != nil {
return fmt.Errorf("failed to build controller: %w", err)
}
Expand All @@ -777,5 +777,5 @@ func (r *LinodeMachineReconciler) SetupWithManager(mgr ctrl.Manager, options crc
}

func (r *LinodeMachineReconciler) TracedClient() client.Client {
return wrappedruntimeclient.NewRuntimeClientWithTracing(r.Client)
return wrappedruntimeclient.NewRuntimeClientWithTracing(r.Client, wrappedruntimeclient.DefaultDecorator())
}
4 changes: 2 additions & 2 deletions controller/linodeobjectstoragebucket_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ func (r *LinodeObjectStorageBucketReconciler) SetupWithManager(mgr ctrl.Manager,
&clusterv1.Cluster{},
handler.EnqueueRequestsFromMapFunc(linodeObjectStorageBucketMapper),
builder.WithPredicates(predicates.ClusterUnpausedAndInfrastructureReady(mgr.GetLogger())),
).Complete(wrappedruntimereconciler.NewRuntimeReconcilerWithTracing(r))
).Complete(wrappedruntimereconciler.NewRuntimeReconcilerWithTracing(r, wrappedruntimereconciler.DefaultDecorator()))
if err != nil {
return fmt.Errorf("failed to build controller: %w", err)
}
Expand All @@ -291,5 +291,5 @@ func (r *LinodeObjectStorageBucketReconciler) SetupWithManager(mgr ctrl.Manager,
}

func (r *LinodeObjectStorageBucketReconciler) TracedClient() client.Client {
return wrappedruntimeclient.NewRuntimeClientWithTracing(r.Client)
return wrappedruntimeclient.NewRuntimeClientWithTracing(r.Client, wrappedruntimeclient.DefaultDecorator())
}
4 changes: 2 additions & 2 deletions controller/linodevpc_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ func (r *LinodeVPCReconciler) SetupWithManager(mgr ctrl.Manager, options crcontr
&clusterv1.Cluster{},
handler.EnqueueRequestsFromMapFunc(linodeVPCMapper),
builder.WithPredicates(predicates.ClusterUnpausedAndInfrastructureReady(mgr.GetLogger())),
).Complete(wrappedruntimereconciler.NewRuntimeReconcilerWithTracing(r))
).Complete(wrappedruntimereconciler.NewRuntimeReconcilerWithTracing(r, wrappedruntimereconciler.DefaultDecorator()))
if err != nil {
return fmt.Errorf("failed to build controller: %w", err)
}
Expand All @@ -357,5 +357,5 @@ func (r *LinodeVPCReconciler) SetupWithManager(mgr ctrl.Manager, options crcontr
}

func (r *LinodeVPCReconciler) TracedClient() client.Client {
return wrappedruntimeclient.NewRuntimeClientWithTracing(r.Client)
return wrappedruntimeclient.NewRuntimeClientWithTracing(r.Client, wrappedruntimeclient.DefaultDecorator())
}
4 changes: 3 additions & 1 deletion hack/templates/opentelemetry.go.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ func (_d {{$decorator}}) {{$method.Declaration}} {
defer func() {
if _d._spanDecorator != nil {
_d._spanDecorator(_span, {{$method.ParamsMap}}, {{$method.ResultsMap}})
}{{- if $method.ReturnsError}} else if err != nil {
}
{{if $method.ReturnsError}}
if err != nil {
_span.RecordError(err)
_span.SetAttributes(
attribute.String("event", "error"),
Expand Down
39 changes: 39 additions & 0 deletions observability/wrappers/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
Copyright 2023 Akamai Technologies, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package wrappers

func GetValue[T any](params map[string]any, key string) (T, bool) {
var zero T

if val, ok := params[key]; ok {
if val, ok := val.(T); ok {
return val, true
}
}

return zero, false
}

func Optional[T any](val *T) T {
var zero T

if val != nil {
return *val
}

return zero
}
157 changes: 157 additions & 0 deletions observability/wrappers/linodeclient/decorator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
/*
Copyright 2024 Akamai Technologies, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package linodeclient

import (
"github.com/linode/linodego"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"

"github.com/linode/cluster-api-provider-linode/observability/wrappers"
)

const (
NodebalancerIDParam = "nodebalancerID"
ConfigIDParam = "configID"
DomainIDParam = "domainID"
DiskIDParam = "diskID"
NodeIDParam = "nodeID"
DomainRecordIDParam = "domainRecordID"
LinodeIDParam = "linodeID"
KeyIDParam = "keyID"
VpcIDParam = "vpcID"
ImageIDParam = "imageID"
ClusterParam = "cluster"
LabelParam = "label"
RegionIDParam = "regionID"
TypeIDParam = "typeID"
SizeParam = "size"
RecordReqParam = "recordReq"
)

func DefaultDecorator() func(span trace.Span, params, results map[string]interface{}) { //nolint:cyclop,gocognit // TODO: refactor this
return func(span trace.Span, params, results map[string]interface{}) {
attr := []attribute.KeyValue{}

if val, ok := wrappers.GetValue[int](params, NodebalancerIDParam); ok {
attr = append(attr,
attribute.Int("req.nodebalancer_id", val),
)
}

if val, ok := wrappers.GetValue[int](params, ConfigIDParam); ok {
attr = append(attr,
attribute.Int("req.config_id", val),
)
}

if val, ok := wrappers.GetValue[int](params, DomainIDParam); ok {
attr = append(attr,
attribute.Int("req.domain_id", val),
)
}

if val, ok := wrappers.GetValue[int](params, DiskIDParam); ok {
attr = append(attr,
attribute.Int("req.disk_id", val),
)
}

if val, ok := wrappers.GetValue[int](params, NodeIDParam); ok {
attr = append(attr,
attribute.Int("req.node_id", val),
)
}

if val, ok := wrappers.GetValue[int](params, DomainRecordIDParam); ok {
attr = append(attr,
attribute.Int("req.domain_record_id", val),
)
}

if val, ok := wrappers.GetValue[int](params, LinodeIDParam); ok {
attr = append(attr,
attribute.Int("req.linode_id", val),
)
}

if val, ok := wrappers.GetValue[int](params, KeyIDParam); ok {
attr = append(attr,
attribute.Int("req.key_id", val),
)
}

if val, ok := wrappers.GetValue[int](params, VpcIDParam); ok {
attr = append(attr,
attribute.Int("req.vpc_id", val),
)
}

if val, ok := wrappers.GetValue[int](params, SizeParam); ok {
attr = append(attr,
attribute.Int("req.size", val),
)
}

if val, ok := wrappers.GetValue[string](params, ImageIDParam); ok {
attr = append(attr,
attribute.String("req.image_id", val),
)
}

if val, ok := wrappers.GetValue[string](params, ClusterParam); ok {
attr = append(attr,
attribute.String("req.cluster", val),
)
}

if val, ok := wrappers.GetValue[string](params, LabelParam); ok {
attr = append(attr,
attribute.String("req.label", val),
)
}

if val, ok := wrappers.GetValue[string](params, RegionIDParam); ok {
attr = append(attr,
attribute.String("req.region_id", val),
)
}

if val, ok := wrappers.GetValue[string](params, TypeIDParam); ok {
attr = append(attr,
attribute.String("req.type_id", val),
)
}

if val, ok := wrappers.GetValue[linodego.DomainRecordUpdateOptions](params, RecordReqParam); ok {
attr = append(attr,
attribute.String("req.domain_record_update_options.name", val.Name),
attribute.String("req.domain_record_update_options.target", val.Target),
attribute.String("req.domain_record_update_options.type", string(val.Type)),
attribute.String("req.domain_record_update_options.protocol", wrappers.Optional(val.Protocol)),
attribute.String("req.domain_record_update_options.service", wrappers.Optional(val.Service)),
attribute.String("req.domain_record_update_options.tag", wrappers.Optional(val.Tag)),
attribute.Int("req.domain_record_update_options.ttl_sec", val.TTLSec),
attribute.Int("req.domain_record_update_options.port", wrappers.Optional(val.Port)),
attribute.Int("req.domain_record_update_options.priority", wrappers.Optional(val.Priority)),
attribute.Int("req.domain_record_update_options.weight", wrappers.Optional(val.Weight)),
)
}

span.SetAttributes(attr...)
}
}
Loading

0 comments on commit 5c13b82

Please sign in to comment.