diff --git a/gcp/gcp_test.go b/gcp/gcp_test.go deleted file mode 100644 index ff1e87c..0000000 --- a/gcp/gcp_test.go +++ /dev/null @@ -1,34 +0,0 @@ -// Copyright (c) 2024 The nilgo authors -// Use of this source code is governed by a MIT license found in the LICENSE file. - -package gcp_test - -import ( - "testing" - - "github.com/stretchr/testify/assert" - - "github.com/nil-go/nilgo/gcp" - "github.com/nil-go/nilgo/gcp/profiler" -) - -func TestLogHandler(t *testing.T) { - t.Parallel() - - assert.NotNil(t, gcp.LogHandler( - gcp.WithService("test"), - gcp.WithVersion("dev"), - gcp.WithProject("project"), - )) -} - -func TestProfiler(t *testing.T) { - t.Parallel() - - assert.NotNil(t, gcp.Profiler( - gcp.WithService("test"), - gcp.WithVersion("dev"), - gcp.WithProject("project"), - gcp.WithProfiler(profiler.WithMutexProfiling()), - )) -} diff --git a/gcp/gcp.go b/gcp/log/log.go similarity index 52% rename from gcp/gcp.go rename to gcp/log/log.go index 7058a87..9604eb7 100644 --- a/gcp/gcp.go +++ b/gcp/log/log.go @@ -1,29 +1,44 @@ // Copyright (c) 2024 The nilgo authors // Use of this source code is governed by a MIT license found in the LICENSE file. -// Package gcp provides customization for application runs on GCP. -package gcp +// Package log provides a slog.Handler integrated with [Cloud Logging]. +// +// [Cloud Logging]: https://cloud.google.com/logging +package log import ( "context" "log/slog" + "os" + "cloud.google.com/go/compute/metadata" "github.com/nil-go/sloth/gcp" "github.com/nil-go/sloth/otel" "github.com/nil-go/sloth/rate" "github.com/nil-go/sloth/sampling" "go.opentelemetry.io/otel/trace" - - "github.com/nil-go/nilgo/gcp/profiler" ) -// LogHandler returns a slog.Handler integrated with [Cloud Logging] and [Cloud Error Reporting]. +// Handler returns a slog.Handler integrated with [Cloud Logging] and [Cloud Error Reporting]. // // [Cloud Logging]: https://cloud.google.com/logging // [Cloud Error Reporting]: https://cloud.google.com/error-reporting -func LogHandler(opts ...Option) slog.Handler { +func Handler(opts ...Option) slog.Handler { option := options{} - option.apply(opts) + for _, opt := range opts { + opt(&option) + } + // Get service and version from Google Cloud Run environment variables. + if option.service == "" { + option.service = os.Getenv("K_SERVICE") + } + if option.version == "" { + option.version = os.Getenv("K_REVISION") + } + if option.project == "" { + option.project, _ = metadata.ProjectID() + } + logOpts := append( []gcp.Option{ gcp.WithErrorReporting(option.service, option.version), @@ -45,23 +60,3 @@ func LogHandler(opts ...Option) slog.Handler { return handler } - -// Profiler returns a function to start [Cloud Profiler]. -// It requires the following IAM roles: -// - roles/cloudprofiler.agent -// -// [Cloud Profiler]: https://cloud.google.com/profiler -func Profiler(opts ...Option) func(context.Context) error { - option := options{} - option.apply(opts) - profilerOpts := append( - []profiler.Option{ - profiler.WithProject(option.project), - profiler.WithService(option.service), - profiler.WithVersion(option.version), - }, - option.profilerOpts..., - ) - - return profiler.Run(profilerOpts...) -} diff --git a/gcp/log/log_test.go b/gcp/log/log_test.go new file mode 100644 index 0000000..adcf7ae --- /dev/null +++ b/gcp/log/log_test.go @@ -0,0 +1,22 @@ +// Copyright (c) 2024 The nilgo authors +// Use of this source code is governed by a MIT license found in the LICENSE file. + +package log_test + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + "github.com/nil-go/nilgo/gcp/log" +) + +func TestLogHandler(t *testing.T) { + t.Parallel() + + assert.NotNil(t, log.Handler( + log.WithService("test"), + log.WithVersion("dev"), + log.WithProject("project"), + )) +} diff --git a/gcp/option.go b/gcp/log/option.go similarity index 54% rename from gcp/option.go rename to gcp/log/option.go index ccf55c7..7e63b53 100644 --- a/gcp/option.go +++ b/gcp/log/option.go @@ -1,14 +1,10 @@ // Copyright (c) 2024 The nilgo authors // Use of this source code is governed by a MIT license found in the LICENSE file. -package gcp +package log import ( - "os" - - "cloud.google.com/go/compute/metadata" "github.com/nil-go/sloth/gcp" - "google.golang.org/api/option" ) // WithProject provides the GCP project ID. @@ -38,26 +34,13 @@ func WithVersion(version string) Option { } } -// WithLog provides the gcp.Option(s) to configure the logger. -func WithLog(opts ...gcp.Option) Option { +// WithOption provides the gcp.Option(s) to configure the logger. +func WithOption(opts ...gcp.Option) Option { return func(options *options) { - if options.logOpts == nil { - options.logOpts = []gcp.Option{} - } options.logOpts = append(options.logOpts, opts...) } } -// WithProfiler provides the option.ClientOption(s) to configure the profiler. -func WithProfiler(opts ...option.ClientOption) Option { - return func(options *options) { - if options.profilerOpts == nil { - options.profilerOpts = []option.ClientOption{} - } - options.profilerOpts = append(options.profilerOpts, opts...) - } -} - type ( // Option configures the GCP runtime with specific options. Option func(*options) @@ -65,25 +48,6 @@ type ( project string service string version string - - logOpts []gcp.Option - profilerOpts []option.ClientOption + logOpts []gcp.Option } ) - -func (o *options) apply(opts []Option) { - for _, opt := range opts { - opt(o) - } - - // Get service and version from Google Cloud Run environment variables. - if o.service == "" { - o.service = os.Getenv("K_SERVICE") - } - if o.version == "" { - o.version = os.Getenv("K_REVISION") - } - if o.project == "" { - o.project, _ = metadata.ProjectID() - } -} diff --git a/gcp/profiler/profiler.go b/gcp/profiler/profiler.go index 39f7bd1..a1e02df 100644 --- a/gcp/profiler/profiler.go +++ b/gcp/profiler/profiler.go @@ -1,22 +1,27 @@ // Copyright (c) 2024 The nilgo authors // Use of this source code is governed by a MIT license found in the LICENSE file. -// Package profiler enables the Cloud Profiler for the application. +// Package profiler enables the [Cloud Profiler] for the application. +// +// [Cloud Profiler]: https://cloud.google.com/profiler package profiler import ( "context" "fmt" "log/slog" + "os" + "cloud.google.com/go/compute/metadata" "cloud.google.com/go/profiler" "google.golang.org/api/option" ) -// Run starts the Cloud Profiler with given options. +// Run starts the [Cloud Profiler] with given options. +// It requires the following IAM roles: +// - roles/cloudprofiler.agent // -// It's not indented to be used directly in the application. -// Please use [gcp.WithProfiler] instead. +// [Cloud Profiler]: https://cloud.google.com/profiler func Run(opts ...option.ClientOption) func(context.Context) error { return func(ctx context.Context) error { config := profiler.Config{} @@ -25,6 +30,16 @@ func Run(opts ...option.ClientOption) func(context.Context) error { f.fn(&config) } } + // Get service and version from Google Cloud Run environment variables. + if config.Service == "" { + config.Service = os.Getenv("K_SERVICE") + } + if config.ServiceVersion == "" { + config.ServiceVersion = os.Getenv("K_REVISION") + } + if config.ProjectID == "" { + config.ProjectID, _ = metadata.ProjectID() + } if err := profiler.Start(config, opts...); err != nil { return fmt.Errorf("start cloud profiling: %w", err) diff --git a/gcp/profiler/profiler_test.go b/gcp/profiler/profiler_test.go new file mode 100644 index 0000000..8157d1d --- /dev/null +++ b/gcp/profiler/profiler_test.go @@ -0,0 +1,23 @@ +// Copyright (c) 2024 The nilgo authors +// Use of this source code is governed by a MIT license found in the LICENSE file. + +package profiler_test + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + "github.com/nil-go/nilgo/gcp/profiler" +) + +func TestProfiler(t *testing.T) { + t.Parallel() + + assert.NotNil(t, profiler.Run( + profiler.WithService("test"), + profiler.WithVersion("dev"), + profiler.WithProject("project"), + profiler.WithMutexProfiling(), + )) +}