From 85e2eb7c6af4f78d6dc323641f95449f9dbfb8d9 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Wed, 5 Jul 2023 14:17:03 +0000 Subject: [PATCH] fix: improve logging --- srv/srv.go | 13 +++++++++---- tailscale/tailscale.go | 4 +++- zeroconf/zeroconf.go | 3 ++- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/srv/srv.go b/srv/srv.go index 4bd268a..2085ca0 100644 --- a/srv/srv.go +++ b/srv/srv.go @@ -10,10 +10,15 @@ import ( "github.com/charmbracelet/wishlist" ) +const ( + service = "_ssh._tcp" + txtPrefix = "wishlist.name " +) + // Endpoints returns the _ssh._tcp SRV records on the given domain as // Wishlist endpoints. func Endpoints(ctx context.Context, domain string) ([]*wishlist.Endpoint, error) { - log.Info("discovering _ssh._tcp SRV records", "domain", domain) + log.Debug("discovering SRV records", "service", service, "domain", domain) _, srvs, err := net.DefaultResolver.LookupSRV(ctx, "ssh", "tcp", domain) if err != nil { return nil, fmt.Errorf("srv: could not resolve %s: %w", domain, err) @@ -22,11 +27,11 @@ func Endpoints(ctx context.Context, domain string) ([]*wishlist.Endpoint, error) if err != nil { return nil, fmt.Errorf("srv: could not resolve %s: %w", domain, err) } - return fromRecords(srvs, txts), nil + endpoints := fromRecords(srvs, txts) + log.Info("discovered from SRV records", "service", service, "domain", domain, "devices", len(endpoints)) + return endpoints, nil } -const txtPrefix = "wishlist.name " - func fromRecords(srvs []*net.SRV, txts []string) []*wishlist.Endpoint { result := make([]*wishlist.Endpoint, 0, len(srvs)) for _, entry := range srvs { diff --git a/tailscale/tailscale.go b/tailscale/tailscale.go index bcf4260..7927896 100644 --- a/tailscale/tailscale.go +++ b/tailscale/tailscale.go @@ -14,7 +14,7 @@ import ( // Endpoints returns the found endpoints from tailscale. func Endpoints(ctx context.Context, tailnet, key string) ([]*wishlist.Endpoint, error) { - log.Info("discovering from tailscale", "tailnet", tailnet) + log.Debug("discovering from tailscale", "tailnet", tailnet) req, err := http.NewRequestWithContext( ctx, http.MethodGet, @@ -50,6 +50,8 @@ func Endpoints(ctx context.Context, tailnet, key string) ([]*wishlist.Endpoint, Address: net.JoinHostPort(device.Addresses[0], "22"), }) } + + log.Info("discovered from tailscale", "tailnet", tailnet, "devices", len(endpoints)) return endpoints, nil } diff --git a/zeroconf/zeroconf.go b/zeroconf/zeroconf.go index 7bd3660..111be8d 100644 --- a/zeroconf/zeroconf.go +++ b/zeroconf/zeroconf.go @@ -17,7 +17,7 @@ const service = "_ssh._tcp" // Endpoints returns the found endpoints from zeroconf. func Endpoints(ctx context.Context, domain string, timeout time.Duration) ([]*wishlist.Endpoint, error) { - log.Info("discovering from zeroconf", "service", service, "domain", domain) + log.Debug("discovering from zeroconf", "service", service, "domain", domain) r, err := zeroconf.NewResolver() if err != nil { return nil, fmt.Errorf("zeroconf: could not create resolver: %w", err) @@ -40,5 +40,6 @@ func Endpoints(ctx context.Context, domain string, timeout time.Duration) ([]*wi Address: net.JoinHostPort(hostname, port), }) } + log.Info("discovered from zeroconf", "service", service, "domain", domain, "devices", len(endpoints)) return endpoints, nil }