Skip to content

Commit f44ad39

Browse files
committed
NewServer now has optional publicAddr argument
1 parent e5729f1 commit f44ad39

6 files changed

Lines changed: 37 additions & 11 deletions

File tree

internal/grpcinject/opentelemetry.go

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import (
3131
"google.golang.org/grpc"
3232
)
3333

34-
func OpenTelemetry(config *config.Config, addr net.Addr) error {
34+
func OpenTelemetry(config *config.Config, localAddr net.Addr, publicAddr string) error {
3535
resOptions := []resource.Option{
3636
resource.WithHost(),
3737
resource.WithOS(),
@@ -40,8 +40,8 @@ func OpenTelemetry(config *config.Config, addr net.Addr) error {
4040
resource.WithAttributes(semconv.ServiceName(config.OpenTelemetry.ServiceName)),
4141
}
4242

43-
if addr != nil {
44-
host, port, err := net.SplitHostPort(addr.String())
43+
if localAddr != nil {
44+
host, port, err := net.SplitHostPort(localAddr.String())
4545
if err != nil {
4646
return err
4747
}
@@ -52,10 +52,27 @@ func OpenTelemetry(config *config.Config, addr net.Addr) error {
5252
}
5353

5454
resOptions = append(resOptions,
55-
resource.WithAttributes(semconv.NetworkTransportKey.String(addr.Network())),
55+
resource.WithAttributes(semconv.NetworkTransportKey.String(localAddr.Network())),
5656
resource.WithAttributes(semconv.NetworkLocalAddress(host)),
5757
resource.WithAttributes(semconv.NetworkLocalPort(portInt)),
5858
)
59+
60+
if publicAddr != localAddr.String() {
61+
host, port, err = net.SplitHostPort(publicAddr)
62+
if err != nil {
63+
return err
64+
}
65+
66+
portInt, err = strconv.Atoi(port)
67+
if err != nil {
68+
return err
69+
}
70+
}
71+
72+
resOptions = append(resOptions,
73+
resource.WithAttributes(semconv.ServerAddress(host)),
74+
resource.WithAttributes(semconv.ServerPort(portInt)),
75+
)
5976
}
6077

6178
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
@@ -83,7 +100,7 @@ func OpenTelemetry(config *config.Config, addr net.Addr) error {
83100
propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}),
84101
)
85102

86-
if addr != nil {
103+
if localAddr != nil {
87104
config.GRPCOptions.Server = append(config.GRPCOptions.Server,
88105
grpc.StatsHandler(otelgrpc.NewServerHandler(tracerProvider, propagators)),
89106
)

rpcplatform_grpcinject.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ import (
2222
"github.com/nexcode/rpcplatform/internal/grpcinject"
2323
)
2424

25-
func (p *RPCPlatform) grpcinject(addr net.Addr) error {
25+
func (p *RPCPlatform) grpcinject(localAddr net.Addr, publicAddr string) error {
2626
if p.config.OpenTelemetry != nil {
27-
if err := grpcinject.OpenTelemetry(p.config, addr); err != nil {
27+
if err := grpcinject.OpenTelemetry(p.config, localAddr, publicAddr); err != nil {
2828
return err
2929
}
3030
}

rpcplatform_newclient.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func (p *RPCPlatform) NewClient(target string, attributes *ClientAttributes) (*C
4040
grpc.WithDefaultServiceConfig(`{"loadBalancingConfig":[{"`+balancerName+`":{}}]}`),
4141
)
4242

43-
if err := p.grpcinject(nil); err != nil {
43+
if err := p.grpcinject(nil, ""); err != nil {
4444
return nil, err
4545
}
4646

rpcplatform_newserver.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ import (
2424
)
2525

2626
// NewServer creates a new server. You need to provide the server name, listening address and attributes.
27+
// Optional param `publicAddr` used if server can't reachable by listening address.
2728
// If no additional settings are needed, attributes can be nil.
28-
func (p *RPCPlatform) NewServer(name, addr string, attributes *ServerAttributes) (*Server, error) {
29+
func (p *RPCPlatform) NewServer(name, addr string, attributes *ServerAttributes, publicAddr ...string) (*Server, error) {
2930
if attributes == nil {
3031
attributes = Attributes().Server()
3132
}
@@ -35,7 +36,13 @@ func (p *RPCPlatform) NewServer(name, addr string, attributes *ServerAttributes)
3536
return nil, err
3637
}
3738

38-
if err = p.grpcinject(listener.Addr()); err != nil {
39+
if len(publicAddr) != 0 {
40+
addr = publicAddr[0]
41+
} else {
42+
addr = listener.Addr().String()
43+
}
44+
45+
if err = p.grpcinject(listener.Addr(), addr); err != nil {
3946
return nil, err
4047
}
4148

@@ -45,5 +52,6 @@ func (p *RPCPlatform) NewServer(name, addr string, attributes *ServerAttributes)
4552
server: grpc.NewServer(p.config.GRPCOptions.Server...),
4653
listener: listener,
4754
attributes: attributes,
55+
publicAddr: addr,
4856
}, nil
4957
}

server.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,5 @@ type Server struct {
2929
server *grpc.Server
3030
listener net.Listener
3131
attributes *ServerAttributes
32+
publicAddr string
3233
}

server_serve.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func (s *Server) Serve() error {
5353
}
5454

5555
ctx, cancel = context.WithTimeout(global, 4*time.Second)
56-
_, err = s.etcd.Put(ctx, path, s.listener.Addr().String(), etcd.WithLease(lease.ID))
56+
_, err = s.etcd.Put(ctx, path, s.publicAddr, etcd.WithLease(lease.ID))
5757
cancel()
5858

5959
if err != nil {

0 commit comments

Comments
 (0)