Skip to content
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,11 @@ $RECYCLE.BIN/
*.lnk

# End of https://www.toptal.com/developers/gitignore/api/go,visualstudiocode,osx,linux,windows

### Additional IDEs/Editors. ###
/.idea/
/*.iml

/.vimrc
*.swp
*.tmp
13 changes: 7 additions & 6 deletions inits.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ var now = time.Now()

func startAPM(ctx context.Context, cfg *configs.Configs) *apm.APM {
ap, err := apm.New(ctx, &apm.Options{
Debug: cfg.Environment == configs.EnvLocal,
Environment: cfg.Environment.String(),
ServiceName: cfg.AppName,
ServiceVersion: cfg.AppVersion,
TracesSampleRate: 50.00,
UseStdOut: cfg.Environment == configs.EnvLocal,
Debug: cfg.Environment == configs.EnvLocal,
Environment: cfg.Environment.String(),
ServiceName: cfg.AppName,
ServiceVersion: cfg.AppVersion,
PrometheusScrapePort: 9090,
TracesSampleRate: 50.00,
UseStdOut: cfg.Environment == configs.EnvLocal,
})
if err != nil {
panic(errors.Wrap(err, "failed to start APM"))
Expand Down
10 changes: 6 additions & 4 deletions internal/pkg/apm/apm.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/naughtygopher/errors"
"github.com/naughtygopher/goapp/internal/pkg/logger"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp"
"go.opentelemetry.io/otel/exporters/stdout/stdoutmetric"
Expand Down Expand Up @@ -70,7 +71,7 @@ func New(ctx context.Context, opts *Options) (*APM, error) {
s.meterProvider = mProvider
SetGlobal(s)

return s, nil
return Global(), nil
}

// Shutdown gracefully switch off apm, flushing any data it have
Expand Down Expand Up @@ -140,9 +141,7 @@ func SetGlobal(apm *APM) {
// Global gets global apm instance
func Global() *APM {
if global == nil {
apm, _ := New(context.Background(), &Options{UseStdOut: false})
global = apm
return apm
logger.Error(context.Background(), "APM access attempt before initialisation is a bug")
}
return global
}
Expand Down Expand Up @@ -187,6 +186,9 @@ func newTracer(ctx context.Context, opts *Options) (trace.TracerProvider, *Trace

if opts.UseStdOut {
exporter, err = stdouttrace.New()
} else if opts.CollectorURL == "" {
// Using no-op tracer as CollectorURL was not set.
return nil, nil, nil
Comment thread
bnkamalesh marked this conversation as resolved.
} else if httpCollector {
exporter, err = otlptracehttp.New(
ctx,
Expand Down
19 changes: 5 additions & 14 deletions internal/pkg/apm/prometheus.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package apm

import (
"context"
"fmt"
"net/http"
"time"

"github.com/naughtygopher/errors"
"github.com/naughtygopher/goapp/internal/pkg/logger"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.opentelemetry.io/otel/exporters/prometheus"
)
Expand All @@ -24,24 +26,13 @@ func prometheusScraper(opts *Options) {
mux.Handle("/-/metrics", promhttp.Handler())
server := &http.Server{
Handler: mux,
Addr: fmt.Sprintf("%d", opts.PrometheusScrapePort),
Addr: fmt.Sprintf(":%d", opts.PrometheusScrapePort),
ReadHeaderTimeout: 5 * time.Second,
}

// logger.Info(
// "[otel/http] starting prometheus scrape endpoint",
// zap.String(
// "addr",
// fmt.Sprintf("localhost:%d/-/metrics", opts.PrometheusScrapePort),
// ),
// )
logger.Info(context.Background(), fmt.Sprintf("[otel/http] starting prometheus metrics on :%d/-/metrics", opts.PrometheusScrapePort))
err := server.ListenAndServe()
if err != nil {
// logger.Error(
// "[otel/http] failed to serve metrics at:",
// zap.Error(err),
// zap.Uint16("port", opts.PrometheusScrapePort),
// )
return
logger.Error(context.Background(), fmt.Sprintf("[otel/http] failed to start prometheus metrics on :%d/-/metrics ; %+v", opts.PrometheusScrapePort, err))
}
}
1 change: 1 addition & 0 deletions internal/pkg/sysignals/sysignals.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func NotifyErrorOnQuit(errs chan<- error, otherSignals ...syscall.Signal) {

for signalType := range interrupt {
switch signalType {
// CAUTION: syscall.SIGTSTP is not supported on Windows and go build will fail.
case syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGTSTP:
errs <- errors.Wrapf(ErrSigQuit, "%v", signalType)
return
Expand Down
5 changes: 4 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ func main() {
}),
)

// This needs to remain after log initialisation and before server initialisation.
ap := startAPM(ctx, cfgs)

healthResponder, err := startHealthResponder(ctx, probestatus, fatalErr)
if err != nil {
panic(err)
Expand All @@ -93,7 +96,7 @@ func main() {
healthResponder,
hserver,
gserver,
startAPM(ctx, cfgs),
ap,
)
exitErr = <-fatalErr
}
4 changes: 3 additions & 1 deletion shutdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ func shutdown(
probes as much context as possible. Esepcially during the graceful shutdown period.
Hence it is recommended to setup an independent server for health checks alone.
*/
defer healthResp.Shutdown(ctx)
defer func() {
_ = healthResp.Shutdown(ctx)
}()

/*
When a server begins its shutdown process, it first signals Kubernetes (or any other prober)
Expand Down
Loading