Skip to content

Commit

Permalink
Merge pull request #470 from kube-tarian/opentelemtry
Browse files Browse the repository at this point in the history
Instrumented open telemetry
  • Loading branch information
vramk23 authored May 18, 2024
2 parents 156537e + 8fa5b0e commit ab4353d
Show file tree
Hide file tree
Showing 16 changed files with 335 additions and 43 deletions.
3 changes: 3 additions & 0 deletions capten/agent/internal/api/store_cred_apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/kube-tarian/kad/capten/common-pkg/agentpb"
"github.com/kube-tarian/kad/capten/common-pkg/k8s"
vaultcred "github.com/kube-tarian/kad/capten/common-pkg/vault-cred"

v1 "k8s.io/api/core/v1"

"github.com/kube-tarian/kad/capten/common-pkg/credential"
Expand All @@ -20,6 +21,8 @@ var (
)

func (a *Agent) StoreCredential(ctx context.Context, request *agentpb.StoreCredentialRequest) (*agentpb.StoreCredentialResponse, error) {


credPath := fmt.Sprintf("%s/%s/%s", request.CredentialType, request.CredEntityName, request.CredIdentifier)
credAdmin, err := credentials.NewCredentialAdmin(ctx)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions capten/common-pkg/plugins/argocd/clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ func (a *ArgoCDClient) DeleteCluster(ctx context.Context, clusterURL string) (*c
}

func (a *ArgoCDClient) GetCluster(ctx context.Context, clusterURL string) (*v1alpha1.Cluster, error) {

conn, appClient, err := a.client.NewClusterClient()
if err != nil {
return nil, err
Expand Down
58 changes: 54 additions & 4 deletions server/cmd/server/main.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package main

import (
"context"
"fmt"
"net"
"os"
"os/signal"
"syscall"

"github.com/kube-tarian/kad/server/pkg/opentelemetry"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"

Expand All @@ -30,7 +33,15 @@ func main() {
if err != nil {
log.Fatal("failed to load service congfig", err)
}

cleanup, err := opentelemetry.InitTracer()
if err != nil {
log.Errorf("unable to set the open telemetry , error: %v", err)
}
defer func() {
if cleanup != nil {
cleanup(context.Background())
}
}()
err = iamclient.RegisterService(log)
if err != nil {
log.Fatalf("%v", err)
Expand Down Expand Up @@ -85,11 +96,29 @@ func main() {
}

var grpcServer *grpc.Server
if cfg.AuthEnabled {
log.Info("Server Authentication enabled")
if (cfg.OptelEnabled) && (cfg.AuthEnabled) {

log.Info("Server Authentication and opentelemetry is enabled")

interceptor := CombineInterceptors(rpcServer.AuthInterceptor, otelgrpc.UnaryServerInterceptor())

grpcServer = grpc.NewServer(grpc.UnaryInterceptor(interceptor))

} else if cfg.OptelEnabled {

log.Info("Opentelemetry is enabled and Server Authentication disabled")

grpcServer = grpc.NewServer(grpc.UnaryInterceptor(otelgrpc.UnaryServerInterceptor()))

} else if cfg.AuthEnabled {

log.Info("Server Authentication enabled and opentelemetry disabled")

grpcServer = grpc.NewServer(grpc.UnaryInterceptor(rpcServer.AuthInterceptor))
} else {
log.Info("Server Authentication disabled")

log.Info("Server Authentication and opentelemetry instrumentation disabled")

grpcServer = grpc.NewServer()
}

Expand All @@ -110,3 +139,24 @@ func main() {
<-signals
log.Info("interrupt received, exiting")
}

// CombineInterceptors combines multiple unary interceptors into one
func CombineInterceptors(interceptors ...grpc.UnaryServerInterceptor) grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
// Chain the interceptors
chained := handler
for i := len(interceptors) - 1; i >= 0; i-- {
chained = chainedInterceptor(interceptors[i], chained)
}
// Call the combined interceptors
return chained(ctx, req)
}
}

// chainedInterceptor chains two unary interceptors together
func chainedInterceptor(a grpc.UnaryServerInterceptor, b grpc.UnaryHandler) grpc.UnaryHandler {
return func(ctx context.Context, req interface{}) (interface{}, error) {
// Call the first interceptor, passing the next handler as the next interceptor
return a(ctx, req, nil, b)
}
}
35 changes: 25 additions & 10 deletions server/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,28 @@ go 1.21
require (
github.com/go-git/go-git/v5 v5.11.0
github.com/gocql/gocql v1.3.1
github.com/google/uuid v1.6.0
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.0.1
github.com/intelops/go-common v1.0.20
github.com/kelseyhightower/envconfig v1.4.0
github.com/ory/client-go v1.1.49
github.com/pkg/errors v0.9.1
github.com/stargate/stargate-grpc-go-client v0.0.0-20220822130422-9a1c6261d4fa
golang.org/x/oauth2 v0.14.0
google.golang.org/grpc v1.59.0
google.golang.org/protobuf v1.31.0
golang.org/x/oauth2 v0.17.0
google.golang.org/grpc v1.63.2
google.golang.org/protobuf v1.33.0
gopkg.in/yaml.v2 v2.4.0
)

require github.com/stretchr/testify v1.9.0 // indirect

require (
dario.cat/mergo v1.0.0 // indirect
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20230828082145-3c4c8a2d2371 // indirect
github.com/cenkalti/backoff/v3 v3.0.0 // indirect
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/cloudflare/circl v1.3.3 // indirect
github.com/containerd/containerd v1.7.13 // indirect
github.com/cyphar/filepath-securejoin v0.2.4 // indirect
Expand All @@ -34,10 +38,12 @@ require (
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
github.com/go-git/go-billy/v5 v5.5.0 // indirect
github.com/go-jose/go-jose/v3 v3.0.0 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/uuid v1.4.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.1 // indirect
github.com/hailocab/go-hostpool v0.0.0-20160125115350-e80d13ce29ed // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
Expand Down Expand Up @@ -70,15 +76,24 @@ require (
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/skeema/knownhosts v1.2.1 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
golang.org/x/crypto v0.17.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.51.0 // indirect
go.opentelemetry.io/otel v1.26.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.26.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.26.0 // indirect
go.opentelemetry.io/otel/metric v1.26.0 // indirect
go.opentelemetry.io/otel/sdk v1.26.0 // indirect
go.opentelemetry.io/otel/trace v1.26.0 // indirect
go.opentelemetry.io/proto/otlp v1.2.0 // indirect
golang.org/x/crypto v0.22.0 // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/net v0.24.0 // indirect
golang.org/x/sys v0.19.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.3.0 // indirect
golang.org/x/tools v0.13.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231030173426-d783a09b4405 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240227224415-6ceb2ff114de // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240401170217-c3f982113cda // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
)
Loading

0 comments on commit ab4353d

Please sign in to comment.