Skip to content

Commit

Permalink
feat: Adding dump counter
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Boutour <bob@vibioh.fr>
  • Loading branch information
ViBiOh committed Apr 3, 2024
1 parent 176580a commit 9fc647e
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
2 changes: 1 addition & 1 deletion cmd/goweb/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func main() {

appServer := server.New(config.appServer)

go appServer.Start(client.health.EndCtx(), httputils.Handler(newPort(config), client.health, recoverer.Middleware, client.telemetry.Middleware("http"), owasp.New(config.owasp).Middleware, cors.New(config.cors).Middleware))
go appServer.Start(client.health.EndCtx(), httputils.Handler(newPort(config, client), client.health, recoverer.Middleware, client.telemetry.Middleware("http"), owasp.New(config.owasp).Middleware, cors.New(config.cors).Middleware))

client.health.WaitForTermination(appServer.Done())

Expand Down
4 changes: 2 additions & 2 deletions cmd/goweb/port.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ const (
delayPath = "/delay/"
)

func newPort(config configuration) http.Handler {
func newPort(config configuration, client client) http.Handler {
mux := http.NewServeMux()

mux.Handle(helloPath, hello.Handler(config.hello))
mux.Handle(dumpPath, dump.Handler())
mux.Handle(dumpPath, dump.Handler(client.telemetry.MeterProvider()))
mux.Handle(delayPath, delay.Handler())

mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ toolchain go1.22.0
require (
github.com/ViBiOh/flags v1.5.0
github.com/ViBiOh/httputils/v4 v4.74.0
go.opentelemetry.io/otel/metric v1.24.0
)

require (
Expand All @@ -23,7 +24,6 @@ require (
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.24.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.24.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.24.0 // indirect
go.opentelemetry.io/otel/metric v1.24.0 // indirect
go.opentelemetry.io/otel/sdk v1.24.0 // indirect
go.opentelemetry.io/otel/sdk/metric v1.24.0 // indirect
go.opentelemetry.io/otel/trace v1.24.0 // indirect
Expand Down
12 changes: 10 additions & 2 deletions pkg/dump/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dump

import (
"bytes"
"context"
"fmt"
"html"
"io"
Expand All @@ -12,10 +13,15 @@ import (

"github.com/ViBiOh/httputils/v4/pkg/httperror"
"github.com/ViBiOh/httputils/v4/pkg/telemetry"
"go.opentelemetry.io/otel/metric"
)

// Handler for dump request. Should be use with net/http
func Handler() http.Handler {
func Handler(meterProvider metric.MeterProvider) http.Handler {
counter, err := meterProvider.Meter("dd.goweb").Int64Counter("dump")
if err != nil {
slog.LogAttrs(context.Background(), slog.LevelError, "create dump counter", slog.Any("error", err))
}

return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()

Expand All @@ -27,6 +33,8 @@ func Handler() http.Handler {
return
}

counter.Add(ctx, 1)

slog.LogAttrs(ctx, slog.LevelInfo, "Dump of request", slog.String("content", value))

if _, err := w.Write([]byte(html.EscapeString(value))); err != nil {
Expand Down

0 comments on commit 9fc647e

Please sign in to comment.