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 optional Opentracing #111

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions cloud/opentracing/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
Copyright 2022 The Kubernetes Authors.

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 opentracing

import (
"fmt"
crprometheus "github.com/prometheus/client_golang/prometheus"
"go.opentelemetry.io/otel/exporters/prometheus"
"go.opentelemetry.io/otel/metric/global"
"go.opentelemetry.io/otel/sdk/metric/aggregator/histogram"
controller "go.opentelemetry.io/otel/sdk/metric/controller/basic"
"go.opentelemetry.io/otel/sdk/metric/export/aggregation"
processor "go.opentelemetry.io/otel/sdk/metric/processor/basic"
selector "go.opentelemetry.io/otel/sdk/metric/selector/simple"
"sigs.k8s.io/controller-runtime/pkg/metrics"
)

// RegisterMetrics enables prometheus metrics for OpenTelemetry.
func RegisterMetrics() error {
fmt.Printf("Register metrics opentracing \n")
config := prometheus.Config{
Registry: metrics.Registry.(*crprometheus.Registry), // use the controller runtime metrics registry / gatherer
}
c := controller.New(
processor.NewFactory(
selector.NewWithHistogramDistribution(
histogram.WithExplicitBoundaries(config.DefaultHistogramBoundaries),
),
aggregation.CumulativeTemporalitySelector(),
processor.WithMemory(true),
),
)
exporter, err := prometheus.New(config, c)
if err != nil {
return err
}
global.SetMeterProvider(exporter.MeterProvider())

return nil
}
63 changes: 63 additions & 0 deletions cloud/opentracing/opentelemetry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
Copyright 2022 The Kubernetes Authors.

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 opentracing

import (
"context"
"fmt"
"github.com/Azure/go-autorest/tracing"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"go.opentelemetry.io/otel/trace"
"net/http"
)

type (
// OpenTelemetryAutorestTracer implements the tracing interface for AutoRest.
OpenTelemetryAutorestTracer struct {
tracer trace.Tracer
}
)

var _ tracing.Tracer = (*OpenTelemetryAutorestTracer)(nil)

// NewOpenTelemetryAutorestTracer creates a new Autorest tracing adapter for OpenTelemetry.
func NewOpenTelemetryAutorestTracer(tracer trace.Tracer) *OpenTelemetryAutorestTracer {
return &OpenTelemetryAutorestTracer{
tracer: tracer,
}
}

// NewTransport creates a new http.RoundTripper which will augment the base http.RoundTripper with OpenTelemetry
// tracing and metrics.
func (ot *OpenTelemetryAutorestTracer) NewTransport(base *http.Transport) http.RoundTripper {
return otelhttp.NewTransport(base)
}

// StartSpan creates a new span of a given name and adds it to the given context.Context.
func (ot *OpenTelemetryAutorestTracer) StartSpan(ctx context.Context, name string) context.Context {
fmt.Printf("Start Span opentracing")
ctx, _ = ot.tracer.Start(ctx, name)
return ctx
}

// EndSpan ends the current context span. It ignores the httpsStatusCode and error since they are recorded by
// the otelhttp.Transport.
func (ot *OpenTelemetryAutorestTracer) EndSpan(ctx context.Context, httpStatusCode int, err error) {
fmt.Printf("End Span opentracing")
span := trace.SpanFromContext(ctx)
span.End()
}
90 changes: 90 additions & 0 deletions cloud/opentracing/traces.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
Copyright 2022 The Kubernetes Authors.

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 opentracing

import (
"context"
"fmt"
"github.com/go-logr/logr"
"github.com/outscale-dev/cluster-api-provider-outscale.git/util/tele"
"github.com/outscale-dev/cluster-api-provider-outscale.git/util/tracing"
"github.com/pkg/errors"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/sdk/resource"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
"time"
)

// RegisterTracing enables code tracing via OpenTelemetry.
func RegisterTracing(ctx context.Context, log logr.Logger) error {
tp, err := otlpTracerProvider(ctx, "opentelemetry-collector:4317")
if err != nil {
return err
}
fmt.Println("Begin registering opentelemetry")
otel.SetTracerProvider(tp)
tracing.Register(NewOpenTelemetryAutorestTracer(tele.Tracer()))

go func() {
<-ctx.Done()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := tp.Shutdown(ctx); err != nil {
log.Error(err, "failed to shut down tracer provider")
}
}()

return nil
}

// otlpTracerProvider initializes an OTLP exporter and configures the corresponding tracer provider.
func otlpTracerProvider(ctx context.Context, url string) (*sdktrace.TracerProvider, error) {
res, err := resource.New(ctx,
resource.WithAttributes(
semconv.ServiceNameKey.String("capo"),
attribute.String("exporter", "otlp"),
),
)
fmt.Printf("Set Tracer Provider")

if err != nil {
return nil, errors.Wrap(err, "failed to create opentelemetry resource")
}

traceExporter, err := otlptracegrpc.New(ctx,
otlptracegrpc.WithInsecure(),
otlptracegrpc.WithEndpoint(url),
)
if err != nil {
return nil, errors.Wrap(err, "failed to create otlp trace exporter")
}

bsp := sdktrace.NewBatchSpanProcessor(traceExporter)
tracerProvider := sdktrace.NewTracerProvider(
sdktrace.WithSampler(sdktrace.AlwaysSample()),
sdktrace.WithResource(res),
sdktrace.WithSpanProcessor(bsp),
)
otel.SetTracerProvider(tracerProvider)
otel.SetTextMapPropagator(propagation.TraceContext{})

return tracerProvider, nil
}
2 changes: 1 addition & 1 deletion cloud/services/security/securitygroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ func (s *Service) GetSecurityGroupFromSecurityGroupRule(securityGroupId string,
} else if securityGroupMemberId != "" && ipRanges != "" {
return nil, errors.New("Get Both IpRange and securityGroupMemberId")
} else {
fmt.Printf("Have IpRange and no securityGroupMemberId\n")
fmt.Printf("Have IpRange and no securityGroupMemberId \n")
}

