Skip to content

Commit

Permalink
Add tracing support via open telemetry
Browse files Browse the repository at this point in the history
  • Loading branch information
jirwin committed Nov 18, 2024
1 parent fa7db72 commit 2602822
Show file tree
Hide file tree
Showing 50 changed files with 723 additions and 84 deletions.
44 changes: 27 additions & 17 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module github.com/conductorone/baton-sdk

go 1.21
go 1.22.7

toolchain go1.22.9

require (
filippo.io/age v1.1.1
Expand All @@ -14,7 +16,7 @@ require (
github.com/aws/smithy-go v1.20.2
github.com/deckarep/golang-set/v2 v2.6.0
github.com/doug-martin/goqu/v9 v9.19.0
github.com/envoyproxy/protoc-gen-validate v1.0.4
github.com/envoyproxy/protoc-gen-validate v1.1.0
github.com/glebarez/go-sqlite v1.22.0
github.com/go-jose/go-jose/v3 v3.0.3
github.com/golang/protobuf v1.5.4
Expand All @@ -28,20 +30,24 @@ require (
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.18.2
github.com/stretchr/testify v1.9.0
go.opentelemetry.io/otel v1.27.0
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.57.0
go.opentelemetry.io/otel v1.32.0
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.32.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.32.0
go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v1.27.0
go.opentelemetry.io/otel/metric v1.27.0
go.opentelemetry.io/otel/sdk/metric v1.27.0
go.opentelemetry.io/otel/metric v1.32.0
go.opentelemetry.io/otel/sdk v1.32.0
go.opentelemetry.io/otel/sdk/metric v1.32.0
go.uber.org/ratelimit v0.3.1
go.uber.org/zap v1.27.0
golang.org/x/crypto v0.24.0
golang.org/x/net v0.26.0
golang.org/x/oauth2 v0.20.0
golang.org/x/sync v0.7.0
golang.org/x/sys v0.21.0
google.golang.org/genproto/googleapis/rpc v0.0.0-20240506185236-b8a5c65736ae
google.golang.org/grpc v1.63.2
google.golang.org/protobuf v1.34.1
golang.org/x/crypto v0.28.0
golang.org/x/net v0.30.0
golang.org/x/oauth2 v0.23.0
golang.org/x/sync v0.9.0
golang.org/x/sys v0.27.0
google.golang.org/genproto/googleapis/rpc v0.0.0-20241104194629-dd2ea8efbc28
google.golang.org/grpc v1.68.0
google.golang.org/protobuf v1.35.1
gopkg.in/square/go-jose.v2 v2.6.0
gopkg.in/yaml.v2 v2.4.0
)
Expand All @@ -61,13 +67,15 @@ require (
github.com/aws/aws-sdk-go-v2/service/sso v1.20.5 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.23.4 // indirect
github.com/benbjohnson/clock v1.3.5 // indirect
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-ole/go-ole v1.3.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.23.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
Expand All @@ -90,14 +98,16 @@ require (
github.com/tklauser/go-sysconf v0.3.14 // indirect
github.com/tklauser/numcpus v0.8.0 // indirect
github.com/yusufpapurcu/wmi v1.2.4 // indirect
go.opentelemetry.io/otel/sdk v1.27.0 // indirect
go.opentelemetry.io/otel/trace v1.27.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.32.0 // indirect
go.opentelemetry.io/otel/trace v1.32.0 // indirect
go.opentelemetry.io/proto/otlp v1.3.1 // indirect
go.uber.org/atomic v1.11.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 // indirect
golang.org/x/mod v0.18.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/text v0.20.0 // indirect
golang.org/x/tools v0.22.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20241104194629-dd2ea8efbc28 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
modernc.org/libc v1.50.5 // indirect
Expand Down
92 changes: 54 additions & 38 deletions go.sum

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions internal/connector/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (

grpc_zap "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap"
"github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
"go.opentelemetry.io/otel/propagation"
"go.uber.org/zap"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
Expand Down Expand Up @@ -152,6 +154,14 @@ func (cw *wrapper) Run(ctx context.Context, serverCfg *connectorwrapperV1.Server
grpc.Creds(credentials.NewTLS(tlsConfig)),
grpc.ChainUnaryInterceptor(ugrpc.UnaryServerInterceptor(ctx)...),
grpc.ChainStreamInterceptor(ugrpc.StreamServerInterceptors(ctx)...),
grpc.StatsHandler(otelgrpc.NewServerHandler(
otelgrpc.WithPropagators(
propagation.NewCompositeTextMapPropagator(
propagation.TraceContext{},
propagation.Baggage{},
),
),
)),
)
connectorV2.RegisterConnectorServiceServer(server, cw.server)
connectorV2.RegisterGrantsServiceServer(server, cw.server)
Expand Down Expand Up @@ -311,6 +321,14 @@ func (cw *wrapper) C(ctx context.Context) (types.ConnectorClient, error) {
grpc.WithTransportCredentials(credentials.NewTLS(clientTLSConfig)),
grpc.WithBlock(),
grpc.WithChainUnaryInterceptor(ratelimit2.UnaryInterceptor(cw.now, cw.rlDescriptors...)),
grpc.WithStatsHandler(otelgrpc.NewClientHandler(
otelgrpc.WithPropagators(
propagation.NewCompositeTextMapPropagator(
propagation.TraceContext{},
propagation.Baggage{},
),
),
)),
)
if err != nil {
dialErr = err
Expand Down
32 changes: 25 additions & 7 deletions pkg/cli/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,22 @@ import (
"fmt"
"os"

"github.com/conductorone/baton-sdk/internal/connector"
v2 "github.com/conductorone/baton-sdk/pb/c1/connector/v2"
v1 "github.com/conductorone/baton-sdk/pb/c1/connector_wrapper/v1"
"github.com/conductorone/baton-sdk/pkg/connectorrunner"
"github.com/conductorone/baton-sdk/pkg/field"
"github.com/conductorone/baton-sdk/pkg/logging"
"github.com/conductorone/baton-sdk/pkg/types"
"github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.uber.org/zap"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/anypb"

"github.com/conductorone/baton-sdk/internal/connector"
v2 "github.com/conductorone/baton-sdk/pb/c1/connector/v2"
v1 "github.com/conductorone/baton-sdk/pb/c1/connector_wrapper/v1"
"github.com/conductorone/baton-sdk/pkg/connectorrunner"
"github.com/conductorone/baton-sdk/pkg/field"
"github.com/conductorone/baton-sdk/pkg/logging"
"github.com/conductorone/baton-sdk/pkg/types"
"github.com/conductorone/baton-sdk/pkg/uotel"
)

type GetConnectorFunc func(context.Context, *viper.Viper) (types.ConnectorServer, error)
Expand Down Expand Up @@ -53,6 +55,12 @@ func MakeMainCommand(
return err
}

otelShutdown, err := uotel.InitOtel(context.Background(), v.GetString("otel-collector-endpoint"), name)
if err != nil {
return err
}
defer otelShutdown(context.Background())

Check failure on line 62 in pkg/cli/commands.go

View workflow job for this annotation

GitHub Actions / go-lint

Error return value is not checked (errcheck)

l := ctxzap.Extract(runCtx)

if isService() {
Expand Down Expand Up @@ -210,6 +218,16 @@ func MakeGRPCServerCommand(
return err
}

otelShutdown, err := uotel.InitOtel(
context.Background(),
v.GetString("otel-collector-endpoint"),
fmt.Sprintf("%s-grpc", name),
)
if err != nil {
return err
}
defer otelShutdown(context.Background())

Check failure on line 229 in pkg/cli/commands.go

View workflow job for this annotation

GitHub Actions / go-lint

Error return value is not checked (errcheck)

// validate required fields and relationship constraints
if err := field.Validate(confschema, v); err != nil {
return err
Expand Down
3 changes: 2 additions & 1 deletion pkg/cli/service_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ package cli
import (
"context"

"github.com/spf13/cobra"

"github.com/conductorone/baton-sdk/pkg/field"
"github.com/conductorone/baton-sdk/pkg/logging"
"github.com/spf13/cobra"
)

func isService() bool {
Expand Down
10 changes: 6 additions & 4 deletions pkg/cli/service_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,18 @@ import (
"strconv"
"time"

"github.com/conductorone/baton-sdk/pkg/field"
"github.com/conductorone/baton-sdk/pkg/logging"
"github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap"
"github.com/spf13/cobra"
"go.uber.org/zap"
"golang.org/x/sys/windows/svc"
"golang.org/x/sys/windows/svc/debug"
"golang.org/x/sys/windows/svc/eventlog"
"golang.org/x/sys/windows/svc/mgr"
"gopkg.in/yaml.v2"

"github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap"
"github.com/spf13/cobra"

"github.com/conductorone/baton-sdk/pkg/field"
"github.com/conductorone/baton-sdk/pkg/logging"
)

const (
Expand Down
Loading

0 comments on commit 2602822

Please sign in to comment.