Skip to content
Open
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
32 changes: 18 additions & 14 deletions internal/cmd/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,19 +199,19 @@ func NewGRPCServer(
return tracingProvider.Shutdown(ctx)
})

if cfg.Tracing.Enabled {
exp, traceExpShutdown, err := tracing.GetExporter(ctx, &cfg.Tracing)
if err != nil {
return nil, fmt.Errorf("creating tracing exporter: %w", err)
}

server.onShutdown(traceExpShutdown)
if cfg.Tracing.Enabled || cfg.Audit.Enabled() {
if cfg.Tracing.Enabled {
exp, traceExpShutdown, err := tracing.GetExporter(ctx, &cfg.Tracing)
if err != nil {
return nil, fmt.Errorf("creating tracing exporter: %w", err)
}

tracingProvider.RegisterSpanProcessor(tracesdk.NewBatchSpanProcessor(exp, tracesdk.WithBatchTimeout(1*time.Second)))
server.onShutdown(traceExpShutdown)
tracingProvider.RegisterSpanProcessor(tracesdk.NewBatchSpanProcessor(exp, tracesdk.WithBatchTimeout(1*time.Second)))
logger.Debug("otel tracing enabled", zap.String("exporter", cfg.Tracing.Exporter.String()))
}

ipch = ipch.WithServerStatsHandler(otelgrpc.NewServerHandler())

logger.Debug("otel tracing enabled", zap.String("exporter", cfg.Tracing.Exporter.String()))
}

// base inteceptors
Expand Down Expand Up @@ -332,7 +332,8 @@ func NewGRPCServer(
tracingProvider.RegisterSpanProcessor(
tracesdk.NewBatchSpanProcessor(
analyticsExporter,
tracesdk.WithBatchTimeout(cfg.Analytics.Buffer.FlushPeriod)),
tracesdk.WithBatchTimeout(cfg.Analytics.Buffer.FlushPeriod),
),
)
server.onShutdown(func(ctx context.Context) error {
return analyticsExporter.Shutdown(ctx)
Expand Down Expand Up @@ -365,8 +366,10 @@ func NewGRPCServer(
grpc_zap.ReplaceGrpcLoggerV2(logger.WithOptions(zap.IncreaseLevel(grpcLogLevel)))

// add auth interceptors to the server
interceptors = append(interceptors,
append(authInterceptors,
interceptors = append(
interceptors,
append(
authInterceptors,
middlewaregrpc.FliptAcceptServerVersionUnaryInterceptor(logger),
middlewaregrpc.EvaluationUnaryInterceptor(cfg.Analytics.Enabled()),
)...,
Expand Down Expand Up @@ -436,7 +439,8 @@ func NewGRPCServer(

tracingProvider.RegisterSpanProcessor(tracesdk.NewBatchSpanProcessor(spanExporter, tracesdk.WithBatchTimeout(cfg.Audit.Buffer.FlushPeriod), tracesdk.WithMaxExportBatchSize(cfg.Audit.Buffer.Capacity)))

logger.Debug("audit sinks enabled",
logger.Debug(
"audit sinks enabled",
zap.Stringers("sinks", sinks),
zap.Int("buffer capacity", cfg.Audit.Buffer.Capacity),
zap.String("flush period", cfg.Audit.Buffer.FlushPeriod.String()),
Expand Down
70 changes: 70 additions & 0 deletions internal/cmd/grpc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package cmd

import (
"os"
"path/filepath"
"runtime"
"testing"

"github.com/fullstorydev/grpchan/inprocgrpc"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.flipt.io/flipt/internal/config"
"go.flipt.io/flipt/internal/info"
flipt "go.flipt.io/flipt/rpc/flipt"
"go.uber.org/zap/zaptest"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)

func TestAuditLogsRecordedWhenTracingDisabled(t *testing.T) {
tmpDir := t.TempDir()

cfg := config.Default()
cfg.Database.URL = "file:" + filepath.ToSlash(filepath.Join(tmpDir, "flipt.db"))
cfg.Audit.Sinks.Log.Enabled = true
cfg.Audit.Sinks.Log.File = filepath.Join(tmpDir, "audit.log")
cfg.Server.GRPCPort = 0

logger := zaptest.NewLogger(t)
ctx := t.Context()

ipch := &inprocgrpc.Channel{}

server, err := NewGRPCServer(ctx, logger, cfg, ipch, info.Flipt{Version: t.Name()}, true)
require.NoError(t, err)

startServerChan := make(chan struct{})
go func() {
close(startServerChan)
err := server.Run()
assert.NoError(t, err)
}()

runtime.Gosched()
<-startServerChan

conn, err := grpc.NewClient(
server.ln.Addr().String(),
grpc.WithTransportCredentials(insecure.NewCredentials()),
)
require.NoError(t, err)
t.Cleanup(func() {
_ = conn.Close()
})

client := flipt.NewFliptClient(conn)
_, err = client.CreateFlag(ctx, &flipt.CreateFlagRequest{
Key: "test-flag",
Name: "Test Flag",
Type: flipt.FlagType_VARIANT_FLAG_TYPE,
})
require.NoError(t, err)

_ = server.Shutdown(ctx)

data, err := os.ReadFile(cfg.Audit.Sinks.Log.File)
require.NoError(t, err)
assert.Contains(t, string(data), `"type": "flag"`)
assert.Contains(t, string(data), `"action": "created"`)
}
Loading