Skip to content

Instantly share code, notes, and snippets.

@mehmetcemyucel
Last active April 28, 2022 20:40
Show Gist options
  • Save mehmetcemyucel/6aaec9ea53ee4382aeb73c0379954139 to your computer and use it in GitHub Desktop.
Save mehmetcemyucel/6aaec9ea53ee4382aeb73c0379954139 to your computer and use it in GitHub Desktop.
package service
import (
"github.com/go-kit/kit/log"
kitprometheus "github.com/go-kit/kit/metrics/prometheus"
stdprometheus "github.com/prometheus/client_golang/prometheus"
"go.uber.org/zap"
"helloWorld/pkg/model/example/dto/request"
"helloWorld/pkg/model/example/entity"
"helloWorld/pkg/repository"
"os"
)
type IExampleService interface {
Check(trx request.ExampleReq) ([]entity.Example, error)
}
type ExampleService struct {
ExampleRepository repository.IExampleRepository
log *zap.Logger
}
func NewExampleService(p repository.IExampleRepository, logger *zap.Logger) IExampleService {
fieldKeys := []string{"method", "error"}
requestCount := kitprometheus.NewCounterFrom(stdprometheus.CounterOpts{
Namespace: "example_group",
Subsystem: "example_service",
Name: "request_count",
Help: "Number of requests recieved",
}, fieldKeys)
requestLatency := kitprometheus.NewSummaryFrom(stdprometheus.SummaryOpts{
Namespace: "example_group",
Subsystem: "example_service",
Name: "request_latency_microseconds",
Help: "Total duration of request in microseconds",
}, fieldKeys)
countResult := kitprometheus.NewSummaryFrom(stdprometheus.SummaryOpts{
Namespace: "example_group",
Subsystem: "example_service",
Name: "count_result",
Help: "The result of each count method",
}, []string{})
var exampleService IExampleService
exampleService = &ExampleService{p, logger}
//exampleService = ExampleLoggingMiddleware{exampleService, logger, log.NewLogfmtLogger(os.Stderr)}
exampleService = ExampleInstrumentingMiddleware{requestCount, requestLatency, countResult, exampleService}
return exampleService
}
func (s *ExampleService) Check(trx request.ExampleReq) ([]entity.Example, error) {
if examples, err := s.ExampleRepository.ActiveExamples(trx.Name); err != nil {
s.log.Error("examples cannot loaded, ", zap.Error(err))
return nil, err
} else {
return examples, err
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment