Skip to content

Commit

Permalink
fix: external metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
Lifosmin Simon committed Sep 18, 2024
1 parent d2cfe3f commit 9bc9768
Show file tree
Hide file tree
Showing 12 changed files with 26 additions and 63 deletions.
6 changes: 3 additions & 3 deletions cli/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ import (
"fmt"

"github.com/MakeNowJust/heredoc"
"github.com/goto/guardian/config"
"github.com/goto/guardian/pkg/opentelemetry"
"github.com/goto/salt/cmdx"
"github.com/spf13/cobra"
)

var cliConfig *Config

type Config struct {
Host string `mapstructure:"host"`
Telemetry config.OpenTelemetryConfig `mapstructure:"telemetry"`
Host string `mapstructure:"host"`
Telemetry opentelemetry.Config `mapstructure:"telemetry"`
}

func LoadConfig() (*Config, error) {
Expand Down
13 changes: 5 additions & 8 deletions cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cli

import (
"context"
"log"

"github.com/MakeNowJust/heredoc"
handlerv1beta1 "github.com/goto/guardian/api/handler/v1beta1"
Expand Down Expand Up @@ -36,20 +37,16 @@ func New(cfg *Config) *cobra.Command {

if cliConfig.Telemetry.Enabled {
var err error
shutdownOtel, err = opentelemetry.Init(ctx, opentelemetry.Config{
ServiceName: cfg.Telemetry.ServiceName,
ServiceVersion: cfg.Telemetry.ServiceVersion,
SamplingFraction: cfg.Telemetry.SamplingFraction,
MetricInterval: cfg.Telemetry.MetricInterval,
CollectorAddr: cfg.Telemetry.OTLP.Endpoint,
})
shutdownOtel, err = opentelemetry.Init(ctx, cfg.Telemetry)
if err != nil {
return err
}
}

defer func() {
_ = shutdownOtel() // safely ignore the error if telemetry is disabled
if err := shutdownOtel(); err != nil {
log.Printf("telemetry is disabled: %v", err)
}
}()

return nil
Expand Down
10 changes: 5 additions & 5 deletions internal/server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"errors"
"fmt"

gConfig "github.com/goto/guardian/config"
"github.com/goto/guardian/internal/store"
"github.com/goto/guardian/jobs"
"github.com/goto/guardian/pkg/auth"
"github.com/goto/guardian/pkg/opentelemetry"
"github.com/goto/guardian/plugins/notifiers"
"github.com/goto/salt/config"
)
Expand Down Expand Up @@ -41,10 +41,10 @@ type Config struct {
LogLevel string `mapstructure:"log_level" default:"info"`
DB store.Config `mapstructure:"db"`
// Deprecated: use Auth.Default.HeaderKey instead note on the AuthenticatedUserHeaderKey
AuthenticatedUserHeaderKey string `mapstructure:"authenticated_user_header_key"`
AuditLogTraceIDHeaderKey string `mapstructure:"audit_log_trace_id_header_key" default:"X-Trace-Id"`
Jobs Jobs `mapstructure:"jobs"`
OpenTelemetry gConfig.OpenTelemetryConfig `mapstructure:"opentelemetry"`
AuthenticatedUserHeaderKey string `mapstructure:"authenticated_user_header_key"`
AuditLogTraceIDHeaderKey string `mapstructure:"audit_log_trace_id_header_key" default:"X-Trace-Id"`
Jobs Jobs `mapstructure:"jobs"`
Telemetry opentelemetry.Config `mapstructure:"telemetry"`

Auth Auth `mapstructure:"auth"`
}
Expand Down
12 changes: 3 additions & 9 deletions internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,9 @@ func RunServer(config *Config) error {
ctx := context.Background()

// var shutdownOtel = func() error { return nil }
if config.OpenTelemetry.Enabled {
if config.Telemetry.Enabled {
logger.Info(ctx, "open telemetry is initiating...")
shutdownOtel, err := opentelemetry.Init(ctx, opentelemetry.Config{
ServiceName: config.OpenTelemetry.ServiceName,
ServiceVersion: config.OpenTelemetry.ServiceVersion,
SamplingFraction: config.OpenTelemetry.SamplingFraction,
MetricInterval: config.OpenTelemetry.MetricInterval,
CollectorAddr: config.OpenTelemetry.OTLP.Endpoint,
})
shutdownOtel, err := opentelemetry.Init(ctx, config.Telemetry)
if err != nil {
return fmt.Errorf("error initiating open telemetry: %w", err)
}
Expand Down Expand Up @@ -240,7 +234,7 @@ func Migrate(c *Config) error {

func getStore(c *Config) (*postgres.Store, error) {
store, err := postgres.NewStore(&c.DB)
if c.OpenTelemetry.Enabled {
if c.Telemetry.Enabled {
sqlDB, err := store.DB().DB()
if err != nil {
return nil, err
Expand Down
4 changes: 2 additions & 2 deletions config/telemetry.go → pkg/opentelemetry/config.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package config
package opentelemetry

import "time"

type OpenTelemetryConfig struct {
type Config struct {
Enabled bool `mapstructure:"enabled" default:"false"`
ServiceName string `mapstructure:"service_name" default:"guardian"`
ServiceVersion string `mapstructure:"service_version"`
Expand Down
12 changes: 2 additions & 10 deletions pkg/opentelemetry/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,6 @@ import (

const gracePeriod = 5 * time.Second

type Config struct {
ServiceName string
ServiceVersion string
SamplingFraction int
MetricInterval time.Duration
CollectorAddr string
}

func Init(ctx context.Context, cfg Config) (func() error, error) {
res, err := resource.New(ctx,
resource.WithFromEnv(),
Expand Down Expand Up @@ -83,7 +75,7 @@ func Init(ctx context.Context, cfg Config) (func() error, error) {

func initGlobalMetrics(ctx context.Context, res *resource.Resource, cfg Config) (func() error, error) {
exporter, err := otlpmetricgrpc.New(ctx,
otlpmetricgrpc.WithEndpoint(cfg.CollectorAddr),
otlpmetricgrpc.WithEndpoint(cfg.OTLP.Endpoint),
otlpmetricgrpc.WithCompressor(gzip.Name),
otlpmetricgrpc.WithInsecure(),
)
Expand All @@ -108,7 +100,7 @@ func initGlobalMetrics(ctx context.Context, res *resource.Resource, cfg Config)
func initGlobalTracer(ctx context.Context, res *resource.Resource, cfg Config) (func() error, error) {
exporter, err := otlptrace.New(ctx, otlptracegrpc.NewClient(
otlptracegrpc.WithInsecure(),
otlptracegrpc.WithEndpoint(cfg.CollectorAddr),
otlptracegrpc.WithEndpoint(cfg.OTLP.Endpoint),
otlptracegrpc.WithCompressor(gzip.Name),
))
if err != nil {
Expand Down
9 changes: 2 additions & 7 deletions plugins/providers/bigquery/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

bq "cloud.google.com/go/bigquery"
"github.com/goto/guardian/domain"
"github.com/goto/guardian/pkg/opentelemetry"
bqApi "google.golang.org/api/bigquery/v2"
"google.golang.org/api/cloudresourcemanager/v1"
"google.golang.org/api/iam/v1"
Expand Down Expand Up @@ -36,16 +35,12 @@ func NewBigQueryClient(projectID string, opts ...option.ClientOption) (*bigQuery
return nil, err
}

iamOpt := option.WithHTTPClient(opentelemetry.NewHttpClient("IAMClient"))
iamOpts := append(opts, iamOpt)
iamService, err := iam.NewService(ctx, iamOpts...)
iamService, err := iam.NewService(ctx, opts...)
if err != nil {
return nil, err
}

crmOpt := option.WithHTTPClient(opentelemetry.NewHttpClient("CloudResourceManagerClient"))
crmOpts := append(opts, crmOpt)
crmService, err := cloudresourcemanager.NewService(ctx, crmOpts...)
crmService, err := cloudresourcemanager.NewService(ctx, opts...)
if err != nil {
return nil, err
}
Expand Down
4 changes: 0 additions & 4 deletions plugins/providers/dataplex/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ import (

datacatalog "cloud.google.com/go/datacatalog/apiv1"
"github.com/goto/guardian/domain"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
"google.golang.org/api/iterator"
"google.golang.org/api/option"
pb "google.golang.org/genproto/googleapis/cloud/datacatalog/v1"
iampb "google.golang.org/genproto/googleapis/iam/v1"
"google.golang.org/grpc"
)

type policyTagClient struct {
Expand All @@ -25,8 +23,6 @@ func newPolicyTagClient(projectID, location string, credentialsJSON []byte) (*po
ctx := context.Background()
opts := []option.ClientOption{
option.WithCredentialsJSON(credentialsJSON),
option.WithGRPCDialOption(grpc.WithUnaryInterceptor(otelgrpc.UnaryClientInterceptor())),
option.WithGRPCDialOption(grpc.WithStreamInterceptor(otelgrpc.StreamClientInterceptor())),
}
policyManager, err := datacatalog.NewPolicyTagManagerClient(ctx, opts...)
if err != nil {
Expand Down
2 changes: 0 additions & 2 deletions plugins/providers/gate/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
pv "github.com/goto/guardian/core/provider"
"github.com/goto/guardian/domain"
"github.com/goto/guardian/pkg/gate"
"github.com/goto/guardian/pkg/opentelemetry"
"github.com/goto/guardian/utils"
"github.com/mitchellh/mapstructure"
)
Expand Down Expand Up @@ -281,7 +280,6 @@ func (p *provider) getClient(pc *domain.ProviderConfig) (*gate.Client, error) {
opts := []gate.ClientOption{
gate.WithAPIKey(creds.APIKey),
gate.WithQueryParamAuthMethod(),
gate.WithHTTPClient(opentelemetry.NewHttpClient("PolicyTagManagerClient")),
}

client, err := gate.NewClient(creds.Host, opts...)
Expand Down
9 changes: 2 additions & 7 deletions plugins/providers/gcloudiam/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"strings"

"github.com/goto/guardian/domain"
"github.com/goto/guardian/pkg/opentelemetry"
"google.golang.org/api/cloudresourcemanager/v1"
"google.golang.org/api/iam/v1"
"google.golang.org/api/option"
Expand All @@ -26,16 +25,12 @@ type iamClient struct {
func newIamClient(credentialsJSON []byte, resourceName string) (*iamClient, error) {
ctx := context.Background()

crmClient := opentelemetry.NewHttpClient("CloudResourceManagerClient")

cloudResourceManagerService, err := cloudresourcemanager.NewService(ctx, option.WithCredentialsJSON(credentialsJSON), option.WithHTTPClient(crmClient))
cloudResourceManagerService, err := cloudresourcemanager.NewService(ctx, option.WithCredentialsJSON(credentialsJSON))
if err != nil {
return nil, err
}

iamHTTPClient := opentelemetry.NewHttpClient("IAMClient")

iamService, err := iam.NewService(ctx, option.WithCredentialsJSON(credentialsJSON), option.WithHTTPClient(iamHTTPClient))
iamService, err := iam.NewService(ctx, option.WithCredentialsJSON(credentialsJSON))
if err != nil {
return nil, err
}
Expand Down
4 changes: 1 addition & 3 deletions plugins/providers/gcs/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"cloud.google.com/go/iam"
"cloud.google.com/go/storage"
"github.com/goto/guardian/domain"
"github.com/goto/guardian/pkg/opentelemetry"
"github.com/goto/guardian/utils"
"golang.org/x/sync/errgroup"
"google.golang.org/api/iterator"
Expand All @@ -21,8 +20,7 @@ type gcsClient struct {
}

func newGCSClient(ctx context.Context, projectID string, credentialsJSON []byte) (*gcsClient, error) {
httpClient := opentelemetry.NewHttpClient("MetabaseHttpClient")
client, err := storage.NewClient(ctx, option.WithCredentialsJSON(credentialsJSON), option.WithHTTPClient(httpClient))
client, err := storage.NewClient(ctx, option.WithCredentialsJSON(credentialsJSON))
if err != nil {
return nil, err
}
Expand Down
4 changes: 1 addition & 3 deletions plugins/providers/gitlab/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
pv "github.com/goto/guardian/core/provider"
"github.com/goto/guardian/domain"
"github.com/goto/guardian/pkg/log"
"github.com/goto/guardian/pkg/opentelemetry"
"github.com/goto/guardian/utils"
"github.com/xanzy/go-gitlab"
"golang.org/x/sync/errgroup"
Expand Down Expand Up @@ -272,9 +271,8 @@ func (p *provider) getClient(pc domain.ProviderConfig) (*gitlab.Client, error) {
return nil, fmt.Errorf("unable to decrypt credentials: %w", err)
}

httpClient := opentelemetry.NewHttpClient("GitlabClient")
// add trace for otel instrumentation
client, err := gitlab.NewClient(creds.AccessToken, gitlab.WithBaseURL(creds.Host), gitlab.WithHTTPClient(httpClient))
client, err := gitlab.NewClient(creds.AccessToken, gitlab.WithBaseURL(creds.Host))
if err != nil {
return nil, fmt.Errorf("unable to create gitlab client: %w", err)
}
Expand Down

0 comments on commit 9bc9768

Please sign in to comment.