From 9fc6cc07e1ad08e105f3b023b9ae0bb28fca3cd4 Mon Sep 17 00:00:00 2001 From: Kislay Kishore Date: Wed, 21 Aug 2024 12:19:40 +0530 Subject: [PATCH] Add tests --- go.mod | 9 +- go.sum | 20 +-- internal/fs/wrappers/monitoring.go | 8 +- internal/fs/wrappers/monitoring_test.go | 171 ++++++++++++++++++++++++ 4 files changed, 192 insertions(+), 16 deletions(-) create mode 100644 internal/fs/wrappers/monitoring_test.go diff --git a/go.mod b/go.mod index 214692a445..a128b310f7 100644 --- a/go.mod +++ b/go.mod @@ -27,6 +27,9 @@ require ( github.com/stretchr/testify v1.9.0 github.com/urfave/cli v1.22.15 go.opencensus.io v0.24.0 + go.opentelemetry.io/otel v1.28.0 + go.opentelemetry.io/otel/sdk v1.28.0 + go.opentelemetry.io/otel/trace v1.28.0 golang.org/x/net v0.27.0 golang.org/x/oauth2 v0.22.0 golang.org/x/sync v0.8.0 @@ -63,7 +66,7 @@ require ( github.com/fsnotify/fsnotify v1.7.0 // indirect github.com/go-kit/log v0.2.1 // indirect github.com/go-logfmt/logfmt v0.5.1 // 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/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect github.com/golang/protobuf v1.5.4 // indirect @@ -98,9 +101,7 @@ require ( github.com/subosito/gotenv v1.6.0 // indirect go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.51.0 // indirect go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.51.0 // indirect - go.opentelemetry.io/otel v1.26.0 // indirect - go.opentelemetry.io/otel/metric v1.26.0 // indirect - go.opentelemetry.io/otel/trace v1.26.0 // indirect + go.opentelemetry.io/otel/metric v1.28.0 // indirect go.uber.org/multierr v1.11.0 // indirect golang.org/x/crypto v0.25.0 // indirect golang.org/x/exp v0.0.0-20240530194437-404ba88c7ed0 // indirect diff --git a/go.sum b/go.sum index d5e1cc5203..5ce7a097ba 100644 --- a/go.sum +++ b/go.sum @@ -453,8 +453,8 @@ github.com/go-logr/logr v1.2.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbV github.com/go-logr/logr v1.2.1/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= github.com/go-logr/logr v1.2.3/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= -github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ= -github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= +github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY= +github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= github.com/go-logr/stdr v1.2.0/go.mod h1:YkVgnZu1ZjjL7xTxrfm/LLZBfkhTqSR1ydtm6jTKKwI= github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= @@ -1221,8 +1221,8 @@ go.opentelemetry.io/otel v0.20.0/go.mod h1:Y3ugLH2oa81t5QO+Lty+zXf8zC9L26ax4Nzox go.opentelemetry.io/otel v1.3.0/go.mod h1:PWIKzi6JCp7sM0k9yZ43VX+T345uNbAkDKwHVjb2PTs= go.opentelemetry.io/otel v1.6.0/go.mod h1:bfJD2DZVw0LBxghOTlgnlI0CV3hLDu9XF/QKOUXMTQQ= go.opentelemetry.io/otel v1.6.1/go.mod h1:blzUabWHkX6LJewxvadmzafgh/wnvBSDBdOuwkAtrWQ= -go.opentelemetry.io/otel v1.26.0 h1:LQwgL5s/1W7YiiRwxf03QGnWLb2HW4pLiAhaA5cZXBs= -go.opentelemetry.io/otel v1.26.0/go.mod h1:UmLkJHUAidDval2EICqBMbnAd0/m2vmpf/dAM+fvFs4= +go.opentelemetry.io/otel v1.28.0 h1:/SqNcYk+idO0CxKEUOtKQClMK/MimZihKYMruSMViUo= +go.opentelemetry.io/otel v1.28.0/go.mod h1:q68ijF8Fc8CnMHKyzqL6akLO46ePnjkgfIMIjUIX9z4= go.opentelemetry.io/otel/exporters/otlp v0.20.0/go.mod h1:YIieizyaN77rtLJra0buKiNBOm9XQfkPEKBeuhoMwAM= go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.3.0/go.mod h1:VpP4/RMn8bv8gNo9uK7/IMY4mtWLELsS+JIP0inH0h4= go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.6.1/go.mod h1:NEu79Xo32iVb+0gVNV8PMd7GoWqnyDXRlj04yFjqz40= @@ -1234,22 +1234,22 @@ go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.3.0/go.mod h1 go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.6.1/go.mod h1:DAKwdo06hFLc0U88O10x4xnb5sc7dDRDqRuiN+io8JE= go.opentelemetry.io/otel/metric v0.20.0/go.mod h1:598I5tYlH1vzBjn+BTuhzTCSb/9debfNp6R3s7Pr1eU= go.opentelemetry.io/otel/metric v0.28.0/go.mod h1:TrzsfQAmQaB1PDcdhBauLMk7nyyg9hm+GoQq/ekE9Iw= -go.opentelemetry.io/otel/metric v1.26.0 h1:7S39CLuY5Jgg9CrnA9HHiEjGMF/X2VHvoXGgSllRz30= -go.opentelemetry.io/otel/metric v1.26.0/go.mod h1:SY+rHOI4cEawI9a7N1A4nIg/nTQXe1ccCNWYOJUrpX4= +go.opentelemetry.io/otel/metric v1.28.0 h1:f0HGvSl1KRAU1DLgLGFjrwVyismPlnuU6JD6bOeuA5Q= +go.opentelemetry.io/otel/metric v1.28.0/go.mod h1:Fb1eVBFZmLVTMb6PPohq3TO9IIhUisDsbJoL/+uQW4s= go.opentelemetry.io/otel/oteltest v0.20.0/go.mod h1:L7bgKf9ZB7qCwT9Up7i9/pn0PWIa9FqQ2IQ8LoxiGnw= go.opentelemetry.io/otel/sdk v0.20.0/go.mod h1:g/IcepuwNsoiX5Byy2nNV0ySUF1em498m7hBWC279Yc= go.opentelemetry.io/otel/sdk v1.3.0/go.mod h1:rIo4suHNhQwBIPg9axF8V9CA72Wz2mKF1teNrup8yzs= go.opentelemetry.io/otel/sdk v1.6.1/go.mod h1:IVYrddmFZ+eJqu2k38qD3WezFR2pymCzm8tdxyh3R4E= -go.opentelemetry.io/otel/sdk v1.24.0 h1:YMPPDNymmQN3ZgczicBY3B6sf9n62Dlj9pWD3ucgoDw= -go.opentelemetry.io/otel/sdk v1.24.0/go.mod h1:KVrIYw6tEubO9E96HQpcmpTKDVn9gdv35HoYiQWGDFg= +go.opentelemetry.io/otel/sdk v1.28.0 h1:b9d7hIry8yZsgtbmM0DKyPWMMUMlK9NEKuIG4aBqWyE= +go.opentelemetry.io/otel/sdk v1.28.0/go.mod h1:oYj7ClPUA7Iw3m+r7GeEjz0qckQRJK2B8zjcZEfu7Pg= go.opentelemetry.io/otel/sdk/export/metric v0.20.0/go.mod h1:h7RBNMsDJ5pmI1zExLi+bJK+Dr8NQCh0qGhm1KDnNlE= go.opentelemetry.io/otel/sdk/metric v0.20.0/go.mod h1:knxiS8Xd4E/N+ZqKmUPf3gTTZ4/0TjTXukfxjzSTpHE= go.opentelemetry.io/otel/trace v0.20.0/go.mod h1:6GjCW8zgDjwGHGa6GkyeB8+/5vjT16gUEi0Nf1iBdgw= go.opentelemetry.io/otel/trace v1.3.0/go.mod h1:c/VDhno8888bvQYmbYLqe41/Ldmr/KKunbvWM4/fEjk= go.opentelemetry.io/otel/trace v1.6.0/go.mod h1:qs7BrU5cZ8dXQHBGxHMOxwME/27YH2qEp4/+tZLLwJE= go.opentelemetry.io/otel/trace v1.6.1/go.mod h1:RkFRM1m0puWIq10oxImnGEduNBzxiN7TXluRBtE+5j0= -go.opentelemetry.io/otel/trace v1.26.0 h1:1ieeAUb4y0TE26jUFrCIXKpTuVK7uJGN9/Z/2LP5sQA= -go.opentelemetry.io/otel/trace v1.26.0/go.mod h1:4iDxvGDQuUkHve82hJJ8UqrwswHYsZuWCBllGV2U2y0= +go.opentelemetry.io/otel/trace v1.28.0 h1:GhQ9cUuQGmNDd5BTCP2dAvv75RdMxEfTmYejp+lkx9g= +go.opentelemetry.io/otel/trace v1.28.0/go.mod h1:jPyXzNPg6da9+38HEwElrQiHlVMTnVfM3/yv2OlIHaI= go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqeYNgFYFoEGnI= go.opentelemetry.io/proto/otlp v0.11.0/go.mod h1:QpEjXPrNQzrFDZgoTo49dgHR9RYRSrg3NAKnUGl9YpQ= go.opentelemetry.io/proto/otlp v0.12.1/go.mod h1:H7XAot3MsfNsj7EXtrA2q5xSNQ10UqI405h3+duxN4U= diff --git a/internal/fs/wrappers/monitoring.go b/internal/fs/wrappers/monitoring.go index de6ace3deb..8e9eb2ff06 100644 --- a/internal/fs/wrappers/monitoring.go +++ b/internal/fs/wrappers/monitoring.go @@ -31,9 +31,11 @@ import ( "go.opencensus.io/tag" "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/codes" + "go.opentelemetry.io/otel/trace" ) -const name = "github.com/googlecloudplatform/gcsfuse/fs" +// TODO: name is subject to change. +const name = "cloud.google.com/gcsfuse" var ( opsCount = stats.Int64("fs/ops_count", "The number of ops processed by the file system.", stats.UnitDimensionless) @@ -149,14 +151,16 @@ func (fs *monitoring) Destroy() { type wrappedCall func() error func invokeWrapped(ctx context.Context, opName string, w wrappedCall) error { - ctx, span := tracer.Start(ctx, opName) + ctx, span := tracer.Start(ctx, opName, trace.WithSpanKind(trace.SpanKindServer)) defer span.End() startTime := time.Now() err := w() + if err != nil { span.RecordError(err) span.SetStatus(codes.Error, err.Error()) } + recordOp(ctx, opName, startTime, err) return err } diff --git a/internal/fs/wrappers/monitoring_test.go b/internal/fs/wrappers/monitoring_test.go new file mode 100644 index 0000000000..56e692640d --- /dev/null +++ b/internal/fs/wrappers/monitoring_test.go @@ -0,0 +1,171 @@ +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package wrappers + +import ( + "context" + "testing" + + "github.com/jacobsa/fuse/fuseops" + "github.com/stretchr/testify/assert" + "go.opentelemetry.io/otel" + sdktrace "go.opentelemetry.io/otel/sdk/trace" + "go.opentelemetry.io/otel/sdk/trace/tracetest" + "go.opentelemetry.io/otel/trace" +) + +func newInMemoryExporter(t *testing.T) *tracetest.InMemoryExporter { + t.Helper() + ex := tracetest.NewInMemoryExporter() + t.Cleanup(func() { + ex.Reset() + }) + otel.SetTracerProvider(sdktrace.NewTracerProvider(sdktrace.WithSyncer(ex))) + return ex +} + +type dummyFS struct{} + +func (d dummyFS) StatFS(_ context.Context, _ *fuseops.StatFSOp) error { + return nil +} + +func (d dummyFS) LookUpInode(_ context.Context, _ *fuseops.LookUpInodeOp) error { + return nil +} + +func (d dummyFS) GetInodeAttributes(_ context.Context, _ *fuseops.GetInodeAttributesOp) error { + return nil +} + +func (d dummyFS) SetInodeAttributes(_ context.Context, _ *fuseops.SetInodeAttributesOp) error { + return nil +} + +func (d dummyFS) ForgetInode(_ context.Context, _ *fuseops.ForgetInodeOp) error { + return nil +} + +func (d dummyFS) BatchForget(_ context.Context, _ *fuseops.BatchForgetOp) error { + return nil +} + +func (d dummyFS) MkDir(_ context.Context, _ *fuseops.MkDirOp) error { + return nil +} + +func (d dummyFS) MkNode(_ context.Context, _ *fuseops.MkNodeOp) error { + return nil +} + +func (d dummyFS) CreateFile(_ context.Context, _ *fuseops.CreateFileOp) error { + return nil +} + +func (d dummyFS) CreateLink(_ context.Context, _ *fuseops.CreateLinkOp) error { + return nil +} + +func (d dummyFS) CreateSymlink(_ context.Context, _ *fuseops.CreateSymlinkOp) error { + return nil +} + +func (d dummyFS) Rename(_ context.Context, _ *fuseops.RenameOp) error { + return nil +} + +func (d dummyFS) RmDir(_ context.Context, _ *fuseops.RmDirOp) error { + return nil +} + +func (d dummyFS) Unlink(_ context.Context, _ *fuseops.UnlinkOp) error { + return nil +} + +func (d dummyFS) OpenDir(_ context.Context, _ *fuseops.OpenDirOp) error { + return nil +} + +func (d dummyFS) ReadDir(_ context.Context, _ *fuseops.ReadDirOp) error { + return nil +} + +func (d dummyFS) ReleaseDirHandle(_ context.Context, _ *fuseops.ReleaseDirHandleOp) error { + return nil +} + +func (d dummyFS) OpenFile(_ context.Context, _ *fuseops.OpenFileOp) error { + return nil +} + +func (d dummyFS) ReadFile(_ context.Context, _ *fuseops.ReadFileOp) error { + return nil +} + +func (d dummyFS) WriteFile(_ context.Context, _ *fuseops.WriteFileOp) error { + return nil +} + +func (d dummyFS) SyncFile(_ context.Context, _ *fuseops.SyncFileOp) error { + return nil +} + +func (d dummyFS) FlushFile(_ context.Context, _ *fuseops.FlushFileOp) error { + return nil +} + +func (d dummyFS) ReleaseFileHandle(_ context.Context, _ *fuseops.ReleaseFileHandleOp) error { + return nil +} + +func (d dummyFS) ReadSymlink(_ context.Context, _ *fuseops.ReadSymlinkOp) error { + return nil +} + +func (d dummyFS) RemoveXattr(_ context.Context, _ *fuseops.RemoveXattrOp) error { + return nil +} + +func (d dummyFS) GetXattr(_ context.Context, _ *fuseops.GetXattrOp) error { + return nil +} + +func (d dummyFS) ListXattr(_ context.Context, _ *fuseops.ListXattrOp) error { + return nil +} + +func (d dummyFS) SetXattr(_ context.Context, _ *fuseops.SetXattrOp) error { + return nil +} + +func (d dummyFS) Fallocate(_ context.Context, _ *fuseops.FallocateOp) error { + return nil +} + +func (d dummyFS) Destroy() {} + +func TestSpan(t *testing.T) { + ex := newInMemoryExporter(t) + m := monitoring{ + wrapped: dummyFS{}, + } + + m.StatFS(context.Background(), nil) + + ss := ex.GetSpans() + assert.Len(t, ss, 1) + assert.Equal(t, "StatFS", ss[0].Name) + assert.Equal(t, trace.SpanKindServer, ss[0].SpanKind) +}