Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions examples/cmd/http-server/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package main

import (
"log"
"os"

sconfig "github.com/exoscale/stelling/config"
"github.com/exoscale/stelling/examples/config"
"github.com/exoscale/stelling/examples/server"
"github.com/exoscale/stelling/fxhttp"
"github.com/exoscale/stelling/fxlogging"
"github.com/exoscale/stelling/fxmetrics"
"github.com/exoscale/stelling/fxpprof"
"github.com/exoscale/stelling/fxsentry"
"github.com/exoscale/stelling/fxtracing"
"go.uber.org/fx"
)

func main() {
// Immediately log a line to show that we've started
// This can help debug whether something is failing to start us or whether we are
// stuck in system startup
log.Println("starting server")

// Create the object in which we'll try to load our configuration
conf := &config.Config{}

// Load the configuration:
// It will use a config file, environment variables and cli flags
if err := sconfig.Load(conf, os.Args); err != nil {
// Config failed to load or failed validation
// Error out and let the user know why
log.Fatal(err)
}

app := fx.New(createSystem(conf))

// Start the application:
// This will run until we receive a signal to shut down
// It handles its own errors
app.Run()
}

// createSystem turns the configuration into a system that can be run
// We extract this in a function, because it allows us to write tests
// that assert that it contains all necessary dependencies
func createSystem(conf *config.Config) fx.Option {
opts := fx.Options(
// Insert the configuration as is: this allows other components to reference it
fx.Supply(conf),

// Add the modules from stelling:
// These will insert constructors for the various components,
// as well as register lifecycle hooks if necessary

// fxlogging adds a *zap.Logger into the system
fxlogging.NewModule(conf),

// fxpprof starts a pprof server on its own http-server
// pprof instruments the go runtime directly, you do not have to write any more code
fxpprof.NewModule(conf),
// fxmetrics adds a *prometheus.Registry to the system that you can register
// custom metrics on
// It also starts its own http-server to expose the prometheus endpoint
// In case the system uses grpc, it will wire up middleware and expose grpc
// metrics, such as request counts
fxmetrics.NewModule(conf),
// fxtracing adds a trace.TraceProvider to the system: this can be used to create
// top-level spans
// In case the system uses grpc, middleware will be wired up that traces each request
// As always in go: the current span can be retrieved from the passed in context
fxtracing.NewModule(conf),
// fxsentry adds a *sentry.Client to the system
// It will also configure the zap DPanic level to emit a sentry
fxsentry.NewModule(conf),
// fxhttp.NewModule provides a http.Server to the system
// It is up to you to provide an http.Handler and optionally an RpcMethodMapper
// Alternatively, you can provide the fxhttp.RPCMethodMapper type directly
// if it needs additional system dependencies
fxhttp.NewModule(&conf.HttpServer, fxhttp.WithRPCMethodMapper(server.RpcMapper)),

// Insert our application components
fx.Provide(
server.NewHttpMux,
),

// Invoke functions are run in the order in which they are specified
fx.Invoke(
// Invoke the http server
fxhttp.StartHttpServer,
),
)

return opts
}
2 changes: 2 additions & 0 deletions examples/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/exoscale/stelling/config"
"github.com/exoscale/stelling/fxgrpc"
"github.com/exoscale/stelling/fxhttp"
"github.com/exoscale/stelling/fxlogging"
"github.com/exoscale/stelling/fxmetrics"
"github.com/exoscale/stelling/fxpprof"
Expand All @@ -19,6 +20,7 @@ type Config struct {
fxmetrics.Metrics
fxtracing.Tracing
fxsentry.Sentry
HttpServer fxhttp.Server

FeatureFlag bool
Mode string `default:"high" validate:"oneof=low medium high"`
Expand Down
12 changes: 7 additions & 5 deletions examples/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ require (
github.com/exoscale/multiconfig v0.0.0-20260223134601-625dc5777c41 // indirect
github.com/fatih/camelcase v1.0.0 // indirect
github.com/fatih/structs v1.1.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/gabriel-vasile/mimetype v1.4.13 // indirect
github.com/getsentry/sentry-go v0.43.0 // indirect
github.com/go-logr/logr v1.4.3 // indirect
Expand All @@ -46,15 +47,16 @@ require (
go.opentelemetry.io/auto/sdk v1.2.1 // indirect
go.opentelemetry.io/contrib/bridges/prometheus v0.66.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.66.0 // indirect
go.opentelemetry.io/otel v1.41.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.67.0 // indirect
go.opentelemetry.io/otel v1.42.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.41.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.41.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.41.0 // indirect
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.41.0 // indirect
go.opentelemetry.io/otel/metric v1.41.0 // indirect
go.opentelemetry.io/otel/sdk v1.41.0 // indirect
go.opentelemetry.io/otel/sdk/metric v1.41.0 // indirect
go.opentelemetry.io/otel/trace v1.41.0 // indirect
go.opentelemetry.io/otel/metric v1.42.0 // indirect
go.opentelemetry.io/otel/sdk v1.42.0 // indirect
go.opentelemetry.io/otel/sdk/metric v1.42.0 // indirect
go.opentelemetry.io/otel/trace v1.42.0 // indirect
go.opentelemetry.io/proto/otlp v1.9.0 // indirect
go.uber.org/dig v1.19.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
Expand Down
24 changes: 14 additions & 10 deletions examples/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ github.com/fatih/camelcase v1.0.0 h1:hxNvNX/xYBp0ovncs8WyWZrOrpBNub/JfaMvbURyft8
github.com/fatih/camelcase v1.0.0/go.mod h1:yN2Sb0lFhZJUdVvtELVWefmrXpuZESvPmqwoZc+/fpc=
github.com/fatih/structs v1.1.0 h1:Q7juDM0QtcnhCpeyLGQKyg4TOIghuNXrkL32pHAUMxo=
github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=
github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg=
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/gabriel-vasile/mimetype v1.4.13 h1:46nXokslUBsAJE/wMsp5gtO500a4F3Nkz9Ufpk2AcUM=
github.com/gabriel-vasile/mimetype v1.4.13/go.mod h1:d+9Oxyo1wTzWdyVUPMmXFvp4F9tea18J8ufA774AB3s=
github.com/getsentry/sentry-go v0.43.0 h1:XbXLpFicpo8HmBDaInk7dum18G9KSLcjZiyUKS+hLW4=
Expand Down Expand Up @@ -90,8 +92,10 @@ go.opentelemetry.io/contrib/bridges/prometheus v0.66.0 h1:nQlJkSnoq/O+z7Az1CjwM+
go.opentelemetry.io/contrib/bridges/prometheus v0.66.0/go.mod h1:U87nfzwzcfDvqTeRnI+dBHMAmHGQf9AWqvTC2dAv8as=
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.66.0 h1:w/o339tDd6Qtu3+ytwt+/jon2yjAs3Ot8Xq8pelfhSo=
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.66.0/go.mod h1:pdhNtM9C4H5fRdrnwO7NjxzQWhKSSxCHk/KluVqDVC0=
go.opentelemetry.io/otel v1.41.0 h1:YlEwVsGAlCvczDILpUXpIpPSL/VPugt7zHThEMLce1c=
go.opentelemetry.io/otel v1.41.0/go.mod h1:Yt4UwgEKeT05QbLwbyHXEwhnjxNO6D8L5PQP51/46dE=
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.67.0 h1:OyrsyzuttWTSur2qN/Lm0m2a8yqyIjUVBZcxFPuXq2o=
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.67.0/go.mod h1:C2NGBr+kAB4bk3xtMXfZ94gqFDtg/GkI7e9zqGh5Beg=
go.opentelemetry.io/otel v1.42.0 h1:lSQGzTgVR3+sgJDAU/7/ZMjN9Z+vUip7leaqBKy4sho=
go.opentelemetry.io/otel v1.42.0/go.mod h1:lJNsdRMxCUIWuMlVJWzecSMuNjE7dOYyWlqOXWkdqCc=
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.41.0 h1:VO3BL6OZXRQ1yQc8W6EVfJzINeJ35BkiHx4MYfoQf44=
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.41.0/go.mod h1:qRDnJ2nv3CQXMK2HUd9K9VtvedsPAce3S+/4LZHjX/s=
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.41.0 h1:ao6Oe+wSebTlQ1OEht7jlYTzQKE+pnx/iNywFvTbuuI=
Expand All @@ -100,14 +104,14 @@ go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.41.0 h1:mq/Qc
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.41.0/go.mod h1:yk5LXEYhsL2htyDNJbEq7fWzNEigeEdV5xBF/Y+kAv0=
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.41.0 h1:61oRQmYGMW7pXmFjPg1Muy84ndqMxQ6SH2L8fBG8fSY=
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.41.0/go.mod h1:c0z2ubK4RQL+kSDuuFu9WnuXimObon3IiKjJf4NACvU=
go.opentelemetry.io/otel/metric v1.41.0 h1:rFnDcs4gRzBcsO9tS8LCpgR0dxg4aaxWlJxCno7JlTQ=
go.opentelemetry.io/otel/metric v1.41.0/go.mod h1:xPvCwd9pU0VN8tPZYzDZV/BMj9CM9vs00GuBjeKhJps=
go.opentelemetry.io/otel/sdk v1.41.0 h1:YPIEXKmiAwkGl3Gu1huk1aYWwtpRLeskpV+wPisxBp8=
go.opentelemetry.io/otel/sdk v1.41.0/go.mod h1:ahFdU0G5y8IxglBf0QBJXgSe7agzjE4GiTJ6HT9ud90=
go.opentelemetry.io/otel/sdk/metric v1.41.0 h1:siZQIYBAUd1rlIWQT2uCxWJxcCO7q3TriaMlf08rXw8=
go.opentelemetry.io/otel/sdk/metric v1.41.0/go.mod h1:HNBuSvT7ROaGtGI50ArdRLUnvRTRGniSUZbxiWxSO8Y=
go.opentelemetry.io/otel/trace v1.41.0 h1:Vbk2co6bhj8L59ZJ6/xFTskY+tGAbOnCtQGVVa9TIN0=
go.opentelemetry.io/otel/trace v1.41.0/go.mod h1:U1NU4ULCoxeDKc09yCWdWe+3QoyweJcISEVa1RBzOis=
go.opentelemetry.io/otel/metric v1.42.0 h1:2jXG+3oZLNXEPfNmnpxKDeZsFI5o4J+nz6xUlaFdF/4=
go.opentelemetry.io/otel/metric v1.42.0/go.mod h1:RlUN/7vTU7Ao/diDkEpQpnz3/92J9ko05BIwxYa2SSI=
go.opentelemetry.io/otel/sdk v1.42.0 h1:LyC8+jqk6UJwdrI/8VydAq/hvkFKNHZVIWuslJXYsDo=
go.opentelemetry.io/otel/sdk v1.42.0/go.mod h1:rGHCAxd9DAph0joO4W6OPwxjNTYWghRWmkHuGbayMts=
go.opentelemetry.io/otel/sdk/metric v1.42.0 h1:D/1QR46Clz6ajyZ3G8SgNlTJKBdGp84q9RKCAZ3YGuA=
go.opentelemetry.io/otel/sdk/metric v1.42.0/go.mod h1:Ua6AAlDKdZ7tdvaQKfSmnFTdHx37+J4ba8MwVCYM5hc=
go.opentelemetry.io/otel/trace v1.42.0 h1:OUCgIPt+mzOnaUTpOQcBiM/PLQ/Op7oq6g4LenLmOYY=
go.opentelemetry.io/otel/trace v1.42.0/go.mod h1:f3K9S+IFqnumBkKhRJMeaZeNk9epyhnCmQh/EysQCdc=
go.opentelemetry.io/proto/otlp v1.9.0 h1:l706jCMITVouPOqEnii2fIAuO3IVGBRPV5ICjceRb/A=
go.opentelemetry.io/proto/otlp v1.9.0/go.mod h1:xE+Cx5E/eEHw+ISFkwPLwCZefwVjY+pqKg1qcK03+/4=
go.uber.org/dig v1.19.0 h1:BACLhebsYdpQ7IROQ1AGPjrXcP5dF80U3gKoFzbaq/4=
Expand Down
19 changes: 18 additions & 1 deletion examples/server/server.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package server

import pb "google.golang.org/grpc/examples/route_guide/routeguide"
import (
"net/http"

pb "google.golang.org/grpc/examples/route_guide/routeguide"
)

type RouteGuideServer struct {
pb.UnimplementedRouteGuideServer
Expand All @@ -12,3 +16,16 @@ type RouteGuideServer struct {
func NewServer() pb.RouteGuideServer {
return &RouteGuideServer{}
}

// NewHttpMux provides a handler for an http.Server
func NewHttpMux() http.Handler {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
return mux
}

func RpcMapper(req *http.Request) string {
return "get-health"
}
20 changes: 14 additions & 6 deletions examples/vendor/github.com/exoscale/stelling/fxhttp/README.md

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

Loading