oscApiClient := s.scope.GetApi()
Expand Down
34 changes: 28 additions & 6 deletions controllers/osccluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,14 @@ import (
"context"
"fmt"
infrastructurev1beta1 "github.com/outscale-dev/cluster-api-provider-outscale.git/api/v1beta1"
"github.com/outscale-dev/cluster-api-provider-outscale.git/cloud/scope"
"github.com/outscale-dev/cluster-api-provider-outscale.git/cloud/services/net"
"github.com/outscale-dev/cluster-api-provider-outscale.git/cloud/services/security"
"github.com/outscale-dev/cluster-api-provider-outscale.git/cloud/services/service"
"github.com/outscale-dev/cluster-api-provider-outscale.git/util/reconciler"
"github.com/outscale-dev/cluster-api-provider-outscale.git/util/tele"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/client-go/tools/record"
"strings"
"time"

"github.com/outscale-dev/cluster-api-provider-outscale.git/cloud/scope"
"github.com/outscale-dev/cluster-api-provider-outscale.git/util/reconciler"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/util"
"sigs.k8s.io/cluster-api/util/annotations"
Expand All @@ -39,6 +37,8 @@ import (
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"strings"
"time"
)

// OscClusterReconciler reconciles a OscCluster object
Expand Down Expand Up @@ -100,6 +100,16 @@ func (r *OscClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request)
_ = log.FromContext(ctx)
ctx, cancel := context.WithTimeout(ctx, reconciler.DefaultedLoopTimeout(r.ReconcileTimeout))
defer cancel()

ctx, _, reconcileDone := tele.StartSpanWithLogger(
ctx,
"controllers.OscClusterReconciler.reconcile",
tele.KVP("namespace", req.Namespace),
tele.KVP("name", req.Name),
tele.KVP("kind", "OscCluster"),
)
defer reconcileDone()

log := ctrl.LoggerFrom(ctx)
oscCluster := &infrastructurev1beta1.OscCluster{}

Expand Down Expand Up @@ -181,6 +191,7 @@ func (r *OscClusterReconciler) reconcile(ctx context.Context, clusterScope *scop
if err := clusterScope.PatchObject(); err != nil {
return reconcile.Result{}, err
}

// Check that every element of the cluster spec has the good format (CIDR, Tag, ...)
netName, err := checkNetFormatParameters(clusterScope)
if err != nil {
Expand Down Expand Up @@ -290,6 +301,7 @@ func (r *OscClusterReconciler) reconcile(ctx context.Context, clusterScope *scop
clusterScope.Info("Set OscCluster status to not ready")
clusterScope.SetNotReady()
// Reconcile each element of the cluster

netSvc := r.getNetSvc(ctx, *clusterScope)
reconcileNet, err := reconcileNet(ctx, clusterScope, netSvc)
if err != nil {
Expand Down Expand Up @@ -363,7 +375,6 @@ func (r *OscClusterReconciler) reconcile(ctx context.Context, clusterScope *scop

loadBalancerSvc := r.getLoadBalancerSvc(ctx, *clusterScope)
_, err = reconcileLoadBalancer(ctx, clusterScope, loadBalancerSvc)

clusterScope.Info("Set OscCluster status to ready")
clusterScope.SetReady()
return reconcile.Result{}, nil
Expand All @@ -376,6 +387,15 @@ func (r *OscClusterReconciler) reconcileDelete(ctx context.Context, clusterScope

// reconcile deletion of each element of the cluster

ctx, _, reconcileDone := tele.StartSpanWithLogger(
ctx,
"controllers.OscClusterReconciler.reconcileDelete",
tele.KVP("namespace", clusterScope.GetNamespace()),
tele.KVP("name", clusterScope.GetName()),
tele.KVP("kind", "OscCluster"),
)
defer reconcileDone()

machines, _, err := clusterScope.ListMachines(ctx)
if err != nil {
return reconcile.Result{}, fmt.Errorf("failed to list machines for OscCluster %s/%s: %+v", err, clusterScope.GetNamespace(), clusterScope.GetName())
Expand Down Expand Up @@ -407,6 +427,7 @@ func (r *OscClusterReconciler) reconcileDelete(ctx context.Context, clusterScope
if err != nil {
return reconcileDeletePublicIp, err
}

routeTableSvc := r.getRouteTableSvc(ctx, *clusterScope)
reconcileDeleteRouteTable, err := reconcileDeleteRouteTable(ctx, clusterScope, routeTableSvc)
if err != nil {
Expand Down Expand Up @@ -436,6 +457,7 @@ func (r *OscClusterReconciler) reconcileDelete(ctx context.Context, clusterScope
if err != nil {
return reconcileDeleteNet, err
}

controllerutil.RemoveFinalizer(osccluster, "oscclusters.infrastructure.cluster.x-k8s.io")
return reconcile.Result{}, nil
}
Expand Down
Loading