Skip to content

Commit

Permalink
add basic http metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
negrel committed Apr 26, 2024
1 parent c23fe25 commit a7e23db
Show file tree
Hide file tree
Showing 17 changed files with 187 additions and 43 deletions.
6 changes: 5 additions & 1 deletion cmd/server/full/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import (
)

// ProvideSetup is a wire provider that performs setup of full server.
func ProvideSetup(logger zerolog.Logger, cli grafanaCli.Client, grafanaService grafana.Service) wired.Setup {
func ProvideSetup(
logger zerolog.Logger,
cli grafanaCli.Client,
grafanaService grafana.Service,
) wired.Setup {
grafanaCli.WaitHealthy(logger, cli, 5)
logger.Info().Msg("setting up grafana datasource and dashboards...")
{
Expand Down
3 changes: 3 additions & 0 deletions cmd/server/full/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func Initialize(logger wired.BootstrapLogger) wired.App {
middlewares.ProvideErrorHandler,
middlewares.ProvideEventsCors,
middlewares.ProvideEventsRateLimiter,
middlewares.ProvideMetrics,
middlewares.ProvideNonRegisteredOriginFilter,
middlewares.ProvideRequestId,
middlewares.ProvideStatic,
Expand All @@ -49,6 +50,8 @@ func Initialize(logger wired.BootstrapLogger) wired.App {
wired.ProvideLogger,
wired.ProvideMinimalFiber,
wired.ProvideMinimalFiberConfig,
wired.ProvidePromHttpLogger,
wired.ProvidePrometheusRegistry,
wired.ProvideServerConfig,
)
return wired.App{}
Expand Down
11 changes: 7 additions & 4 deletions cmd/server/full/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions cmd/server/ingestion/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func Initialize(logger wired.BootstrapLogger) wired.App {
middlewares.ProvideErrorHandler,
middlewares.ProvideEventsCors,
middlewares.ProvideEventsRateLimiter,
middlewares.ProvideMetrics,
middlewares.ProvideNonRegisteredOriginFilter,
middlewares.ProvideRequestId,
middlewares.ProvideStatic,
Expand All @@ -43,6 +44,8 @@ func Initialize(logger wired.BootstrapLogger) wired.App {
wired.ProvideLogger,
wired.ProvideMinimalFiber,
wired.ProvideMinimalFiberConfig,
wired.ProvidePromHttpLogger,
wired.ProvidePrometheusRegistry,
wired.ProvideServerConfig,
wired.ProvideSetup,
)
Expand Down
11 changes: 7 additions & 4 deletions cmd/server/ingestion/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 18 additions & 10 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import (
"os"
"os/signal"
"syscall"
"time"

"github.com/prismelabs/analytics/cmd/server/full"
"github.com/prismelabs/analytics/cmd/server/ingestion"
"github.com/prismelabs/analytics/pkg/config"
"github.com/prismelabs/analytics/pkg/log"
"github.com/prismelabs/analytics/pkg/wired"
"github.com/prometheus/client_golang/prometheus/promhttp"
)

func main() {
Expand All @@ -39,16 +41,22 @@ func main() {
app.Logger.Panic().Str("mode", mode).Msg("unknown server mode")
}

// Profiling server.
if app.Config.ProfilerHostPort != "" {
go func() {
app.Logger.Info().Msgf("profiler server listening for incoming request on http://%v", app.Config.ProfilerHostPort)
err := http.ListenAndServe(app.Config.ProfilerHostPort, nil)
app.Logger.Panic().Err(err).Msg("failed to start profiler server")
}()
} else {
app.Logger.Info().Msgf("profiling server disabled")
}
// Admin and profiling server.
go func() {
http.Handle("/metrics", promhttp.HandlerFor(app.PromRegistry, promhttp.HandlerOpts{
ErrorLog: &app.Logger,
ErrorHandling: promhttp.HTTPErrorOnError,
Registry: app.PromRegistry,
DisableCompression: false,
MaxRequestsInFlight: 0,
Timeout: 3 * time.Second,
EnableOpenMetrics: false,
ProcessStartTime: time.Now(),
}))
app.Logger.Info().Msgf("admin server listening for incoming request on http://%v", app.Config.AdminHostPort)
err := http.ListenAndServe(app.Config.AdminHostPort, nil)
app.Logger.Panic().Err(err).Msg("failed to start admin server")
}()

go func() {
socket := "0.0.0.0:" + fmt.Sprint(app.Config.Port)
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ services:
- "./:/app:ro"
ports:
- "8000:8000"
- "6060:6060"
- "9090:9090"
networks:
default:
aliases:
Expand Down
2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
default = pkgs.buildGoModule {
pname = "prisme";
version = "0.14.0";
vendorHash = "sha256-UbZ/acqMyPvVuwoo9XshWk02JpATj6t/ZrEGtzpXPfc=";
vendorHash = "sha256-FgDj0jsFio2EFE1Uq7A6X9G0ulEfkByV1IwzGdS7kng=";

src = ./.;
# Skip go test.
Expand Down
12 changes: 10 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
github.com/google/wire v0.6.0
github.com/negrel/ringo v0.5.0
github.com/oschwald/maxminddb-golang v1.12.0
github.com/prometheus/client_golang v1.19.0
github.com/rs/zerolog v1.32.0
github.com/stretchr/testify v1.9.0
github.com/tidwall/gjson v1.17.1
Expand All @@ -21,20 +22,27 @@ require (

require (
github.com/ClickHouse/ch-go v0.61.5 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/go-faster/city v1.0.1 // indirect
github.com/go-faster/errors v0.7.1 // indirect
github.com/hashicorp/golang-lru v1.0.2 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/paulmach/orb v0.11.1 // indirect
github.com/philhofer/fwd v1.1.2 // indirect
github.com/pierrec/lz4/v4 v4.1.21 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.48.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
github.com/segmentio/asm v1.2.0 // indirect
github.com/shopspring/decimal v1.4.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
github.com/tinylib/msgp v1.1.9 // indirect
go.opentelemetry.io/otel v1.26.0 // indirect
go.opentelemetry.io/otel/trace v1.26.0 // indirect
go.opentelemetry.io/otel v1.24.0 // indirect
go.opentelemetry.io/otel/trace v1.24.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)

Expand Down
32 changes: 23 additions & 9 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migc
github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5+sAH+4kjUM=
github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1U3M=
github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cloudflare/golz4 v0.0.0-20150217214814-ef862a3cdc58 h1:F1EaeKL/ta07PY/k9Os/UFtwERei2/XzGemhpGnBKNg=
github.com/cloudflare/golz4 v0.0.0-20150217214814-ef862a3cdc58/go.mod h1:EOBUe0h4xcZ5GoxqC5SDxFQ8gwyZPKQoEzownBlhI80=
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down Expand Up @@ -74,8 +77,9 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
Expand Down Expand Up @@ -112,11 +116,19 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_golang v1.19.0 h1:ygXvpU1AoN1MhdzckN+PyD9QJOSD4x7kmXYlnfbA6JU=
github.com/prometheus/client_golang v1.19.0/go.mod h1:ZRM9uEAypZakd+q/x7+gmsvXdURP+DABIEIjnmDdp+k=
github.com/prometheus/client_model v0.5.0 h1:VQw1hfvPvk3Uv6Qf29VrPF32JB6rtbgI6cYPYQjL0Qw=
github.com/prometheus/client_model v0.5.0/go.mod h1:dTiFglRmd66nLR9Pv9f0mZi7B7fk5Pm3gvsjB5tr+kI=
github.com/prometheus/common v0.48.0 h1:QO8U2CdOzSn1BBsmXJXduaaW+dY/5QLjfB8svtSzKKE=
github.com/prometheus/common v0.48.0/go.mod h1:0/KsvlIEfPQCQ5I2iNSAWKPZziNCvRs5EC6ILDTlAPc=
github.com/prometheus/procfs v0.12.0 h1:jluTpSng7V9hY0O2R9DzzJHYb2xULk9VTR1V1R/k6Bo=
github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3cnaOZAZEfOo=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.32.0 h1:keLypqrlIjaFsbmJOBdB/qvyF8KEtCWHwobLp5l/mQ0=
github.com/rs/zerolog v1.32.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
Expand Down Expand Up @@ -156,12 +168,12 @@ github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5t
go.mongodb.org/mongo-driver v1.11.4/go.mod h1:PTSz5yu21bkT/wXpkS7WR5f0ddqw5quethTUn9WM+2g=
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.45.0 h1:x8Z78aZx8cOF0+Kkazoc7lwUNMGy0LrzEMxTm4BbTxg=
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.45.0/go.mod h1:62CPTSry9QZtOaSsE3tOzhx6LzDhHnXJ6xHeMNNiM6Q=
go.opentelemetry.io/otel v1.26.0 h1:LQwgL5s/1W7YiiRwxf03QGnWLb2HW4pLiAhaA5cZXBs=
go.opentelemetry.io/otel v1.26.0/go.mod h1:UmLkJHUAidDval2EICqBMbnAd0/m2vmpf/dAM+fvFs4=
go.opentelemetry.io/otel/metric v1.26.0 h1:7S39CLuY5Jgg9CrnA9HHiEjGMF/X2VHvoXGgSllRz30=
go.opentelemetry.io/otel/metric v1.26.0/go.mod h1:SY+rHOI4cEawI9a7N1A4nIg/nTQXe1ccCNWYOJUrpX4=
go.opentelemetry.io/otel/trace v1.26.0 h1:1ieeAUb4y0TE26jUFrCIXKpTuVK7uJGN9/Z/2LP5sQA=
go.opentelemetry.io/otel/trace v1.26.0/go.mod h1:4iDxvGDQuUkHve82hJJ8UqrwswHYsZuWCBllGV2U2y0=
go.opentelemetry.io/otel v1.24.0 h1:0LAOdjNmQeSTzGBzduGe/rU4tZhMwL5rWgtp9Ku5Jfo=
go.opentelemetry.io/otel v1.24.0/go.mod h1:W7b9Ozg4nkF5tWI5zsXkaKKDjdVjpD4oAt9Qi/MArHo=
go.opentelemetry.io/otel/metric v1.24.0 h1:6EhoGWWK28x1fbpA4tYTOWBkPefTDQnb8WSGXlc88kI=
go.opentelemetry.io/otel/metric v1.24.0/go.mod h1:VYhLe1rFfxuTXLgj4CBiyz+9WYBA8pNGJgDcSFRKBco=
go.opentelemetry.io/otel/trace v1.24.0 h1:CsKnnL4dUAr/0llH9FKuc698G04IrpWV0MQA/Y1YELI=
go.opentelemetry.io/otel/trace v1.24.0/go.mod h1:HPc3Xr/cOApsBI154IU0OI0HJexz+aw5uPdbs3UCjNU=
go.uber.org/atomic v1.11.0 h1:ZvwS0R+56ePWxUNi+Atn9dWONBPp/AUETXlHW0DxSjE=
go.uber.org/atomic v1.11.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
Expand Down Expand Up @@ -242,6 +254,8 @@ golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8T
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
Expand Down
16 changes: 8 additions & 8 deletions pkg/config/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ type Server struct {
TrustProxy bool
// X-Forwarded-For proxy header.
ProxyHeader string
// host:port address used by profiler http server.
ProfilerHostPort string
// host:port address of admin http server.
AdminHostPort string
}

// ServerFromEnv loads server related options from environment variables.
func ServerFromEnv() Server {
return Server{
AccessLog: GetEnvOrDefault("PRISME_ACCESS_LOG", "/dev/stdout"),
Debug: GetEnvOrDefault("PRISME_DEBUG", "false") != "false",
Port: uint16(ParseUintEnvOrDefault("PRISME_PORT", 80, 16)),
TrustProxy: GetEnvOrDefault("PRISME_TRUST_PROXY", "false") != "false",
ProxyHeader: GetEnvOrDefault("PRISME_PROXY_HEADER", "X-Forwarded-For"),
ProfilerHostPort: GetEnvOrDefault("PRISME_PROFILER_HOSTPORT", ""),
AccessLog: GetEnvOrDefault("PRISME_ACCESS_LOG", "/dev/stdout"),
Debug: GetEnvOrDefault("PRISME_DEBUG", "false") != "false",
Port: uint16(ParseUintEnvOrDefault("PRISME_PORT", 80, 16)),
TrustProxy: GetEnvOrDefault("PRISME_TRUST_PROXY", "false") != "false",
ProxyHeader: GetEnvOrDefault("PRISME_PROXY_HEADER", "X-Forwarded-For"),
AdminHostPort: GetEnvOrDefault("PRISME_ADMIN_HOSTPORT", "0.0.0.0:9090"),
}
}
Loading

0 comments on commit a7e23db

Please sign in to comment.