74 lines
1.7 KiB
Go
74 lines
1.7 KiB
Go
// This template contains a simple
|
|
// app with OTEL bootstrap that will create an
|
|
// HTTP server configured by environment that exports
|
|
// spans and metrics to an OTEL collector if configured
|
|
// to do so. Will also stand up a prometheus metrics
|
|
// endpoint.
|
|
//
|
|
// Configuration and logger stored in context
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"os/signal"
|
|
"time"
|
|
|
|
"github.com/rs/zerolog"
|
|
"go.opentelemetry.io/otel"
|
|
"go.opentelemetry.io/otel/codes"
|
|
"golang.org/x/sys/unix"
|
|
|
|
"gitea.libretechconsulting.com/rmcguire/go-http-server-with-otel/pkg/config"
|
|
"gitea.libretechconsulting.com/rmcguire/go-http-server-with-otel/pkg/logging"
|
|
"gitea.libretechconsulting.com/rmcguire/go-http-server-with-otel/pkg/observability"
|
|
)
|
|
|
|
func main() {
|
|
ctx, cncl := signal.NotifyContext(context.Background(), os.Interrupt, unix.SIGTERM)
|
|
defer cncl()
|
|
|
|
// Set up app config and logging
|
|
ctx, err := config.LoadConfig(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
cfg := config.MustFromCtx(ctx)
|
|
ctx = logging.MustInitLogging(ctx)
|
|
l := zerolog.Ctx(ctx)
|
|
|
|
// Set up OTEL
|
|
opts := make([]observability.Option, 0)
|
|
if cfg.Logging.Level == "trace" {
|
|
opts = append(opts, observability.EnableStdoutExporter)
|
|
}
|
|
shutdown := observability.Init(ctx, opts...)
|
|
defer func() {
|
|
shutdownCtx, cncl := context.WithTimeout(context.Background(), 30*time.Second)
|
|
defer cncl()
|
|
shutdown(shutdownCtx)
|
|
}()
|
|
|
|
// Begin Server init
|
|
tracer := otel.Tracer(cfg.Name)
|
|
_, initSpan := tracer.Start(ctx, "init")
|
|
|
|
// TODO: HTTP Server
|
|
|
|
// App Ready
|
|
l.Trace().Any("config", *cfg).Send()
|
|
l.Info().
|
|
Str("name", cfg.Name).
|
|
Str("version", cfg.Version).
|
|
Str("logLevel", cfg.Logging.Level).
|
|
Msg("app initialized")
|
|
|
|
initSpan.SetStatus(codes.Ok, "")
|
|
initSpan.End()
|
|
|
|
<-ctx.Done()
|
|
}
|
|
|
|
func init() {}
|