1
0
mirror of https://github.com/go-micro/go-micro.git synced 2025-07-06 22:35:51 +02:00

feat: re-add profiles (#2772)

* feat: re-add profiles

* fix: make profile separate package

* fix: make profile separate package

* fix: profile flag

* fix: defaults
This commit is contained in:
Brian Ketelsen
2025-05-19 13:59:28 -04:00
committed by GitHub
parent a03ab5f601
commit e12504ce3a
2 changed files with 81 additions and 2 deletions

View File

@ -4,6 +4,7 @@ package cmd
import (
"fmt"
"math/rand"
"os"
"sort"
"strings"
"time"
@ -23,6 +24,7 @@ import (
"go-micro.dev/v5/debug/profile/pprof"
"go-micro.dev/v5/debug/trace"
"go-micro.dev/v5/logger"
mprofile "go-micro.dev/v5/profile"
"go-micro.dev/v5/registry"
"go-micro.dev/v5/registry/consul"
"go-micro.dev/v5/registry/etcd"
@ -141,8 +143,8 @@ var (
},
&cli.StringFlag{
Name: "profile",
Usage: "Debug profiler for cpu and memory stats",
EnvVars: []string{"MICRO_DEBUG_PROFILE"},
Usage: "Plugin profile to use. (local, nats, etc)",
EnvVars: []string{"MICRO_PROFILE"},
},
&cli.StringFlag{
Name: "registry",
@ -437,6 +439,40 @@ func (c *cmd) Before(ctx *cli.Context) error {
}
}
// --- Profile Grouping Extension ---
// Check for new profile flag/env (not just debug profiler)
profileName := ctx.String("profile")
if profileName == "" {
profileName = os.Getenv("MICRO_PROFILE")
}
if profileName != "" {
switch profileName {
case "local":
imported := mprofile.LocalProfile()
*c.opts.Registry = imported.Registry
registry.DefaultRegistry = imported.Registry
*c.opts.Broker = imported.Broker
broker.DefaultBroker = imported.Broker
*c.opts.Store = imported.Store
store.DefaultStore = imported.Store
*c.opts.Transport = imported.Transport
transport.DefaultTransport = imported.Transport
case "nats":
imported := mprofile.NatsProfile()
*c.opts.Registry = imported.Registry
registry.DefaultRegistry = imported.Registry
*c.opts.Broker = imported.Broker
broker.DefaultBroker = imported.Broker
*c.opts.Store = imported.Store
store.DefaultStore = imported.Store
*c.opts.Transport = imported.Transport
transport.DefaultTransport = imported.Transport
// Add more profiles as needed
default:
return fmt.Errorf("unsupported profile: %s", profileName)
}
}
// Set the profile
if name := ctx.String("profile"); len(name) > 0 {
p, ok := c.opts.Profiles[name]

43
profile/profile.go Normal file
View File

@ -0,0 +1,43 @@
// Package profileconfig provides grouped plugin profiles for go-micro
package profile
import (
"os"
"go-micro.dev/v5/broker"
"go-micro.dev/v5/broker/http"
"go-micro.dev/v5/broker/nats"
"go-micro.dev/v5/registry"
nreg "go-micro.dev/v5/registry/nats"
"go-micro.dev/v5/store"
"go-micro.dev/v5/transport"
)
type Profile struct {
Registry registry.Registry
Broker broker.Broker
Store store.Store
Transport transport.Transport
}
func LocalProfile() Profile {
return Profile{
Registry: registry.NewMDNSRegistry(),
Broker: http.NewHttpBroker(),
Store: store.NewFileStore(),
Transport: transport.NewHTTPTransport(),
}
}
func NatsProfile() Profile {
addr := os.Getenv("MICRO_NATS_ADDRESS")
return Profile{
Registry: nreg.NewNatsRegistry(registry.Addrs(addr)),
Broker: nats.NewNatsBroker(broker.Addrs(addr)),
Store: store.NewFileStore(), // nats-backed store when available
Transport: transport.NewHTTPTransport(), // nats transport when available
}
}
// Add more profiles as needed, e.g. grpc