]> go.fuhry.dev Git - runtime.git/commitdiff
[metricbus] publish server in SD
authorDan Fuhry <dan@fuhry.com>
Fri, 4 Apr 2025 12:25:50 +0000 (08:25 -0400)
committerDan Fuhry <dan@fuhry.com>
Fri, 4 Apr 2025 12:25:50 +0000 (08:25 -0400)
metrics/metricbus/internal/server.go

index a93927bccd3c4001b5ba179e763e0afa9add49d5..30b14076805962f171ac2ef338c6deb617f2b949 100644 (file)
@@ -18,6 +18,7 @@ import (
        "go.fuhry.dev/runtime/constants"
        "go.fuhry.dev/runtime/metrics/metricbus"
        "go.fuhry.dev/runtime/mtls"
+       "go.fuhry.dev/runtime/sd"
        "go.fuhry.dev/runtime/utils/hashset"
        "go.fuhry.dev/runtime/utils/hostname"
        "go.fuhry.dev/runtime/utils/log"
@@ -82,6 +83,7 @@ type mbServer struct {
        httpServer  *http.Server
        httpMux     *http.ServeMux
        listenOn    *net.TCPAddr
+       publisher   *sd.SDPublisher
        startStopMu sync.Mutex
 }
 
@@ -145,6 +147,12 @@ func NewMetricBusServerWithPort(port uint) (*mbServer, error) {
                },
                listenOn: tcpAddr,
                servicer: servicer,
+               publisher: &sd.SDPublisher{
+                       Protocol:      sd.ProtocolTCP,
+                       AdvertisePort: uint16(port),
+                       Service:       "otel-tls",
+                       ShardName:     fmt.Sprintf("%s-metric-collector", constants.OrgSlug),
+               },
        }
 
        server.log.V(1).Infof("Server configured to listen on %s", tcpAddr.String())
@@ -190,6 +198,7 @@ func (s *mbServer) Start(ctx context.Context) error {
                shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
 
                defer s.startStopMu.Unlock()
+               defer s.publisher.Unpublish()
                defer cancel()
                defer dbusConn.Close()
                defer s.httpServer.Shutdown(shutdownCtx)
@@ -205,6 +214,10 @@ func (s *mbServer) Start(ctx context.Context) error {
                        }
                }
        })()
+       err = s.publisher.Publish(s.ctx)
+       if err != nil {
+               s.log.Warnf("failed to publish service in sd: %v", err)
+       }
 
        s.log.Notice("Server started")