Skip to content

Commit

Permalink
feat(demo): rework
Browse files Browse the repository at this point in the history
  • Loading branch information
ernado committed Dec 10, 2023
1 parent f042ef5 commit bfc7514
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 96 deletions.
111 changes: 39 additions & 72 deletions cmd/otelfaker/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,86 +7,53 @@ import (

"github.com/go-faster/sdk/app"
"github.com/go-faster/sdk/zctx"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"go.uber.org/zap"
"golang.org/x/sync/errgroup"
)

func client(ctx context.Context, lg *zap.Logger, m *app.Metrics) error {
g, ctx := errgroup.WithContext(ctx)
mux := http.NewServeMux()
registry := prometheus.NewRegistry()
registerer := prometheus.WrapRegistererWith(prometheus.Labels{
"service_name": "client",
"service_namespace": "demo",
}, registry)

// Expose /metrics HTTP endpoint using the created custom registry.
mux.Handle("/metrics", promhttp.HandlerFor(registry, promhttp.HandlerOpts{
EnableOpenMetrics: true,
Registry: registerer,
}))
srv := &http.Server{
ReadHeaderTimeout: time.Second,
Addr: "0.0.0.0:8080",
Handler: mux,
httpTransport := otelhttp.NewTransport(http.DefaultTransport,
otelhttp.WithTracerProvider(m.TracerProvider()),
otelhttp.WithMeterProvider(m.MeterProvider()),
)
httpClient := &http.Client{
Transport: httpTransport,
Timeout: time.Second * 10,
}
g.Go(func() error {
lg.Info("server listening", zap.String("addr", srv.Addr))
defer lg.Info("server stopped")
return srv.ListenAndServe()
})
g.Go(func() error {
<-ctx.Done()
return srv.Shutdown(ctx)
})
g.Go(func() error {
httpTransport := otelhttp.NewTransport(http.DefaultTransport,
otelhttp.WithTracerProvider(m.TracerProvider()),
otelhttp.WithMeterProvider(m.MeterProvider()),
)
httpClient := &http.Client{
Transport: httpTransport,
Timeout: time.Second * 10,
}
tracer := m.TracerProvider().Tracer("client")
sendRequest := func(ctx context.Context) {
ctx, cancel := context.WithTimeout(ctx, time.Second*2)
defer cancel()
tracer := m.TracerProvider().Tracer("client")
sendRequest := func(ctx context.Context) {
ctx, cancel := context.WithTimeout(ctx, time.Second*2)
defer cancel()

ctx, span := tracer.Start(ctx, "sendRequest")
defer span.End()
ctx, span := tracer.Start(ctx, "sendRequest")
defer span.End()

req, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://server:8080/api/hello", http.NoBody)
if err != nil {
lg.Error("create request", zap.Error(err))
return
}
resp, err := httpClient.Do(req)
if err != nil {
lg.Error("send request", zap.Error(err))
return
}
_ = resp.Body.Close()

zctx.From(ctx).Info("got response",
zap.Int("status", resp.StatusCode),
zap.String("url", req.URL.String()),
)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://server:8080/api/hello", http.NoBody)
if err != nil {
lg.Error("create request", zap.Error(err))
return
}
sendRequest(ctx)
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return ctx.Err()
case <-ticker.C:
sendRequest(ctx)
}
resp, err := httpClient.Do(req)
if err != nil {
lg.Error("send request", zap.Error(err))
return
}
})
return g.Wait()
_ = resp.Body.Close()

zctx.From(ctx).Info("got response",
zap.Int("status", resp.StatusCode),
zap.String("url", req.URL.String()),
)
}
sendRequest(ctx)
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return ctx.Err()
case <-ticker.C:
sendRequest(ctx)
}
}
}
9 changes: 1 addition & 8 deletions cmd/otelfaker/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,10 @@ func server(ctx context.Context, lg *zap.Logger, m *app.Metrics) error {
})

registry := prometheus.NewRegistry()
registerer := prometheus.WrapRegistererWith(prometheus.Labels{
"service_name": "client",
"service_namespace": "demo",
}, registry)
registerer.MustRegister(
requestDurations,
)
registry.MustRegister(requestDurations)

// Expose /metrics HTTP endpoint using the created custom registry.
mux.Handle("/metrics", promhttp.HandlerFor(registry, promhttp.HandlerOpts{
Registry: registerer,
EnableOpenMetrics: true,
}))
tracer := m.TracerProvider().Tracer("server")
Expand Down
4 changes: 2 additions & 2 deletions dev/local/ch-demo/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ services:
- OTEL_EXPORTER_OTLP_TRACES_PROTOCOL=grpc
- OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=http://otelcol:4317
- OTEL_EXPORTER_PROMETHEUS_HOST=0.0.0.0
- OTEL_RESOURCE_ATTRIBUTES=service.name=api,service.namespace=demo,oteldb.faker=true
- OTEL_RESOURCE_ATTRIBUTES=service.name=client
server:
restart: always
command: ["server"]
Expand All @@ -55,7 +55,7 @@ services:
- OTEL_EXPORTER_OTLP_TRACES_PROTOCOL=grpc
- OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=http://otelcol:4317
- OTEL_EXPORTER_PROMETHEUS_HOST=0.0.0.0
- OTEL_RESOURCE_ATTRIBUTES=service.name=server,service.namespace=demo,oteldb.faker=true
- OTEL_RESOURCE_ATTRIBUTES=service.name=server

oteldb:
build:
Expand Down
17 changes: 3 additions & 14 deletions dev/local/ch-demo/otelcol.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ receivers:
prometheus:
config:
scrape_configs:
- job_name: demo
- job_name: server
scrape_interval: 1s
static_configs:
- targets:
- api:8080
- server:8080
- targets: [server:8080]
processors:
batch:
timeout: 5s
Expand All @@ -21,15 +19,6 @@ processors:
check_interval: 5s
limit_mib: 256
spike_limit_mib: 500
# normalize between prometheus and otel
resource/normalize:
attributes:
- key: service.name
from_attribute: service_name
action: insert
- key: service.namespace
from_attribute: service_namespace
action: insert

exporters:
otlp:
Expand All @@ -55,7 +44,7 @@ service:
exporters: [otlp]
metrics/scrape:
receivers: [prometheus]
processors: [resource/normalize, attributes/cleanup, batch]
processors: [batch]
exporters: [otlp]
logs:
receivers: [otlp]
Expand Down

0 comments on commit bfc7514

Please sign in to comment.