Skip to content

Commit

Permalink
refactor gcp (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
ktong authored May 17, 2024
1 parent eaf22fd commit 12c6d46
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 105 deletions.
34 changes: 0 additions & 34 deletions gcp/gcp_test.go

This file was deleted.

49 changes: 22 additions & 27 deletions gcp/gcp.go → gcp/log/log.go
Original file line number Diff line number Diff line change
@@ -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),
Expand All @@ -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...)
}
22 changes: 22 additions & 0 deletions gcp/log/log_test.go
Original file line number Diff line number Diff line change
@@ -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"),
))
}
44 changes: 4 additions & 40 deletions gcp/option.go → gcp/log/option.go
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -38,52 +34,20 @@ 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)
options struct {
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()
}
}
23 changes: 19 additions & 4 deletions gcp/profiler/profiler.go
Original file line number Diff line number Diff line change
@@ -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{}
Expand All @@ -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)
Expand Down
23 changes: 23 additions & 0 deletions gcp/profiler/profiler_test.go
Original file line number Diff line number Diff line change
@@ -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(),
))
}

0 comments on commit 12c6d46

Please sign in to comment.