Skip to content

Commit 63ea2bd

Browse files
committed
chore: Add comments, and clean up receiver naming
1 parent 03e48a0 commit 63ea2bd

3 files changed

Lines changed: 17 additions & 14 deletions

File tree

localapi.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ type LocalAPIDiscoverer struct {
1515
Client local.Client
1616
}
1717

18+
// peerToDevice converts a PeerStatus from the local API to the internal Device format.
1819
func peerToDevice(p *ipnstate.PeerStatus, d *Device) {
1920
for i := range p.TailscaleIPs {
2021
d.Addresses = append(d.Addresses, p.TailscaleIPs[i].String())

ratelimited.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,35 +22,35 @@ type RateLimitedDiscoverer struct {
2222
last []Device
2323
}
2424

25-
func (c *RateLimitedDiscoverer) refreshDevices(ctx context.Context) ([]Device, error) {
25+
func (d *RateLimitedDiscoverer) refreshDevices(ctx context.Context) ([]Device, error) {
2626
rateLimitedRequestRefreshes.Inc()
2727

28-
devices, err := c.Wrap.Devices(ctx)
28+
devices, err := d.Wrap.Devices(ctx)
2929
if err != nil {
3030
rateLimitedStaleResults.Inc()
3131
return devices, fmt.Errorf("%w: %w", errStaleResults, err)
3232
}
3333

34-
c.mu.Lock()
35-
defer c.mu.Unlock()
36-
c.last = devices
37-
c.earliest = time.Now().Add(c.Frequency)
34+
d.mu.Lock()
35+
defer d.mu.Unlock()
36+
d.last = devices
37+
d.earliest = time.Now().Add(d.Frequency)
3838
return devices, nil
3939
}
4040

4141
// Devices reported by the Tailscale public API as belonging to the configured
4242
// tailnet.
43-
func (c *RateLimitedDiscoverer) Devices(ctx context.Context) ([]Device, error) {
43+
func (d *RateLimitedDiscoverer) Devices(ctx context.Context) ([]Device, error) {
4444
rateLimitedRequests.Inc()
4545

46-
c.mu.RLock()
47-
expired := time.Now().After(c.earliest)
48-
last := make([]Device, len(c.last))
49-
_ = copy(last, c.last)
50-
c.mu.RUnlock()
46+
d.mu.RLock()
47+
expired := time.Now().After(d.earliest)
48+
last := make([]Device, len(d.last))
49+
_ = copy(last, d.last)
50+
d.mu.RUnlock()
5151

5252
if expired {
53-
return c.refreshDevices(ctx)
53+
return d.refreshDevices(ctx)
5454
}
5555
return last, nil
5656
}

tailscalesd.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ func filterEmptyLabels(td TargetDescriptor) TargetDescriptor {
139139
}
140140
}
141141

142+
// tagReplaceRe matches characters in Tailscale tags that are invalid in
143+
// Prometheus label names (colons and hyphens), to be replaced with underscores.
142144
var tagReplaceRe = regexp.MustCompile(`[:-]`)
143145

144146
// tagToLabelKey translates a Tailscale tag to a Prometheus target label key.
@@ -166,7 +168,7 @@ func translate(devices []Device, filters ...TargetFilter) []TargetDescriptor {
166168
for i, d := range devices {
167169
found[i] = TargetDescriptor{
168170
Targets: d.Addresses,
169-
// All labels added here, except for tags.
171+
// Base device labels. Tags are added separately below.
170172
Labels: map[string]string{
171173
LabelMetaAPI: d.API,
172174
LabelMetaDeviceAuthorized: fmt.Sprint(d.Authorized),

0 commit comments

Comments
 (0)