Skip to content

Commit

Permalink
Merge pull request #272 from Surax98/otel
Browse files Browse the repository at this point in the history
first otel implementation on VK only
  • Loading branch information
dciangot authored Aug 6, 2024
2 parents e3eb044 + 627c41c commit eb192e6
Show file tree
Hide file tree
Showing 3 changed files with 258 additions and 114 deletions.
51 changes: 32 additions & 19 deletions cmd/virtual-kubelet/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ package main
import (
"context"
"crypto/tls"
"crypto/x509"
"flag"
"fmt"
"io/ioutil"
"net"
"os"
"path"
Expand All @@ -28,7 +30,7 @@ import (

// "k8s.io/client-go/rest"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/credentials"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/client-go/kubernetes/scheme"
Expand Down Expand Up @@ -106,13 +108,6 @@ func initProvider() (func(context.Context) error, error) {
return nil, fmt.Errorf("failed to create resource: %w", err)
}

// TODO: disable is telemetry is disabled

// If the OpenTelemetry Collector is running on a local cluster (minikube or
// microk8s), it should be accessible through the NodePort service at the
// `localhost:30080` endpoint. Otherwise, replace `localhost` with the
// endpoint of your cluster. If you run the app inside k8s, then you can
// probably connect directly to the service through dns.
ctx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()

Expand All @@ -122,11 +117,35 @@ func initProvider() (func(context.Context) error, error) {
otlpEndpoint = "localhost:4317"
}

conn, err := grpc.DialContext(ctx, otlpEndpoint,
// Note the use of insecure transport here. TLS is recommended in production.
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithBlock(),
)
fmt.Println("TELEMETRY_ENDPOINT: ", otlpEndpoint)

crtFilePath := os.Getenv("TELEMETRY_CRTFILEPATH")

conn := &grpc.ClientConn{}

if crtFilePath != "" {
cert, err := ioutil.ReadFile(crtFilePath)
if err != nil {
return nil, fmt.Errorf("failed to create resource: %w", err)
}

roots := x509.NewCertPool()
if !roots.AppendCertsFromPEM(cert) {
return nil, fmt.Errorf("failed to create resource: %w", err)
}

creds := credentials.NewTLS(&tls.Config{
RootCAs: roots,
})

conn, err = grpc.DialContext(ctx, otlpEndpoint,
grpc.WithTransportCredentials(creds),
grpc.WithBlock(),
)
} else {
conn, err = grpc.DialContext(ctx, otlpEndpoint, grpc.WithInsecure(), grpc.WithBlock())
}

if err != nil {
return nil, fmt.Errorf("failed to create gRPC connection to collector: %w", err)
}
Expand All @@ -150,15 +169,9 @@ func initProvider() (func(context.Context) error, error) {
// set global propagator to tracecontext (the default is no-op).
otel.SetTextMapPropagator(propagation.TraceContext{})

// Shutdown will flush any remaining spans and shut down the exporter.
return tracerProvider.Shutdown, nil
}

func tlsConfig(tls *tls.Config) error {
tls.InsecureSkipVerify = true
return nil
}

func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand Down
Loading

0 comments on commit eb192e6

Please sign in to comment.