Skip to content

Commit e12504c

Browse files
authored
feat: re-add profiles (#2772)
* feat: re-add profiles * fix: make profile separate package * fix: make profile separate package * fix: profile flag * fix: defaults
1 parent a03ab5f commit e12504c

File tree

2 files changed

+81
-2
lines changed

2 files changed

+81
-2
lines changed

cmd/cmd.go

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package cmd
44
import (
55
"fmt"
66
"math/rand"
7+
"os"
78
"sort"
89
"strings"
910
"time"
@@ -23,6 +24,7 @@ import (
2324
"go-micro.dev/v5/debug/profile/pprof"
2425
"go-micro.dev/v5/debug/trace"
2526
"go-micro.dev/v5/logger"
27+
mprofile "go-micro.dev/v5/profile"
2628
"go-micro.dev/v5/registry"
2729
"go-micro.dev/v5/registry/consul"
2830
"go-micro.dev/v5/registry/etcd"
@@ -141,8 +143,8 @@ var (
141143
},
142144
&cli.StringFlag{
143145
Name: "profile",
144-
Usage: "Debug profiler for cpu and memory stats",
145-
EnvVars: []string{"MICRO_DEBUG_PROFILE"},
146+
Usage: "Plugin profile to use. (local, nats, etc)",
147+
EnvVars: []string{"MICRO_PROFILE"},
146148
},
147149
&cli.StringFlag{
148150
Name: "registry",
@@ -437,6 +439,40 @@ func (c *cmd) Before(ctx *cli.Context) error {
437439
}
438440
}
439441

442+
// --- Profile Grouping Extension ---
443+
// Check for new profile flag/env (not just debug profiler)
444+
profileName := ctx.String("profile")
445+
if profileName == "" {
446+
profileName = os.Getenv("MICRO_PROFILE")
447+
}
448+
if profileName != "" {
449+
switch profileName {
450+
case "local":
451+
imported := mprofile.LocalProfile()
452+
*c.opts.Registry = imported.Registry
453+
registry.DefaultRegistry = imported.Registry
454+
*c.opts.Broker = imported.Broker
455+
broker.DefaultBroker = imported.Broker
456+
*c.opts.Store = imported.Store
457+
store.DefaultStore = imported.Store
458+
*c.opts.Transport = imported.Transport
459+
transport.DefaultTransport = imported.Transport
460+
case "nats":
461+
imported := mprofile.NatsProfile()
462+
*c.opts.Registry = imported.Registry
463+
registry.DefaultRegistry = imported.Registry
464+
*c.opts.Broker = imported.Broker
465+
broker.DefaultBroker = imported.Broker
466+
*c.opts.Store = imported.Store
467+
store.DefaultStore = imported.Store
468+
*c.opts.Transport = imported.Transport
469+
transport.DefaultTransport = imported.Transport
470+
// Add more profiles as needed
471+
default:
472+
return fmt.Errorf("unsupported profile: %s", profileName)
473+
}
474+
}
475+
440476
// Set the profile
441477
if name := ctx.String("profile"); len(name) > 0 {
442478
p, ok := c.opts.Profiles[name]

profile/profile.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Package profileconfig provides grouped plugin profiles for go-micro
2+
package profile
3+
4+
import (
5+
"os"
6+
"go-micro.dev/v5/broker"
7+
"go-micro.dev/v5/broker/http"
8+
"go-micro.dev/v5/broker/nats"
9+
"go-micro.dev/v5/registry"
10+
nreg "go-micro.dev/v5/registry/nats"
11+
"go-micro.dev/v5/store"
12+
13+
"go-micro.dev/v5/transport"
14+
15+
)
16+
17+
type Profile struct {
18+
Registry registry.Registry
19+
Broker broker.Broker
20+
Store store.Store
21+
Transport transport.Transport
22+
}
23+
24+
func LocalProfile() Profile {
25+
return Profile{
26+
Registry: registry.NewMDNSRegistry(),
27+
Broker: http.NewHttpBroker(),
28+
Store: store.NewFileStore(),
29+
Transport: transport.NewHTTPTransport(),
30+
}
31+
}
32+
33+
func NatsProfile() Profile {
34+
addr := os.Getenv("MICRO_NATS_ADDRESS")
35+
return Profile{
36+
Registry: nreg.NewNatsRegistry(registry.Addrs(addr)),
37+
Broker: nats.NewNatsBroker(broker.Addrs(addr)),
38+
Store: store.NewFileStore(), // nats-backed store when available
39+
Transport: transport.NewHTTPTransport(), // nats transport when available
40+
}
41+
}
42+
43+
// Add more profiles as needed, e.g. grpc

0 commit comments

Comments
 (0)