Skip to content

Commit 09f950c

Browse files
authored
instrumenting urltest and selector outbounds (#37)
* feat: instrumenting selector * feat: instrumenting urltest * fix: returning error if outbound by any reason is different then expected * Revert "fix: returning error if outbound by any reason is different then expected" This reverts commit 93c63d5.
1 parent ba6454b commit 09f950c

File tree

3 files changed

+159
-0
lines changed

3 files changed

+159
-0
lines changed

protocol/group/selector.go

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package group
2+
3+
import (
4+
"context"
5+
"net"
6+
7+
"github.com/sagernet/sing-box/adapter"
8+
"github.com/sagernet/sing-box/adapter/outbound"
9+
"github.com/sagernet/sing-box/constant"
10+
"github.com/sagernet/sing-box/log"
11+
"github.com/sagernet/sing-box/option"
12+
"github.com/sagernet/sing-box/protocol/group"
13+
"github.com/sagernet/sing/common/metadata"
14+
"github.com/sagernet/sing/common/network"
15+
"go.opentelemetry.io/otel"
16+
"go.opentelemetry.io/otel/attribute"
17+
"go.opentelemetry.io/otel/trace"
18+
)
19+
20+
func RegisterSelector(registry *outbound.Registry) {
21+
outbound.Register[option.SelectorOutboundOptions](registry, constant.TypeSelector, NewSelector)
22+
}
23+
24+
const tracerName = "github.com/getlantern/sing-box-extensions/protocol/group"
25+
26+
type Selector struct {
27+
*group.Selector
28+
}
29+
30+
func NewSelector(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.SelectorOutboundOptions) (adapter.Outbound, error) {
31+
outbound, err := group.NewSelector(ctx, router, logger, tag, options)
32+
if err != nil {
33+
return nil, err
34+
}
35+
36+
return &Selector{outbound.(*group.Selector)}, nil
37+
}
38+
39+
func (s *Selector) DialContext(ctx context.Context, network string, destination metadata.Socksaddr) (net.Conn, error) {
40+
ctx, span := otel.Tracer(tracerName).Start(ctx, "Selector.DialContext", trace.WithAttributes(
41+
attribute.String("network", network),
42+
attribute.StringSlice("supported_network_options", s.Network()),
43+
attribute.String("outbound", s.Now()),
44+
attribute.String("tag", s.Tag()),
45+
attribute.String("type", s.Type()),
46+
))
47+
defer span.End()
48+
conn, err := s.Selector.DialContext(ctx, network, destination)
49+
span.RecordError(err)
50+
return conn, err
51+
}
52+
53+
func (s *Selector) ListenPacket(ctx context.Context, destination metadata.Socksaddr) (net.PacketConn, error) {
54+
ctx, span := otel.Tracer(tracerName).Start(ctx, "Selector.ListenPacket", trace.WithAttributes(
55+
attribute.StringSlice("supported_network_options", s.Network()),
56+
attribute.String("outbound", s.Now()),
57+
attribute.String("tag", s.Tag()),
58+
attribute.String("type", s.Type()),
59+
))
60+
defer span.End()
61+
conn, err := s.Selector.ListenPacket(ctx, destination)
62+
span.RecordError(err)
63+
return conn, err
64+
}
65+
66+
func (s *Selector) NewConnectionEx(ctx context.Context, conn net.Conn, metadata adapter.InboundContext, onClose network.CloseHandlerFunc) {
67+
ctx, span := otel.Tracer(tracerName).Start(ctx, "Selector.NewConnectionEx", trace.WithAttributes(
68+
attribute.String("inbound", s.Now()),
69+
attribute.String("outbound", metadata.Outbound),
70+
attribute.String("protocol", metadata.Protocol),
71+
attribute.String("tag", s.Tag()),
72+
attribute.String("type", s.Type()),
73+
))
74+
defer span.End()
75+
76+
s.Selector.NewConnectionEx(ctx, conn, metadata, func(it error) {
77+
span.RecordError(it)
78+
onClose(it)
79+
})
80+
}
81+
func (s *Selector) NewPacketConnectionEx(ctx context.Context, conn network.PacketConn, metadata adapter.InboundContext, onClose network.CloseHandlerFunc) {
82+
ctx, span := otel.Tracer(tracerName).Start(ctx, "Selector.NewPacketConnectionEx", trace.WithAttributes(
83+
attribute.String("inbound", s.Now()),
84+
attribute.String("outbound", metadata.Outbound),
85+
attribute.String("protocol", metadata.Protocol),
86+
attribute.String("tag", s.Tag()),
87+
attribute.String("type", s.Type()),
88+
))
89+
defer span.End()
90+
91+
s.Selector.NewPacketConnectionEx(ctx, conn, metadata, func(it error) {
92+
span.RecordError(it)
93+
onClose(it)
94+
})
95+
}

protocol/group/urltest.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package group
2+
3+
import (
4+
"context"
5+
"net"
6+
7+
"github.com/sagernet/sing-box/adapter"
8+
"github.com/sagernet/sing-box/adapter/outbound"
9+
"github.com/sagernet/sing-box/constant"
10+
"github.com/sagernet/sing-box/log"
11+
"github.com/sagernet/sing-box/option"
12+
"github.com/sagernet/sing-box/protocol/group"
13+
"github.com/sagernet/sing/common/metadata"
14+
"go.opentelemetry.io/otel"
15+
"go.opentelemetry.io/otel/attribute"
16+
"go.opentelemetry.io/otel/trace"
17+
)
18+
19+
type URLTest struct {
20+
*group.URLTest
21+
}
22+
23+
func RegisterURLTest(registry *outbound.Registry) {
24+
outbound.Register[option.URLTestOutboundOptions](registry, constant.TypeURLTest, NewURLTest)
25+
}
26+
27+
func NewURLTest(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.URLTestOutboundOptions) (adapter.Outbound, error) {
28+
outbound, err := group.NewURLTest(ctx, router, logger, tag, options)
29+
if err != nil {
30+
return nil, err
31+
}
32+
return &URLTest{outbound.(*group.URLTest)}, nil
33+
}
34+
35+
func (s *URLTest) DialContext(ctx context.Context, network string, destination metadata.Socksaddr) (net.Conn, error) {
36+
ctx, span := otel.Tracer(tracerName).Start(ctx, "URLTest.DialContext", trace.WithAttributes(
37+
attribute.String("network", network),
38+
attribute.StringSlice("supported_network_options", s.Network()),
39+
attribute.String("outbound", s.Now()),
40+
attribute.String("tag", s.Tag()),
41+
attribute.String("type", s.Type()),
42+
))
43+
defer span.End()
44+
conn, err := s.URLTest.DialContext(ctx, network, destination)
45+
span.RecordError(err)
46+
return conn, err
47+
}
48+
49+
func (s *URLTest) ListenPacket(ctx context.Context, destination metadata.Socksaddr) (net.PacketConn, error) {
50+
ctx, span := otel.Tracer(tracerName).Start(ctx, "URLTest.ListenPacket", trace.WithAttributes(
51+
attribute.StringSlice("supported_network_options", s.Network()),
52+
attribute.String("outbound", s.Now()),
53+
attribute.String("tag", s.Tag()),
54+
attribute.String("type", s.Type()),
55+
))
56+
defer span.End()
57+
conn, err := s.URLTest.ListenPacket(ctx, destination)
58+
span.RecordError(err)
59+
return conn, err
60+
}

protocol/register.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ var supportedProtocols = []string{
1919
"algeneva",
2020
"amnezia",
2121
"outline",
22+
"selector",
23+
"urltest",
2224

2325
// sing-box built-in protocols
2426
"http",
@@ -64,6 +66,8 @@ func registerOutbounds(registry *outbound.Registry) {
6466

6567
// utility outbounds
6668
group.RegisterFallback(registry)
69+
group.RegisterSelector(registry)
70+
group.RegisterURLTest(registry)
6771
}
6872

6973
func registerEndpoints(registry *endpoint.Registry) {

0 commit comments

Comments
 (0)