Skip to content

Commit

Permalink
Trace fs operations (#2436)
Browse files Browse the repository at this point in the history
* Enable tracing of fs operations.
  • Loading branch information
kislaykishore authored Sep 9, 2024
1 parent bd70467 commit b9e53cf
Show file tree
Hide file tree
Showing 16 changed files with 252 additions and 98 deletions.
24 changes: 24 additions & 0 deletions cfg/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,10 @@ type MetricsConfig struct {

type MonitoringConfig struct {
ExperimentalOpentelemetryCollectorAddress string `yaml:"experimental-opentelemetry-collector-address,omitempty" json:"experimental-opentelemetry-collector-address,omitempty"`

ExperimentalTracingMode string `yaml:"experimental-tracing-mode,omitempty" json:"experimental-tracing-mode,omitempty"`

ExperimentalTracingSamplingRatio float64 `yaml:"experimental-tracing-sampling-ratio,omitempty" json:"experimental-tracing-sampling-ratio,omitempty"`
}

type WriteConfig struct {
Expand Down Expand Up @@ -415,6 +419,26 @@ func BindFlags(v *viper.Viper, flagSet *pflag.FlagSet) error {
return err
}

flagSet.StringP("experimental-tracing-mode", "", "", "Experimental: specify tracing mode")

if err := flagSet.MarkHidden("experimental-tracing-mode"); err != nil {
return err
}

if err := v.BindPFlag("monitoring.experimental-tracing-mode", flagSet.Lookup("experimental-tracing-mode")); err != nil {
return err
}

flagSet.Float64P("experimental-tracing-sampling-ratio", "", 0, "Experimental: Trace sampling ratio")

if err := flagSet.MarkHidden("experimental-tracing-sampling-ratio"); err != nil {
return err
}

if err := v.BindPFlag("monitoring.experimental-tracing-sampling-ratio", flagSet.Lookup("experimental-tracing-sampling-ratio")); err != nil {
return err
}

flagSet.IntP("file-cache-max-size-mb", "", -1, "Maximum size of the file-cache in MiBs")

if err := v.BindPFlag("file-cache.max-size-mb", flagSet.Lookup("file-cache-max-size-mb")); err != nil {
Expand Down
14 changes: 14 additions & 0 deletions cfg/params.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,20 @@
deprecated: true
deprecation-warning: "Experimental flag: could be dropped even in a minor release."

- config-path: "monitoring.experimental-tracing-mode"
flag-name: "experimental-tracing-mode"
type: "string"
usage: "Experimental: specify tracing mode"
default: ""
hide-flag: true

- config-path: "monitoring.experimental-tracing-sampling-ratio"
flag-name: "experimental-tracing-sampling-ratio"
type: "float64"
usage: "Experimental: Trace sampling ratio"
default: 0
hide-flag: true

- config-path: "only-dir"
flag-name: "only-dir"
type: "string"
Expand Down
3 changes: 2 additions & 1 deletion cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"time"

"github.com/googlecloudplatform/gcsfuse/v2/cfg"
"github.com/googlecloudplatform/gcsfuse/v2/common"
"github.com/googlecloudplatform/gcsfuse/v2/internal/config"
"github.com/googlecloudplatform/gcsfuse/v2/internal/logger"
"github.com/googlecloudplatform/gcsfuse/v2/internal/mount"
Expand Down Expand Up @@ -69,7 +70,7 @@ func newApp() (app *cli.App) {

app = &cli.App{
Name: "gcsfuse",
Version: getVersion(),
Version: common.GetVersion(),
Usage: "Mount a specified GCS bucket or all accessible buckets locally",
Writer: os.Stderr,
Flags: []cli.Flag{
Expand Down
23 changes: 14 additions & 9 deletions cmd/legacy_main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"strings"

"github.com/googlecloudplatform/gcsfuse/v2/cfg"
"github.com/googlecloudplatform/gcsfuse/v2/common"
"github.com/googlecloudplatform/gcsfuse/v2/internal/canned"
"github.com/googlecloudplatform/gcsfuse/v2/internal/config"
"github.com/googlecloudplatform/gcsfuse/v2/internal/locker"
Expand Down Expand Up @@ -79,12 +80,12 @@ func registerSIGINTHandler(mountPoint string) {
func getUserAgent(appName string, config string) string {
gcsfuseMetadataImageType := os.Getenv("GCSFUSE_METADATA_IMAGE_TYPE")
if len(gcsfuseMetadataImageType) > 0 {
userAgent := fmt.Sprintf("gcsfuse/%s %s (GPN:gcsfuse-%s) (Cfg:%s)", getVersion(), appName, gcsfuseMetadataImageType, config)
userAgent := fmt.Sprintf("gcsfuse/%s %s (GPN:gcsfuse-%s) (Cfg:%s)", common.GetVersion(), appName, gcsfuseMetadataImageType, config)
return strings.Join(strings.Fields(userAgent), " ")
} else if len(appName) > 0 {
return fmt.Sprintf("gcsfuse/%s (GPN:gcsfuse-%s) (Cfg:%s)", getVersion(), appName, config)
return fmt.Sprintf("gcsfuse/%s (GPN:gcsfuse-%s) (Cfg:%s)", common.GetVersion(), appName, config)
} else {
return fmt.Sprintf("gcsfuse/%s (GPN:gcsfuse) (Cfg:%s)", getVersion(), config)
return fmt.Sprintf("gcsfuse/%s (GPN:gcsfuse) (Cfg:%s)", common.GetVersion(), config)
}
}

Expand Down Expand Up @@ -129,10 +130,7 @@ func createStorageHandle(newConfig *cfg.Config, userAgent string) (storageHandle
////////////////////////////////////////////////////////////////////////

// Mount the file system according to arguments in the supplied context.
func mountWithArgs(
bucketName string,
mountPoint string,
newConfig *cfg.Config) (mfs *fuse.MountedFileSystem, err error) {
func mountWithArgs(bucketName string, mountPoint string, newConfig *cfg.Config) (mfs *fuse.MountedFileSystem, err error) {
// Enable invariant checking if requested.
if newConfig.Debug.ExitOnInvariantViolation {
locker.EnableInvariantsCheck()
Expand Down Expand Up @@ -275,7 +273,7 @@ func Mount(newConfig *cfg.Config, bucketName, mountPoint string) (err error) {
}
}

logger.Infof("Start gcsfuse/%s for app %q using mount point: %s\n", getVersion(), newConfig.AppName, mountPoint)
logger.Infof("Start gcsfuse/%s for app %q using mount point: %s\n", common.GetVersion(), newConfig.AppName, mountPoint)

// Log mount-config and the CLI flags in the log-file.
// If there is no log-file, then log these to stdout.
Expand Down Expand Up @@ -381,6 +379,8 @@ func Mount(newConfig *cfg.Config, bucketName, mountPoint string) (err error) {
_ = monitor.EnableStackdriverExporter(newConfig.Metrics.StackdriverExportInterval)
_ = monitor.EnableOpenTelemetryCollectorExporter(newConfig.Monitoring.ExperimentalOpentelemetryCollectorAddress)
_ = monitor.EnablePrometheusCollectorExporter(int(newConfig.Metrics.PrometheusPort))
ctx := context.Background()
shutdownFn := monitor.SetupTracing(ctx, newConfig)

// Mount, writing information about our progress to the writer that package
// daemonize gives us and telling it about the outcome.
Expand Down Expand Up @@ -438,11 +438,16 @@ func Mount(newConfig *cfg.Config, bucketName, mountPoint string) (err error) {
registerSIGINTHandler(mfs.Dir())

// Wait for the file system to be unmounted.
err = mfs.Join(context.Background())
err = mfs.Join(ctx)

monitor.CloseStackdriverExporter()
monitor.CloseOpenTelemetryCollectorExporter()
monitor.ClosePrometheusCollectorExporter()
if shutdownFn != nil {
if shutdownErr := shutdownFn(ctx); shutdownErr != nil {
logger.Errorf("Error while shutting down trace exporter: %v", shutdownErr)
}
}

if err != nil {
err = fmt.Errorf("MountedFileSystem.Join: %w", err)
Expand Down
17 changes: 9 additions & 8 deletions cmd/legacy_main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"testing"

"github.com/googlecloudplatform/gcsfuse/v2/cfg"
"github.com/googlecloudplatform/gcsfuse/v2/common"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)
Expand Down Expand Up @@ -68,23 +69,23 @@ func (t *MainTest) TestGetUserAgentWhenMetadataImageTypeEnvVarIsSet() {

mountConfig := &cfg.Config{}
userAgent := getUserAgent("AppName", getConfigForUserAgent(mountConfig))
expectedUserAgent := strings.TrimSpace(fmt.Sprintf("gcsfuse/%s AppName (GPN:gcsfuse-DLVM) (Cfg:0:0)", getVersion()))
expectedUserAgent := strings.TrimSpace(fmt.Sprintf("gcsfuse/%s AppName (GPN:gcsfuse-DLVM) (Cfg:0:0)", common.GetVersion()))

assert.Equal(t.T(), expectedUserAgent, userAgent)
}

func (t *MainTest) TestGetUserAgentWhenMetadataImageTypeEnvVarIsNotSet() {
mountConfig := &cfg.Config{}
userAgent := getUserAgent("AppName", getConfigForUserAgent(mountConfig))
expectedUserAgent := strings.TrimSpace(fmt.Sprintf("gcsfuse/%s (GPN:gcsfuse-AppName) (Cfg:0:0)", getVersion()))
expectedUserAgent := strings.TrimSpace(fmt.Sprintf("gcsfuse/%s (GPN:gcsfuse-AppName) (Cfg:0:0)", common.GetVersion()))

assert.Equal(t.T(), expectedUserAgent, userAgent)
}

func (t *MainTest) TestGetUserAgentConfigWithNoFileCache() {
mountConfig := &cfg.Config{}
userAgent := getUserAgent("AppName", getConfigForUserAgent(mountConfig))
expectedUserAgent := strings.TrimSpace(fmt.Sprintf("gcsfuse/%s (GPN:gcsfuse-AppName) (Cfg:0:0)", getVersion()))
expectedUserAgent := strings.TrimSpace(fmt.Sprintf("gcsfuse/%s (GPN:gcsfuse-AppName) (Cfg:0:0)", common.GetVersion()))
assert.Equal(t.T(), expectedUserAgent, userAgent)
}

Expand All @@ -97,7 +98,7 @@ func (t *MainTest) TestGetUserAgentConfigWithFileCacheEnabledRandomReadEnabled()
},
}
userAgent := getUserAgent("AppName", getConfigForUserAgent(mountConfig))
expectedUserAgent := strings.TrimSpace(fmt.Sprintf("gcsfuse/%s (GPN:gcsfuse-AppName) (Cfg:1:1)", getVersion()))
expectedUserAgent := strings.TrimSpace(fmt.Sprintf("gcsfuse/%s (GPN:gcsfuse-AppName) (Cfg:1:1)", common.GetVersion()))
assert.Equal(t.T(), expectedUserAgent, userAgent)
}

Expand All @@ -110,7 +111,7 @@ func (t *MainTest) TestGetUserAgentConfigWithFileCacheEnabledRandomDisabled() {
},
}
userAgent := getUserAgent("AppName", getConfigForUserAgent(mountConfig))
expectedUserAgent := strings.TrimSpace(fmt.Sprintf("gcsfuse/%s (GPN:gcsfuse-AppName) (Cfg:1:0)", getVersion()))
expectedUserAgent := strings.TrimSpace(fmt.Sprintf("gcsfuse/%s (GPN:gcsfuse-AppName) (Cfg:1:0)", common.GetVersion()))
assert.Equal(t.T(), expectedUserAgent, userAgent)
}
func (t *MainTest) TestGetUserAgentConfigWithFileCacheSizeSetCacheDirNotSet() {
Expand All @@ -121,7 +122,7 @@ func (t *MainTest) TestGetUserAgentConfigWithFileCacheSizeSetCacheDirNotSet() {
},
}
userAgent := getUserAgent("AppName", getConfigForUserAgent(mountConfig))
expectedUserAgent := strings.TrimSpace(fmt.Sprintf("gcsfuse/%s (GPN:gcsfuse-AppName) (Cfg:0:0)", getVersion()))
expectedUserAgent := strings.TrimSpace(fmt.Sprintf("gcsfuse/%s (GPN:gcsfuse-AppName) (Cfg:0:0)", common.GetVersion()))
assert.Equal(t.T(), expectedUserAgent, userAgent)
}

Expand All @@ -134,7 +135,7 @@ func (t *MainTest) TestGetUserAgentConfigWithCacheDirSetMaxSizeDisabled() {
},
}
userAgent := getUserAgent("AppName", getConfigForUserAgent(mountConfig))
expectedUserAgent := strings.TrimSpace(fmt.Sprintf("gcsfuse/%s (GPN:gcsfuse-AppName) (Cfg:0:0)", getVersion()))
expectedUserAgent := strings.TrimSpace(fmt.Sprintf("gcsfuse/%s (GPN:gcsfuse-AppName) (Cfg:0:0)", common.GetVersion()))
assert.Equal(t.T(), expectedUserAgent, userAgent)
}

Expand All @@ -144,7 +145,7 @@ func (t *MainTest) TestGetUserAgentWhenMetadataImageTypeEnvVarSetAndAppNameNotSe

mountConfig := &cfg.Config{}
userAgent := getUserAgent("", getConfigForUserAgent(mountConfig))
expectedUserAgent := strings.TrimSpace(fmt.Sprintf("gcsfuse/%s (GPN:gcsfuse-DLVM) (Cfg:0:0)", getVersion()))
expectedUserAgent := strings.TrimSpace(fmt.Sprintf("gcsfuse/%s (GPN:gcsfuse-DLVM) (Cfg:0:0)", common.GetVersion()))

assert.Equal(t.T(), expectedUserAgent, userAgent)
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"fmt"

"github.com/googlecloudplatform/gcsfuse/v2/cfg"
"github.com/googlecloudplatform/gcsfuse/v2/common"
"github.com/googlecloudplatform/gcsfuse/v2/internal/util"
"github.com/mitchellh/mapstructure"
"github.com/spf13/cobra"
Expand All @@ -40,7 +41,7 @@ func NewRootCmd(m mountFn) (*cobra.Command, error) {
Long: `Cloud Storage FUSE is an open source FUSE adapter that lets you mount
and access Cloud Storage buckets as local file systems. For a technical overview
of Cloud Storage FUSE, see https://cloud.google.com/storage/docs/gcs-fuse.`,
Version: getVersion(),
Version: common.GetVersion(),
Args: cobra.RangeArgs(2, 3),
RunE: func(cmd *cobra.Command, args []string) error {
if cfgErr != nil {
Expand Down
7 changes: 4 additions & 3 deletions cmd/version.go → common/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,19 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd
package common

import (
"fmt"
"runtime"
)

// Set with `-ldflags -X github.com/googlecloudplatform/gcsfuse/v2/cmd.gcsfuseVersion=1.2.3`
// Set with `-ldflags -X github.com/googlecloudplatform/gcsfuse/v2/common.gcsfuseVersion=1.2.3`
// by tools/build_gcsfuse. If not defined, we use "unknown" in getVersion.
var gcsfuseVersion string

func getVersion() string {
// GetVersion returns the version of the GCSFuse binary
func GetVersion() string {
v := gcsfuseVersion
if v == "" {
v = "unknown"
Expand Down
13 changes: 9 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
contrib.go.opencensus.io/exporter/ocagent v0.7.0
contrib.go.opencensus.io/exporter/prometheus v0.4.2
contrib.go.opencensus.io/exporter/stackdriver v0.13.14
github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/trace v1.24.1
github.com/fsouza/fake-gcs-server v1.49.3
github.com/google/uuid v1.6.0
github.com/googleapis/gax-go/v2 v2.13.0
Expand All @@ -27,9 +28,11 @@ 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
go.opentelemetry.io/contrib/detectors/gcp v1.29.0
go.opentelemetry.io/otel v1.29.0
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.29.0
go.opentelemetry.io/otel/sdk v1.29.0
go.opentelemetry.io/otel/trace v1.29.0
golang.org/x/net v0.28.0
golang.org/x/oauth2 v0.22.0
golang.org/x/sync v0.8.0
Expand All @@ -53,6 +56,8 @@ require (
cloud.google.com/go/monitoring v1.20.3 // indirect
cloud.google.com/go/pubsub v1.41.0 // indirect
cloud.google.com/go/trace v1.10.11 // indirect
github.com/GoogleCloudPlatform/opentelemetry-operations-go/detectors/gcp v1.24.1 // indirect
github.com/GoogleCloudPlatform/opentelemetry-operations-go/internal/resourcemapping v0.48.1 // indirect
github.com/aws/aws-sdk-go v1.44.217 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/census-instrumentation/opencensus-proto v0.4.1 // indirect
Expand Down Expand Up @@ -101,7 +106,7 @@ require (
github.com/subosito/gotenv v1.6.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.53.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.53.0 // indirect
go.opentelemetry.io/otel/metric v1.28.0 // indirect
go.opentelemetry.io/otel/metric v1.29.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.26.0 // indirect
golang.org/x/exp v0.0.0-20240530194437-404ba88c7ed0 // indirect
Expand Down
Loading

0 comments on commit b9e53cf

Please sign in to comment.