Skip to content

Commit

Permalink
refactor (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
ktong authored May 5, 2024
1 parent eb2000d commit c41706e
Show file tree
Hide file tree
Showing 14 changed files with 167 additions and 218 deletions.
5 changes: 0 additions & 5 deletions config/konf.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,6 @@ func New(opts ...Option) (*konf.Config, error) {
}
// Ignore error: env loader does not return error.
_ = config.Load(env.New())
if options.fn != nil {
if err := options.fn(config); err != nil {
return nil, err
}
}

return config, nil
}
29 changes: 0 additions & 29 deletions config/konf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@
package config_test

import (
"errors"
"testing"
"testing/fstest"

"github.com/nil-go/konf"
"github.com/nil-go/konf/provider/env"
"github.com/nil-go/konf/provider/fs"
"gopkg.in/yaml.v3"

"github.com/nil-go/nilgo/config"
"github.com/nil-go/nilgo/internal/assert"
Expand Down Expand Up @@ -87,21 +84,6 @@ Here are other value(loader)s:
key: "nilgo.Source",
explanation: `nilgo.Source has no configuration.
`,
},
{
description: "with",
opts: []config.Option{
config.With(func(cfg *konf.Config) error {
return cfg.Load(fs.New(nil, "testdata/config.yaml", fs.WithUnmarshal(yaml.Unmarshal)))
}),
config.WithFS(fstest.MapFS{"config/config.yaml": {Data: []byte("nilgo:\n source: fs")}}),
},
key: "nilgo.Source",
explanation: `nilgo.Source has value[file] that is loaded by loader[fs:///testdata/config.yaml].
Here are other value(loader)s:
- fs(fs:///config/config.yaml)
`,
},
{
Expand All @@ -111,17 +93,6 @@ Here are other value(loader)s:
err: "load config file config/config.yaml: load configuration: unmarshal: yaml: unmarshal errors:\n" +
" line 1: cannot unmarshal !!str `nilgo` into map[string]interface {}",
},
{
description: "with error",
opts: []config.Option{
config.With(func(*konf.Config) error {
return errors.New("with error")
}),
config.WithFS(fstest.MapFS{"config/config.yaml": {Data: []byte("nilgo:\n source: fs")}}),
},
key: "nilgo.Source",
err: "with error",
},
}

for _, testcase := range testcases {
Expand Down
8 changes: 0 additions & 8 deletions config/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,12 @@ func WithOption(opts ...konf.Option) Option {
}
}

// With allows to customize the config with the given function.
func With(fn func(config *konf.Config) error) Option {
return func(options *options) {
options.fn = fn
}
}

type (
// Option configures the config with specific options.
Option func(*options)
options struct {
opts []konf.Option
files []string
fs fs.FS
fn func(*konf.Config) error
}
)
25 changes: 18 additions & 7 deletions gcp/gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ package gcp
import (
"context"
"fmt"
"log/slog"
"os"

"cloud.google.com/go/compute/metadata"
"cloud.google.com/go/profiler"
"github.com/nil-go/sloth/gcp"
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
Expand Down Expand Up @@ -57,13 +59,6 @@ func Options(opts ...Option) ([]any, error) { //nolint:cyclop,funlen
return appOpts, nil
}

if runner := profile(&option); runner != nil {
appOpts = append(appOpts, runner)
}
if option.traceOpts == nil && option.metricOpts == nil {
return appOpts, nil
}

res := resource.Default()
ctx := context.Background()
if option.traceOpts != nil {
Expand Down Expand Up @@ -93,5 +88,21 @@ func Options(opts ...Option) ([]any, error) { //nolint:cyclop,funlen
)
}

if option.profilerOpts != nil || option.mutextProfiling {
appOpts = append(appOpts, func(ctx context.Context) error {
if err := profiler.Start(profiler.Config{
ProjectID: option.project,
Service: option.service,
ServiceVersion: option.version,
MutexProfiling: option.mutextProfiling,
}, option.profilerOpts...); err != nil {
return fmt.Errorf("start cloud profiling: %w", err)
}
slog.LogAttrs(ctx, slog.LevelInfo, "Cloud profiling has been initialized.")

return nil
})
}

return appOpts, nil
}
31 changes: 27 additions & 4 deletions gcp/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/nil-go/sloth/gcp"
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
"google.golang.org/api/option"
)

// WithProject provides the GCP project ID.
Expand Down Expand Up @@ -67,6 +68,27 @@ func WithMetric(opts ...otlpmetricgrpc.Option) Option {
}
}

// WithProfiler enables Google [Cloud Profiler].
// It requires the following IAM roles:
// - roles/cloudprofiler.agent
//
// [Cloud Profiler]: https://cloud.google.com/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...)
}
}

// WithMutextProfiling enables mutex profiling.
func WithMutextProfiling() Option {
return func(options *options) {
options.mutextProfiling = true
}
}

type (
// Option configures the GCP runtime with specific options.
Option func(*options)
Expand All @@ -75,9 +97,10 @@ type (
service string
version string

logOpts []gcp.Option
metricOpts []otlpmetricgrpc.Option
traceOpts []otlptracegrpc.Option
profilerOptions
logOpts []gcp.Option
metricOpts []otlpmetricgrpc.Option
traceOpts []otlptracegrpc.Option
profilerOpts []option.ClientOption
mutextProfiling bool
}
)
60 changes: 0 additions & 60 deletions gcp/profiler.go

This file was deleted.

6 changes: 3 additions & 3 deletions grpc/client.go → grpc/client/client.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) 2024 The nilgo authors
// Use of this source code is governed by a MIT license found in the LICENSE file.

package grpc
package client

import (
_ "unsafe" // For go:linkname
Expand All @@ -12,10 +12,10 @@ import (
//go:linkname addGlobalDialOptions google.golang.org/grpc/internal.AddGlobalDialOptions
var addGlobalDialOptions any //nolint:gochecknoglobals // func(opt ...DialOption)

// WithGlobalDialOption adds global dial options for all gRPC clients.
// AddGlobalDialOption adds global dial options for all gRPC clients.
//
// CAUTION: This function may break in new version of `google.golang.org/grpc`
// since it is using internal package from grpc.
func WithGlobalDialOption(opts ...grpc.DialOption) {
func AddGlobalDialOption(opts ...grpc.DialOption) {
addGlobalDialOptions.(func(opt ...grpc.DialOption))(opts...) //nolint:forcetypeassert
}
8 changes: 4 additions & 4 deletions grpc/client_test.go → grpc/client/client_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
// Copyright (c) 2024 The nilgo authors
// Use of this source code is governed by a MIT license found in the LICENSE file.

package grpc_test
package client_test

import (
"testing"

ngrpc "github.com/nil-go/nilgo/grpc"
ngrpc "github.com/nil-go/nilgo/grpc/client"
)

func TestLinkName(*testing.T) {
func TestAddGlobalDialOption(*testing.T) {
// Should not panic.
ngrpc.WithGlobalDialOption()
ngrpc.AddGlobalDialOption()
}
81 changes: 0 additions & 81 deletions http/client.go

This file was deleted.

Loading

0 comments on commit c41706e

Please sign in to comment.