-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathnode.go
More file actions
142 lines (121 loc) · 4.11 KB
/
node.go
File metadata and controls
142 lines (121 loc) · 4.11 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package victoriatraces
import (
"context"
"fmt"
"sync"
"github.com/celestiaorg/tastora/framework/docker/container"
"github.com/celestiaorg/tastora/framework/docker/internal"
"github.com/celestiaorg/tastora/framework/types"
"github.com/docker/go-connections/nat"
"go.uber.org/zap"
)
type nodeType int
func (nodeType) String() string { return "victoriatraces" }
// default port for VictoriaTraces — serves both OTLP HTTP ingest and query API.
const defaultHTTPPort = "10428"
type Config struct {
Logger *zap.Logger
DockerClient types.TastoraDockerClient
DockerNetworkID string
Image container.Image
}
type Node struct {
*container.Node
cfg Config
logger *zap.Logger
started bool
mu sync.Mutex
internalHTTPPort string
externalHTTPPort string
Internal scope
External scope
}
func New(ctx context.Context, cfg Config, testName string, index int) (*Node, error) {
img := cfg.Image
if img.Repository == "" {
img = container.NewImage("victoriametrics/victoria-traces", "latest", "")
}
log := cfg.Logger.With(zap.String("component", "victoriatraces"), zap.Int("i", index))
home := "/home/victoriatraces"
n := &Node{cfg: cfg, logger: log}
n.Node = container.NewNode(cfg.DockerNetworkID, cfg.DockerClient, testName, img, home, index, nodeType(0), log)
n.SetContainerLifecycle(container.NewLifecycle(cfg.Logger, cfg.DockerClient, n.Name()))
if err := n.CreateAndSetupVolume(ctx, n.Name()); err != nil {
return nil, err
}
n.Internal = scope{hostname: func() string { return n.Name() }, port: &n.internalHTTPPort}
n.External = scope{hostname: func() string { return "0.0.0.0" }, port: &n.externalHTTPPort}
return n, nil
}
func (n *Node) Name() string {
return fmt.Sprintf("victoriatraces-%d-%s", n.Index, internal.SanitizeDockerResourceName(n.TestName))
}
func (n *Node) HostName() string {
return internal.CondenseHostName(n.Name())
}
func (n *Node) Start(ctx context.Context) error {
n.mu.Lock()
defer n.mu.Unlock()
if n.started {
return n.StartContainer(ctx)
}
if err := n.createContainer(ctx); err != nil {
return err
}
if err := n.ContainerLifecycle.StartContainer(ctx); err != nil {
return err
}
hostPorts, err := n.ContainerLifecycle.GetHostPorts(ctx, n.internalHTTPPort+"/tcp")
if err != nil {
return err
}
n.externalHTTPPort = internal.MustExtractPort(hostPorts[0])
n.started = true
return nil
}
func (n *Node) createContainer(ctx context.Context) error {
if n.internalHTTPPort == "" {
n.internalHTTPPort = defaultHTTPPort
}
ports := nat.PortMap{
nat.Port(n.internalHTTPPort + "/tcp"): {},
}
cmd := []string{"-storageDataPath", n.HomeDir() + "/data"}
return n.CreateContainer(ctx, n.TestName, n.NetworkID, n.Image, ports, "", n.Bind(), nil, n.HostName(), cmd, nil, nil)
}
func (n *Node) GetNetworkInfo(ctx context.Context) (types.NetworkInfo, error) {
internalIP, err := internal.GetContainerInternalIP(ctx, n.DockerClient, n.ContainerLifecycle.ContainerID())
if err != nil {
return types.NetworkInfo{}, err
}
return types.NetworkInfo{
Internal: types.Network{
Hostname: n.HostName(),
IP: internalIP,
Ports: types.Ports{HTTP: n.internalHTTPPort},
},
External: types.Network{
Hostname: "0.0.0.0",
Ports: types.Ports{HTTP: n.externalHTTPPort},
},
}, nil
}
// scope provides scoped (internal/external) access to VictoriaTraces endpoints.
type scope struct {
hostname func() string
port *string
}
// IngestHTTPEndpoint returns the full OTLP HTTP ingest URL including the /v1/traces path.
// Use this for exporters (like Go's otlptracehttp with WithEndpointURL) that send to the
// URL as-is without appending any path.
func (s scope) IngestHTTPEndpoint() string {
return fmt.Sprintf("http://%s:%s/insert/opentelemetry/v1/traces", s.hostname(), *s.port)
}
// OTLPBaseEndpoint returns the OTLP base URL without the /v1/traces suffix.
// Use this for exporters (like Rust's opentelemetry-otlp) that auto-append /v1/traces.
func (s scope) OTLPBaseEndpoint() string {
return fmt.Sprintf("http://%s:%s/insert/opentelemetry", s.hostname(), *s.port)
}
func (s scope) QueryURL() string {
return fmt.Sprintf("http://%s:%s", s.hostname(), *s.port)
}