/
otel_default_impl.go
51 lines (41 loc) · 1.38 KB
/
otel_default_impl.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//go:build !wasm
package otel
import (
"context"
goNATs "github.com/enobufs/go-nats/nats"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
)
func broflakeTracer() trace.Tracer {
return otel.GetTracerProvider().Tracer("broflake")
}
// collectAndSendNATBehaviorTelemetry attempts to perform NAT behavior discovery, trying one batch
// of STUN servers, and sends the results in an attribute-rich Otel trace under name 'name'
func CollectAndSendNATBehaviorTelemetry(srvs []string, name string) {
var res *goNATs.DiscoverResult
attrs := trace.WithAttributes()
for _, s := range srvs {
n, err := goNATs.NewNATS(&goNATs.Config{
Server: s[5:], // XXX: hackily remove the leading 'stun:' from the STUN servers in the ProxyConfig
Verbose: false,
})
if err != nil {
continue
}
res, err = n.Discover()
if err == nil {
attrs = trace.WithAttributes(
attribute.Bool("nat_is_natted", res.IsNatted),
attribute.String("nat_mapping_behavior", res.MappingBehavior.String()),
attribute.String("nat_filtering_behavior", res.FilteringBehavior.String()),
attribute.Bool("nat_port_preservation", res.PortPreservation),
attribute.String("nat_type", res.NATType),
attribute.String("nat_external_ip", res.ExternalIP),
)
break
}
}
_, span := broflakeTracer().Start(context.Background(), name, attrs)
span.End()
}