Use otelgrpc for tracing grpc server and client (#15237)

* Use otelgrpc for tracing grpc server and client.

* Changelog fragment

* gofmt

* Use context in prometheus service

* Remove async start of prometheus service

* Use random port to reduce the probability of concurrent tests using the same port

* Remove comment

* fix lint error

---------

Co-authored-by: Bastin <bastin.m@proton.me>
This commit is contained in:
Preston Van Loon
2025-05-05 13:46:33 -05:00
committed by GitHub
parent 6df476835c
commit 97a95dddfc
14 changed files with 113 additions and 93 deletions

View File

@@ -3,6 +3,7 @@ package prometheus_test
import (
"fmt"
"io"
"math/rand"
"net/http"
"strconv"
"strings"
@@ -15,8 +16,6 @@ import (
log "github.com/sirupsen/logrus"
)
const addr = "127.0.0.1:8989"
type logger interface {
Info(args ...interface{})
Warn(args ...interface{})
@@ -24,10 +23,11 @@ type logger interface {
}
func TestLogrusCollector(t *testing.T) {
service := prometheus.NewService(addr, nil)
addr := fmt.Sprintf("0.0.0.0:%d", 1000+rand.Intn(1000))
service := prometheus.NewService(t.Context(), addr, nil)
hook := prometheus.NewLogrusCollector()
log.AddHook(hook)
go service.Start()
service.Start()
defer func() {
err := service.Stop()
require.NoError(t, err)
@@ -60,8 +60,8 @@ func TestLogrusCollector(t *testing.T) {
}
logExampleMessage(log.StandardLogger(), tt.level)
}
time.Sleep(time.Millisecond)
metrics := metrics(t)
time.Sleep(time.Second)
metrics := metrics(t, addr)
count := valueFor(t, metrics, prefix, tt.level)
if count != tt.want {
t.Errorf("Expecting %d and receive %d", tt.want, count)
@@ -70,7 +70,7 @@ func TestLogrusCollector(t *testing.T) {
}
}
func metrics(t *testing.T) []string {
func metrics(t *testing.T, addr string) []string {
resp, err := http.Get(fmt.Sprintf("http://%s/metrics", addr))
require.NoError(t, err)
body, err := io.ReadAll(resp.Body)

View File

@@ -23,6 +23,7 @@ var log = logrus.WithField("prefix", "prometheus")
// Service provides Prometheus metrics via the /metrics route. This route will
// show all the metrics registered with the Prometheus DefaultRegisterer.
type Service struct {
ctx context.Context
server *http.Server
svcRegistry *runtime.ServiceRegistry
failStatus error
@@ -36,8 +37,8 @@ type Handler struct {
// NewService sets up a new instance for a given address host:port.
// An empty host will match with any IP so an address like ":2121" is perfectly acceptable.
func NewService(addr string, svcRegistry *runtime.ServiceRegistry, additionalHandlers ...Handler) *Service {
s := &Service{svcRegistry: svcRegistry}
func NewService(ctx context.Context, addr string, svcRegistry *runtime.ServiceRegistry, additionalHandlers ...Handler) *Service {
s := &Service{ctx: ctx, svcRegistry: svcRegistry}
mux := http.NewServeMux()
mux.Handle("/metrics", promhttp.HandlerFor(prometheus.DefaultGatherer, promhttp.HandlerOpts{
@@ -148,7 +149,7 @@ func (s *Service) Start() {
// Stop the service gracefully.
func (s *Service) Stop() error {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
ctx, cancel := context.WithTimeout(s.ctx, 2*time.Second)
defer cancel()
return s.server.Shutdown(ctx)
}

View File

@@ -2,7 +2,9 @@ package prometheus
import (
"errors"
"fmt"
"io"
"math/rand"
"net/http"
"net/http/httptest"
"strings"
@@ -21,13 +23,14 @@ func init() {
}
func TestLifecycle(t *testing.T) {
prometheusService := NewService(":2112", nil)
port := 1000 + rand.Intn(1000)
prometheusService := NewService(t.Context(), fmt.Sprintf(":%d", port), nil)
prometheusService.Start()
// Give service time to start.
time.Sleep(time.Second)
// Query the service to ensure it really started.
resp, err := http.Get("http://localhost:2112/metrics")
resp, err := http.Get(fmt.Sprintf("http://localhost:%d/metrics", port))
require.NoError(t, err)
assert.NotEqual(t, uint64(0), resp.ContentLength, "Unexpected content length 0")
@@ -37,7 +40,7 @@ func TestLifecycle(t *testing.T) {
time.Sleep(time.Second)
// Query the service to ensure it really stopped.
_, err = http.Get("http://localhost:2112/metrics")
_, err = http.Get(fmt.Sprintf("http://localhost:%d/metrics", port))
assert.NotNil(t, err, "Service still running after Stop()")
}
@@ -60,7 +63,7 @@ func TestHealthz(t *testing.T) {
registry := runtime.NewServiceRegistry()
m := &mockService{}
require.NoError(t, registry.RegisterService(m), "Failed to register service")
s := NewService("" /*addr*/, registry)
s := NewService(t.Context(), "" /*addr*/, registry)
req, err := http.NewRequest("GET", "/healthz", nil /*reader*/)
require.NoError(t, err)
@@ -112,7 +115,7 @@ func TestContentNegotiation(t *testing.T) {
registry := runtime.NewServiceRegistry()
m := &mockService{}
require.NoError(t, registry.RegisterService(m), "Failed to register service")
s := NewService("", registry)
s := NewService(t.Context(), "", registry)
req, err := http.NewRequest("GET", "/healthz", nil /* body */)
require.NoError(t, err)
@@ -143,7 +146,7 @@ func TestContentNegotiation(t *testing.T) {
m := &mockService{}
m.status = errors.New("something is wrong")
require.NoError(t, registry.RegisterService(m), "Failed to register service")
s := NewService("", registry)
s := NewService(t.Context(), "", registry)
req, err := http.NewRequest("GET", "/healthz", nil /* body */)
require.NoError(t, err)