Skip to content

Commit e34e03c

Browse files
Handwritten elasticsearch router, support gzipped body (#486)
* Handwritten elasticsearch router, support gzipped body * Rename tls package -> xtls
1 parent 82333a1 commit e34e03c

File tree

13 files changed

+357
-80
lines changed

13 files changed

+357
-80
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ require (
2121
github.com/jackc/pgconn v1.14.1
2222
github.com/jackc/pgproto3/v2 v2.3.2
2323
github.com/jackc/pgx/v4 v4.18.1
24+
github.com/klauspost/compress v1.16.7
2425
github.com/minio/minio-go v6.0.14+incompatible
2526
github.com/prometheus/client_golang v1.11.1
2627
github.com/rjeczalik/notify v0.9.3
@@ -100,7 +101,6 @@ require (
100101
github.com/jcmturner/rpc/v2 v2.0.3 // indirect
101102
github.com/josharian/intern v1.0.0 // indirect
102103
github.com/json-iterator/go v1.1.12 // indirect
103-
github.com/klauspost/compress v1.16.7 // indirect
104104
github.com/lann/builder v0.0.0-20180802200727-47ae307949d0 // indirect
105105
github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 // indirect
106106
github.com/lib/pq v1.10.4 // indirect

metric/controller.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ const (
1111
)
1212

1313
var (
14-
SecondsBucketsDetailed = prometheus.ExponentialBuckets(0.0005, 2, 16) // covers range from 500us to 16.384s
15-
SecondsBucketsLong = prometheus.ExponentialBuckets(0.005, 2, 16) // covers range from 5ms to 163.84s
14+
SecondsBucketsDetailedNano = prometheus.ExponentialBuckets(0.000005, 2, 19) // covers range from 5ns to 1.3ms
15+
SecondsBucketsDetailed = prometheus.ExponentialBuckets(0.0005, 2, 16) // covers range from 500us to 16.384s
16+
SecondsBucketsLong = prometheus.ExponentialBuckets(0.005, 2, 16) // covers range from 5ms to 163.84s
1617
)
1718

1819
type Ctl struct {

pipeline/pipeline.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ func (p *Pipeline) registerMetrics() {
211211
p.wrongEventCRIFormatMetric = m.RegisterCounter("wrong_event_cri_format", "Wrong event CRI format counter")
212212
p.maxEventSizeExceededMetric = m.RegisterCounter("max_event_size_exceeded", "Max event size exceeded counter")
213213
p.eventPoolLatency = m.RegisterHistogram("event_pool_latency_seconds",
214-
"How long we are wait an event from the pool", metric.SecondsBucketsDetailed).
214+
"How long we are wait an event from the pool", metric.SecondsBucketsDetailedNano).
215215
WithLabelValues()
216216
}
217217

plugin/action/debug/debug.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ var (
101101
loggerByPipelineMu sync.Mutex
102102
)
103103

104+
// return shared logger between concurrent running processors
104105
func (p *Plugin) setupLogger(pipelineName string, parentLogger *zap.Logger, config *Config) {
105106
loggerByPipelineMu.Lock()
106107
defer loggerByPipelineMu.Unlock()

plugin/input/http/elasticsearch.go

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package http
22

33
import (
44
"net/http"
5-
6-
"go.uber.org/zap"
75
)
86

97
var info = []byte(`{
@@ -89,6 +87,15 @@ var xpack = []byte(`{
8987
"tagline": "You know, for nothing"
9088
}`)
9189

90+
var license = []byte(`{
91+
"license": {
92+
"mode": "basic",
93+
"status": "active",
94+
"type": "basic",
95+
"uid": "e76d6ce9-f78c-44ff-8fd5-b5877357d649"
96+
}
97+
}`)
98+
9299
var result = []byte(`{
93100
"took": 30,
94101
"errors": false,
@@ -97,35 +104,18 @@ var result = []byte(`{
97104

98105
var empty = []byte(`{}`)
99106

100-
func (p *Plugin) elasticsearch(mux *http.ServeMux) {
101-
mux.HandleFunc("/", p.serveElasticsearchInfo)
102-
mux.HandleFunc("/_xpack", p.serveElasticsearchXPack)
103-
mux.HandleFunc("/_bulk", p.serve)
104-
mux.HandleFunc("/_template/", p.serveElasticsearchTemplate)
105-
}
106-
107107
func (p *Plugin) serveElasticsearchXPack(w http.ResponseWriter, _ *http.Request) {
108-
_, err := w.Write(xpack)
109-
if err != nil {
110-
p.logger.Error("can't write response", zap.Error(err))
111-
}
112-
}
113-
114-
func (p *Plugin) serveElasticsearchTemplate(w http.ResponseWriter, _ *http.Request) {
115-
_, err := w.Write(empty)
116-
if err != nil {
117-
p.logger.Error("can't write response", zap.Error(err))
118-
}
108+
_, _ = w.Write(xpack)
119109
}
120110

121111
func (p *Plugin) serveElasticsearchInfo(w http.ResponseWriter, r *http.Request) {
122-
if r.Method == http.MethodGet && r.RequestURI == "/" {
123-
_, err := w.Write(info)
124-
if err != nil {
125-
p.logger.Error("can't write response", zap.Error(err))
126-
}
112+
if r.Method == http.MethodGet {
113+
_, _ = w.Write(info)
127114
return
128115
}
116+
// otherwise return an empty response
117+
}
129118

130-
p.logger.Error("unknown request", zap.String("uri", r.RequestURI), zap.String("method", r.Method))
119+
func (p *Plugin) serveElasticsearchLicense(w http.ResponseWriter, _ *http.Request) {
120+
_, _ = w.Write(license)
131121
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package http
2+
3+
import (
4+
"encoding/json"
5+
"net/http"
6+
"net/http/httptest"
7+
"testing"
8+
9+
"github.com/stretchr/testify/require"
10+
)
11+
12+
func TestElasticsearchResponse(t *testing.T) {
13+
t.Parallel()
14+
r := require.New(t)
15+
16+
p := Plugin{}
17+
18+
tcs := []struct {
19+
Name string
20+
Handler http.HandlerFunc
21+
Request *http.Request
22+
ExpectedBody []byte
23+
ExpectedCode int
24+
}{
25+
{
26+
Name: "info",
27+
Handler: p.serveElasticsearchInfo,
28+
Request: httptest.NewRequest(http.MethodGet, "/", http.NoBody),
29+
ExpectedBody: info,
30+
ExpectedCode: http.StatusOK,
31+
},
32+
{
33+
Name: "info head",
34+
Handler: p.serveElasticsearchInfo,
35+
Request: httptest.NewRequest(http.MethodHead, "/", http.NoBody),
36+
ExpectedBody: nil,
37+
ExpectedCode: http.StatusOK,
38+
},
39+
{
40+
Name: "xpack",
41+
Handler: p.serveElasticsearchXPack,
42+
Request: httptest.NewRequest(http.MethodGet, "/", http.NoBody),
43+
ExpectedBody: xpack,
44+
ExpectedCode: http.StatusOK,
45+
},
46+
{
47+
Name: "license",
48+
Handler: p.serveElasticsearchLicense,
49+
Request: httptest.NewRequest(http.MethodGet, "/", http.NoBody),
50+
ExpectedBody: license,
51+
ExpectedCode: http.StatusOK,
52+
},
53+
}
54+
55+
for _, tc := range tcs {
56+
tc := tc
57+
t.Run(tc.Name, func(t *testing.T) {
58+
t.Parallel()
59+
60+
rec := httptest.NewRecorder()
61+
62+
tc.Handler.ServeHTTP(rec, tc.Request)
63+
64+
r.Equal(http.StatusOK, rec.Code)
65+
66+
body := rec.Body.Bytes()
67+
r.Equal(tc.ExpectedBody, body)
68+
69+
// check body is valid json
70+
if len(body) > 0 {
71+
m := map[string]any{}
72+
r.NoError(json.Unmarshal(rec.Body.Bytes(), &m))
73+
}
74+
})
75+
}
76+
}

0 commit comments

Comments
 (0)