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:
40
cmd/cmd.go
40
cmd/cmd.go
@ -4,6 +4,7 @@ package cmd
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
|
"os"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@ -23,6 +24,7 @@ import (
|
|||||||
"go-micro.dev/v5/debug/profile/pprof"
|
"go-micro.dev/v5/debug/profile/pprof"
|
||||||
"go-micro.dev/v5/debug/trace"
|
"go-micro.dev/v5/debug/trace"
|
||||||
"go-micro.dev/v5/logger"
|
"go-micro.dev/v5/logger"
|
||||||
|
mprofile "go-micro.dev/v5/profile"
|
||||||
"go-micro.dev/v5/registry"
|
"go-micro.dev/v5/registry"
|
||||||
"go-micro.dev/v5/registry/consul"
|
"go-micro.dev/v5/registry/consul"
|
||||||
"go-micro.dev/v5/registry/etcd"
|
"go-micro.dev/v5/registry/etcd"
|
||||||
@ -141,8 +143,8 @@ var (
|
|||||||
},
|
},
|
||||||
&cli.StringFlag{
|
&cli.StringFlag{
|
||||||
Name: "profile",
|
Name: "profile",
|
||||||
Usage: "Debug profiler for cpu and memory stats",
|
Usage: "Plugin profile to use. (local, nats, etc)",
|
||||||
EnvVars: []string{"MICRO_DEBUG_PROFILE"},
|
EnvVars: []string{"MICRO_PROFILE"},
|
||||||
},
|
},
|
||||||
&cli.StringFlag{
|
&cli.StringFlag{
|
||||||
Name: "registry",
|
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
|
// Set the profile
|
||||||
if name := ctx.String("profile"); len(name) > 0 {
|
if name := ctx.String("profile"); len(name) > 0 {
|
||||||
p, ok := c.opts.Profiles[name]
|
p, ok := c.opts.Profiles[name]
|
||||||
|
43
profile/profile.go
Normal file
43
profile/profile.go
Normal 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
|
Reference in New Issue
Block a user