Compare commits

...
9 Commits
Author SHA1 Message Date
lukaszraczylo 322a0b4929 Add appropriate labels to the container 2025-02-08 01:27:45 +00:00
lukaszraczylo aab31c4bce Merge remote-tracking branch 'origin/main' into testing 2025-02-08 00:56:23 +00:00
lukaszraczyloandgithub-actions[bot] 93e078971c Update go.mod and go.sum
Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
2025-02-07 03:05:38 +00:00
lukaszraczyloandgithub-actions[bot] 589f22fe33 Update go.mod and go.sum
Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
2025-02-06 03:04:42 +00:00
lukaszraczylo 68dd114da1 fixup! fixup! fixup! Staticheck fixes 2025-01-30 22:45:24 +00:00
lukaszraczylo 478a420b6b fixup! fixup! Staticheck fixes 2025-01-30 22:10:59 +00:00
lukaszraczylo d4dd3636f3 fixup! Staticheck fixes 2025-01-30 11:25:10 +00:00
lukaszraczylo fa24095b88 Staticheck fixes 2025-01-29 23:42:58 +00:00
lukaszraczylo 15d1dd7164 fixup! Add tracing and relevant tests (#21) 2025-01-29 22:15:14 +00:00
12 changed files with 51 additions and 91 deletions
+6
View File
@@ -6,3 +6,9 @@ ARG TARGETOS
COPY --chmod=777 --chown=nonroot:nonroot static/app /go/src/app COPY --chmod=777 --chown=nonroot:nonroot static/app /go/src/app
ADD dist/bot-$TARGETOS-$TARGETARCH /go/src/app/graphql-proxy ADD dist/bot-$TARGETOS-$TARGETARCH /go/src/app/graphql-proxy
ENTRYPOINT ["/go/src/app/graphql-proxy"] ENTRYPOINT ["/go/src/app/graphql-proxy"]
LABEL org.opencontainers.image.maintainer="[email protected]" \
org.opencontainers.image.authors="[email protected]" \
org.opencontainers.image.title="graphql-monitoring-proxy" \
org.opencontainers.image.description="GraphQL monitoring proxy" \
org.opencontainers.image.url="https://github.com/lukaszraczylo/graphql-monitoring-proxy"
+2
View File
@@ -351,3 +351,5 @@ graphql_proxy_cache_hit{microservice="graphql_proxy",pod="hasura-w-proxy-interna
graphql_proxy_cache_hit{pod="hasura-w-proxy-internal-6b5f4b4bbb-9xwfc",microservice="graphql_proxy"} 1 graphql_proxy_cache_hit{pod="hasura-w-proxy-internal-6b5f4b4bbb-9xwfc",microservice="graphql_proxy"} 1
graphql_proxy_cache_miss{microservice="graphql_proxy",pod="hasura-w-proxy-internal-6b5f4b4bbb-9xwfc"} 23 graphql_proxy_cache_miss{microservice="graphql_proxy",pod="hasura-w-proxy-internal-6b5f4b4bbb-9xwfc"} 23
``` ```
.
+3 -3
View File
@@ -5,7 +5,7 @@ go 1.22.7
toolchain go1.23.4 toolchain go1.23.4
require ( require (
github.com/VictoriaMetrics/metrics v1.35.1 github.com/VictoriaMetrics/metrics v1.35.2
github.com/alicebob/miniredis/v2 v2.33.0 github.com/alicebob/miniredis/v2 v2.33.0
github.com/avast/retry-go/v4 v4.6.0 github.com/avast/retry-go/v4 v4.6.0
github.com/goccy/go-json v0.10.5 github.com/goccy/go-json v0.10.5
@@ -17,7 +17,7 @@ require (
github.com/jackc/pgx/v5 v5.7.2 github.com/jackc/pgx/v5 v5.7.2
github.com/lukaszraczylo/ask v0.0.0-20240916204100-6e9ef53a62d9 github.com/lukaszraczylo/ask v0.0.0-20240916204100-6e9ef53a62d9
github.com/lukaszraczylo/go-ratecounter v0.1.12 github.com/lukaszraczylo/go-ratecounter v0.1.12
github.com/lukaszraczylo/go-simple-graphql v1.2.39 github.com/lukaszraczylo/go-simple-graphql v1.2.41
github.com/redis/go-redis/v9 v9.7.0 github.com/redis/go-redis/v9 v9.7.0
github.com/stretchr/testify v1.10.0 github.com/stretchr/testify v1.10.0
github.com/valyala/fasthttp v1.58.0 github.com/valyala/fasthttp v1.58.0
@@ -66,6 +66,6 @@ require (
golang.org/x/text v0.22.0 // indirect golang.org/x/text v0.22.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20250204164813-702378808489 // indirect google.golang.org/genproto/googleapis/api v0.0.0-20250204164813-702378808489 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20250204164813-702378808489 // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20250204164813-702378808489 // indirect
google.golang.org/protobuf v1.36.4 // indirect google.golang.org/protobuf v1.36.5 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect
) )
+6 -6
View File
@@ -1,5 +1,5 @@
github.com/VictoriaMetrics/metrics v1.35.1 h1:o84wtBKQbzLdDy14XeskkCZih6anG+veZ1SwJHFGwrU= github.com/VictoriaMetrics/metrics v1.35.2 h1:Bj6L6ExfnakZKYPpi7mGUnkJP4NGQz2v5wiChhXNyWQ=
github.com/VictoriaMetrics/metrics v1.35.1/go.mod h1:r7hveu6xMdUACXvB8TYdAj8WEsKzWB0EkpJN+RDtOf8= github.com/VictoriaMetrics/metrics v1.35.2/go.mod h1:r7hveu6xMdUACXvB8TYdAj8WEsKzWB0EkpJN+RDtOf8=
github.com/alicebob/gopher-json v0.0.0-20200520072559-a9ecdc9d1d3a h1:HbKu58rmZpUGpz5+4FfNmIU+FmZg2P3Xaj2v2bfNWmk= github.com/alicebob/gopher-json v0.0.0-20200520072559-a9ecdc9d1d3a h1:HbKu58rmZpUGpz5+4FfNmIU+FmZg2P3Xaj2v2bfNWmk=
github.com/alicebob/gopher-json v0.0.0-20200520072559-a9ecdc9d1d3a/go.mod h1:SGnFV6hVsYE877CKEZ6tDNTjaSXYUk6QqoIK6PrAtcc= github.com/alicebob/gopher-json v0.0.0-20200520072559-a9ecdc9d1d3a/go.mod h1:SGnFV6hVsYE877CKEZ6tDNTjaSXYUk6QqoIK6PrAtcc=
github.com/alicebob/miniredis/v2 v2.33.0 h1:uvTF0EDeu9RLnUEG27Db5I68ESoIxTiXbNUiji6lZrA= github.com/alicebob/miniredis/v2 v2.33.0 h1:uvTF0EDeu9RLnUEG27Db5I68ESoIxTiXbNUiji6lZrA=
@@ -66,8 +66,8 @@ github.com/lukaszraczylo/ask v0.0.0-20240916204100-6e9ef53a62d9 h1:pL8B9mjv6RPUf
github.com/lukaszraczylo/ask v0.0.0-20240916204100-6e9ef53a62d9/go.mod h1:M+UVdyqZs++xtEPrascaVmZdOMhCnxjZ2SgH+xHpR0c= github.com/lukaszraczylo/ask v0.0.0-20240916204100-6e9ef53a62d9/go.mod h1:M+UVdyqZs++xtEPrascaVmZdOMhCnxjZ2SgH+xHpR0c=
github.com/lukaszraczylo/go-ratecounter v0.1.12 h1:VO6hHYGw/Jy9JUizXf/bS0AI2QX1ueWWAWckMFVJ/w4= github.com/lukaszraczylo/go-ratecounter v0.1.12 h1:VO6hHYGw/Jy9JUizXf/bS0AI2QX1ueWWAWckMFVJ/w4=
github.com/lukaszraczylo/go-ratecounter v0.1.12/go.mod h1:TqXEOCtFJStk1i0tkipprv1kiDHGon1MVUisjSTBSKM= github.com/lukaszraczylo/go-ratecounter v0.1.12/go.mod h1:TqXEOCtFJStk1i0tkipprv1kiDHGon1MVUisjSTBSKM=
github.com/lukaszraczylo/go-simple-graphql v1.2.39 h1:cW980i4x9XFpJceUIlIJm6olG1QLwqxC755A3qQtNGM= github.com/lukaszraczylo/go-simple-graphql v1.2.41 h1:RNFEjntCsjvKA5VADdio3zid3nH0+rO9qdKJvXmRpfQ=
github.com/lukaszraczylo/go-simple-graphql v1.2.39/go.mod h1:asfydmeb6/wiPr39/ENudhgWGl7DoEa89+4Dg33oTeg= github.com/lukaszraczylo/go-simple-graphql v1.2.41/go.mod h1:i0R9B7tR025qduN4/t6ujolMBdWyiMlAppqczrnPfLc=
github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE= github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE=
github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8= github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
@@ -145,8 +145,8 @@ google.golang.org/genproto/googleapis/rpc v0.0.0-20250204164813-702378808489 h1:
google.golang.org/genproto/googleapis/rpc v0.0.0-20250204164813-702378808489/go.mod h1:8BS3B93F/U1juMFq9+EDk+qOT5CO1R9IzXxG3PTqiRk= google.golang.org/genproto/googleapis/rpc v0.0.0-20250204164813-702378808489/go.mod h1:8BS3B93F/U1juMFq9+EDk+qOT5CO1R9IzXxG3PTqiRk=
google.golang.org/grpc v1.70.0 h1:pWFv03aZoHzlRKHWicjsZytKAiYCtNS0dHbXnIdq7jQ= google.golang.org/grpc v1.70.0 h1:pWFv03aZoHzlRKHWicjsZytKAiYCtNS0dHbXnIdq7jQ=
google.golang.org/grpc v1.70.0/go.mod h1:ofIJqVKDXx/JiXrwr2IG4/zwdH9txy3IlF40RmcJSQw= google.golang.org/grpc v1.70.0/go.mod h1:ofIJqVKDXx/JiXrwr2IG4/zwdH9txy3IlF40RmcJSQw=
google.golang.org/protobuf v1.36.4 h1:6A3ZDJHn/eNqc1i+IdefRzy/9PokBTPvcqMySR7NNIM= google.golang.org/protobuf v1.36.5 h1:tPhr+woSbjfYvY6/GPufUoYizxw1cF/yFoxJ2fmpwlM=
google.golang.org/protobuf v1.36.4/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE= google.golang.org/protobuf v1.36.5/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
+6 -14
View File
@@ -282,23 +282,15 @@ func (suite *Tests) Test_parseGraphQLQuery() {
suite.Run(tt.name, func() { suite.Run(tt.name, func() {
cfg = &config{} cfg = &config{}
parseConfig() parseConfig()
ctx_headers := func() *fasthttp.RequestHeader { ctx := suite.app.AcquireCtx(&fasthttp.RequestCtx{})
h := fasthttp.RequestHeader{}
// Set headers
for k, v := range tt.suppliedQuery.headers { for k, v := range tt.suppliedQuery.headers {
h.Add(k, v) ctx.Request().Header.Add(k, v)
}
return &h
}()
ctx_request := fasthttp.Request{
Header: *ctx_headers,
} }
ctx_request.AppendBody([]byte(tt.suppliedQuery.body)) // Set body
ctx.Request().AppendBody([]byte(tt.suppliedQuery.body))
ctx := suite.app.AcquireCtx(&fasthttp.RequestCtx{
Request: ctx_request,
})
// defer func() { // defer func() {
// cfg = &config{} // cfg = &config{}
+2 -2
View File
@@ -55,9 +55,9 @@ func Benchmark_NewLogger(b *testing.B) {
for _, tt := range tests { for _, tt := range tests {
b.Run(tt.name, func(b *testing.B) { b.Run(tt.name, func(b *testing.B) {
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
got := New() logger := New()
if tt.triggers.ModLevel.Level != 0 { if tt.triggers.ModLevel.Level != 0 {
got = got.SetMinLogLevel(tt.triggers.ModLevel.Level) logger.SetMinLogLevel(tt.triggers.ModLevel.Level)
} }
} }
}) })
-25
View File
@@ -3,7 +3,6 @@ package libpack_logger
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"os"
"reflect" "reflect"
"testing" "testing"
"time" "time"
@@ -11,30 +10,6 @@ import (
"github.com/goccy/go-json" "github.com/goccy/go-json"
) )
func captureStderr(f func()) string {
originalStderr := os.Stderr
r, w, _ := os.Pipe()
os.Stderr = w
f()
w.Close()
var buf bytes.Buffer
buf.ReadFrom(r)
os.Stderr = originalStderr
return buf.String()
}
func captureStdOut(f func()) string {
originalStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
f()
w.Close()
var buf bytes.Buffer
buf.ReadFrom(r)
os.Stdout = originalStdout
return buf.String()
}
func (suite *LoggerTestSuite) Test_LogMessageString() { func (suite *LoggerTestSuite) Test_LogMessageString() {
msg := &LogMessage{ msg := &LogMessage{
Message: "test message", Message: "test message",
-1
View File
@@ -17,7 +17,6 @@ var sortedLabelKeysCache = struct {
}{} }{}
func (ms *MetricsSetup) get_metrics_name(name string, labels map[string]string) string { func (ms *MetricsSetup) get_metrics_name(name string, labels map[string]string) string {
const unknownPodName = "unknown"
var buf bytes.Buffer var buf bytes.Buffer
podName := getPodName() podName := getPodName()
+5 -6
View File
@@ -40,10 +40,9 @@ func createFasthttpClient(timeout int) *fasthttp.Client {
// proxyTheRequest handles the request proxying logic. // proxyTheRequest handles the request proxying logic.
func proxyTheRequest(c *fiber.Ctx, currentEndpoint string) error { func proxyTheRequest(c *fiber.Ctx, currentEndpoint string) error {
var span trace.Span
ctx := context.Background()
if cfg.Tracing.Enable && tracer != nil { if cfg.Tracing.Enable && tracer != nil {
var span trace.Span
spanCtx := context.Background()
// Extract trace information from header // Extract trace information from header
if traceHeader := c.Get("X-Trace-Span"); traceHeader != "" { if traceHeader := c.Get("X-Trace-Span"); traceHeader != "" {
spanInfo, err := libpack_tracing.ParseTraceHeader(traceHeader) spanInfo, err := libpack_tracing.ParseTraceHeader(traceHeader)
@@ -53,14 +52,14 @@ func proxyTheRequest(c *fiber.Ctx, currentEndpoint string) error {
Pairs: map[string]interface{}{"error": err.Error()}, Pairs: map[string]interface{}{"error": err.Error()},
}) })
} else { } else {
if spanCtx, err := tracer.ExtractSpanContext(spanInfo); err == nil { if extractedSpanCtx, err := tracer.ExtractSpanContext(spanInfo); err == nil {
ctx = trace.ContextWithSpanContext(ctx, spanCtx) spanCtx = trace.ContextWithSpanContext(spanCtx, extractedSpanCtx)
} }
} }
} }
// Start a new span // Start a new span
span, ctx = tracer.StartSpan(ctx, "proxy_request") span, _ = tracer.StartSpan(spanCtx, "proxy_request")
defer span.End() defer span.End()
} }
+9 -15
View File
@@ -87,23 +87,17 @@ func (suite *Tests) Test_proxyTheRequest() {
cfg.Server.HostGraphQLReadOnly = tt.hostRO cfg.Server.HostGraphQLReadOnly = tt.hostRO
} }
ctx_headers := func() *fasthttp.RequestHeader { ctx := suite.app.AcquireCtx(&fasthttp.RequestCtx{})
h := fasthttp.RequestHeader{}
for k, v := range tt.headers {
h.Add(k, v)
}
return &h
}()
ctx_request := fasthttp.Request{ // Set headers
Header: *ctx_headers, for k, v := range tt.headers {
ctx.Request().Header.Add(k, v)
} }
ctx_request.SetBody([]byte(tt.body))
ctx_request.SetRequestURI(tt.path) // Set body and other request properties
ctx_request.Header.SetMethod("POST") ctx.Request().SetBody([]byte(tt.body))
ctx := suite.app.AcquireCtx(&fasthttp.RequestCtx{ ctx.Request().SetRequestURI(tt.path)
Request: ctx_request, ctx.Request().Header.SetMethod("POST")
})
res := parseGraphQLQuery(ctx) res := parseGraphQLQuery(ctx)
assert.NotNil(ctx, "Fiber context is nil", tt.name) assert.NotNil(ctx, "Fiber context is nil", tt.name)
err := proxyTheRequest(ctx, res.activeEndpoint) err := proxyTheRequest(ctx, res.activeEndpoint)
+5 -13
View File
@@ -12,8 +12,6 @@ import (
sdktrace "go.opentelemetry.io/otel/sdk/trace" sdktrace "go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.21.0" semconv "go.opentelemetry.io/otel/semconv/v1.21.0"
"go.opentelemetry.io/otel/trace" "go.opentelemetry.io/otel/trace"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
) )
type TracingSetup struct { type TracingSetup struct {
@@ -27,23 +25,17 @@ type TraceSpanInfo struct {
// NewTracing creates a new tracing setup with OTLP exporter // NewTracing creates a new tracing setup with OTLP exporter
func NewTracing(ctx context.Context, endpoint string) (*TracingSetup, error) { func NewTracing(ctx context.Context, endpoint string) (*TracingSetup, error) {
if ctx == nil { if ctx.Err() != nil {
return nil, fmt.Errorf("context cannot be nil") return nil, fmt.Errorf("invalid context: %v", ctx.Err())
} }
if endpoint == "" { if endpoint == "" {
return nil, fmt.Errorf("endpoint cannot be empty") return nil, fmt.Errorf("endpoint cannot be empty")
} }
conn, err := grpc.DialContext(ctx, endpoint, exporter, err := otlptracegrpc.New(ctx,
grpc.WithTransportCredentials(insecure.NewCredentials()), otlptracegrpc.WithEndpoint(endpoint),
grpc.WithBlock(), otlptracegrpc.WithInsecure(),
grpc.WithReturnConnectionError(),
) )
if err != nil {
return nil, fmt.Errorf("failed to create gRPC connection to collector: %w", err)
}
exporter, err := otlptracegrpc.New(ctx, otlptracegrpc.WithGRPCConn(conn))
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to create trace exporter: %w", err) return nil, fmt.Errorf("failed to create trace exporter: %w", err)
} }
+5 -4
View File
@@ -65,10 +65,11 @@ func TestNewTracing(t *testing.T) {
assert.Contains(t, err.Error(), "endpoint cannot be empty") assert.Contains(t, err.Error(), "endpoint cannot be empty")
}) })
t.Run("nil context", func(t *testing.T) { t.Run("invalid context", func(t *testing.T) {
_, err := NewTracing(nil, "localhost:4317") ctx, cancel := context.WithCancel(context.Background())
assert.Error(t, err, "Expected error for nil context") cancel() // Cancel the context immediately
assert.Contains(t, err.Error(), "context cannot be nil") _, err := NewTracing(ctx, "localhost:4317")
assert.Error(t, err, "Expected error for invalid context")
}) })
} }