Skip to content

Commit

Permalink
Design telemetry
Browse files Browse the repository at this point in the history
  • Loading branch information
googollee committed Jul 27, 2023
1 parent 2f224a7 commit 42b6537
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions examples/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package main

import (
"context"
"net/http"
"os"

"github.com/googollee/go-espresso"
"github.com/googollee/go-espresso/logger"
"github.com/googollee/go-espresso/prometheus"
"google.golang.org/api/tracing/v2"
)

type Data struct{}

type Service struct {
logger logger.Logger
metric metric.Int
tracer tracing.Tracer
}

func (s *Service) HandleCreate(ctx espresso.Context[Data]) error {
if err := ctx.Endpoint(http.MethodPost, "/service").End(); err != nil {
return espresso.ErrWithStatus(http.StatusBadRequest, err)
}

return espresso.Procedure(ctx, s.Create)
}

func (s *Service) Create(ctx context.Context, arg int) (string, error) {
s.metric.Add(1)

ctx, span := s.tracer.Start(ctx)
defer span.End()

s.logger.Info(ctx, "in create")
s.logger.Debug(ctx, "input", "arg", arg)

req := http.NewRequest()
s.tracer.Inject(ctx, req.Header)
resp := http.DefaultClient.Do(req)

return "", nil
}

func main() {
svc := &Service{}

eng := espresso.NewEngine(
logger.New(os.Stderr, logger.DEBUG),
prometheus.New("/metrics"),
tracing.New("https://url"),
)

svc.logger = eng.Logger()
svc.metric = prometheus.IntGauge(eng, "calls")
svc.tracer = eng.Tracer()

eng.HandleAll(svc)

eng.ListenAndServe(":8080")
}

0 comments on commit 42b6537

Please sign in to comment.