Skip to content

Commit

Permalink
fix log && Optimize promProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
smx-Morgan committed Oct 8, 2024
1 parent 0ebc361 commit 8fa3353
Show file tree
Hide file tree
Showing 19 changed files with 57 additions and 97 deletions.
3 changes: 2 additions & 1 deletion log/logging/logrus/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ package logrus

import (
"context"
"github.com/cloudwego/hertz/pkg/common/hlog"
"io"

"github.com/cloudwego/hertz/pkg/common/hlog"

"github.com/sirupsen/logrus"
)

Expand Down
3 changes: 2 additions & 1 deletion log/logging/logrus/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ package logrus_test

import (
"context"
"github.com/cloudwego/hertz/pkg/common/hlog"
"testing"

"github.com/cloudwego/hertz/pkg/common/hlog"

cwlogrus "github.com/cloudwego-contrib/cwgo-pkg/log/logging/logrus"

"github.com/sirupsen/logrus"
Expand Down
3 changes: 2 additions & 1 deletion log/logging/slog/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ package slog
import (
"context"
"fmt"
"github.com/cloudwego/hertz/pkg/common/hlog"
"io"
"log/slog"

"github.com/cloudwego/hertz/pkg/common/hlog"
)

const (
Expand Down
3 changes: 2 additions & 1 deletion log/logging/slog/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ import (
"bufio"
"bytes"
"context"
"github.com/cloudwego/hertz/pkg/common/hlog"
"log/slog"
"os"
"testing"

"github.com/cloudwego/hertz/pkg/common/hlog"

"github.com/stretchr/testify/assert"
)

Expand Down
3 changes: 2 additions & 1 deletion log/logging/slog/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
package slog

import (
"github.com/cloudwego/hertz/pkg/common/hlog"
"io"
"log/slog"
"os"

"github.com/cloudwego/hertz/pkg/common/hlog"
)

type Option interface {
Expand Down
3 changes: 2 additions & 1 deletion log/logging/slog/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
package slog

import (
"github.com/cloudwego/hertz/pkg/common/hlog"
"log/slog"

"github.com/cloudwego/hertz/pkg/common/hlog"
)

// Adapt log level to otelslog level
Expand Down
6 changes: 2 additions & 4 deletions log/logging/zap/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ package zap

import (
"context"
"github.com/cloudwego/hertz/pkg/common/hlog"
"io"

"github.com/cloudwego/hertz/pkg/common/hlog"

"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
Expand Down Expand Up @@ -69,9 +70,6 @@ func (l *Logger) PutExtraKeys(keys ...ExtraKey) {

func (l *Logger) Log(level hlog.Level, kvs ...interface{}) {
sugar := l.l.Sugar()
if l.config.customFields != nil {
sugar.With(l.config.customFields)
}
switch level {
case hlog.LevelTrace, hlog.LevelDebug:
sugar.Debug(kvs...)
Expand Down
3 changes: 2 additions & 1 deletion log/logging/zap/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/cloudwego/hertz/pkg/common/hlog"
"os"
"path/filepath"
"reflect"
"strings"
"testing"

"github.com/cloudwego/hertz/pkg/common/hlog"

"github.com/stretchr/testify/assert"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
Expand Down
3 changes: 2 additions & 1 deletion log/logging/zerolog/levels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
package zerolog

import (
"github.com/cloudwego/hertz/pkg/common/hlog"
"testing"

"github.com/cloudwego/hertz/pkg/common/hlog"

"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
)
Expand Down
3 changes: 2 additions & 1 deletion log/logging/zerolog/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ import (
"context"
"errors"
"fmt"
"github.com/cloudwego/hertz/pkg/common/hlog"
"io"
"os"

"github.com/cloudwego/hertz/pkg/common/hlog"

"github.com/rs/zerolog"
)

Expand Down
3 changes: 2 additions & 1 deletion log/logging/zerolog/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ import (
"bytes"
"context"
"encoding/json"
"github.com/cloudwego/hertz/pkg/common/hlog"
"testing"

"github.com/cloudwego/hertz/pkg/common/hlog"

"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/stretchr/testify/assert"
Expand Down
3 changes: 2 additions & 1 deletion log/logging/zerolog/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
package zerolog

import (
"github.com/cloudwego/hertz/pkg/common/hlog"
"io"

"github.com/cloudwego/hertz/pkg/common/hlog"

"github.com/rs/zerolog"
)

Expand Down
3 changes: 2 additions & 1 deletion log/logging/zerolog/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ package zerolog

import (
"bytes"
"github.com/cloudwego/hertz/pkg/common/hlog"
"path/filepath"
"strings"
"testing"
"time"

"github.com/cloudwego/hertz/pkg/common/hlog"

"github.com/cloudwego/hertz/pkg/common/json"
"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
Expand Down
3 changes: 2 additions & 1 deletion telemetry/provider/otelprovider/otelprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ package otelprovider

import (
"context"
"github.com/cloudwego/kitex/pkg/klog"
"time"

"github.com/cloudwego/kitex/pkg/klog"

"github.com/cloudwego/hertz/pkg/common/hlog"

"github.com/cloudwego-contrib/cwgo-pkg/telemetry/meter/global"
Expand Down
14 changes: 6 additions & 8 deletions telemetry/provider/promprovider/example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,12 @@ import (
func main() {
registry := prometheus.NewRegistry()

mux := http.NewServeMux()

provider := promprovider.NewPromProvider(":9090",
provider := promprovider.NewPromProvider(
promprovider.WithRegistry(registry),
promprovider.WithServeMux(mux),
promprovider.WithHttpServer(),
)
defer provider.Shutdown(context.Background())

provider.Serve(":9090", "/metrics-demo")

labels := []label.CwLabel{
{Key: "http_method", Value: "/test"},
Expand All @@ -56,7 +54,7 @@ func main() {
measure.Add(context.Background(), semantic.HTTPCounter, 6, labels...)
measure.Record(context.Background(), semantic.HTTPLatency, float64(time.Second.Microseconds()), labels...)

promServerResp, err := http.Get("http://localhost:9090/prometheus")
promServerResp, err := http.Get("http://localhost:9090/metrics-demo")
if err != nil {
return
}
Expand All @@ -69,8 +67,8 @@ func main() {
return
}
respStr := string(bodyBytes)
if strings.Contains(respStr, `counter{http_method="/test",path="/cwgo/provider/promProvider",statusCode="200"} 6`) &&
strings.Contains(respStr, `latency_sum{http_method="/test",path="/cwgo/provider/promProvider",statusCode="200"} 1e+06`) {
if strings.Contains(respStr, `counter{http_method="/test",http_status_code="200",path="/cwgo/provider/promProvider"} 6`) &&
strings.Contains(respStr, `latency_sum{http_method="/test",http_status_code="200",path="/cwgo/provider/promProvider"} 1e+06`) {
fmt.Print("record and counter work correctly")
}
}
41 changes: 5 additions & 36 deletions telemetry/provider/promprovider/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
package promprovider

import (
"net/http"

"github.com/prometheus/client_golang/prometheus"
)

Expand All @@ -40,13 +38,9 @@ func (fn option) apply(cfg *config) {

type config struct {
buckets []float64
serveMux *http.ServeMux
registry *prometheus.Registry

disableServer bool
path string
name string

name string
enableRPC bool
enableHTTP bool
}
Expand All @@ -63,13 +57,10 @@ func newConfig(opts []Option) *config {

func defaultConfig() *config {
return &config{
buckets: defaultBuckets,
registry: prometheus.NewRegistry(),
serveMux: http.DefaultServeMux,
disableServer: false,
enableHTTP: false,
enableRPC: false,
path: "/prometheus",
buckets: defaultBuckets,
registry: prometheus.NewRegistry(),
enableHTTP: false,
enableRPC: false,
}
}

Expand All @@ -82,28 +73,6 @@ func WithRegistry(registry *prometheus.Registry) Option {
})
}

// WithServeMux define your custom serve mux
func WithServeMux(serveMux *http.ServeMux) Option {
return option(func(cfg *config) {
if serveMux != nil {
cfg.serveMux = serveMux
}
})
}

// WithDisableServer disable prometheus server
func WithDisableServer(disable bool) Option {
return option(func(cfg *config) {
cfg.disableServer = disable
})
}

func WithPath(path string) Option {
return option(func(cfg *config) {
cfg.path = path
})
}

func WithHttpServer() Option {
return option(func(cfg *config) {
cfg.enableHTTP = true
Expand Down
45 changes: 15 additions & 30 deletions telemetry/provider/promprovider/promprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,63 +19,39 @@ package promprovider
import (
"context"
"fmt"
"log"
"net/http"

"github.com/cloudwego/hertz/pkg/common/hlog"
"github.com/prometheus/client_golang/prometheus/promhttp"

"github.com/cloudwego-contrib/cwgo-pkg/telemetry/meter/global"

"github.com/cloudwego-contrib/cwgo-pkg/telemetry/meter/metric"
"github.com/cloudwego-contrib/cwgo-pkg/telemetry/provider"
"github.com/cloudwego-contrib/cwgo-pkg/telemetry/semantic"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)

var _ provider.Provider = &promProvider{}

// promProvider Structure of promProvider, including Prometheus registry and HTTP server
type promProvider struct {
registry *prometheus.Registry
server *http.Server
}

// Shutdown Implement the Shutdown method for the Provider interface
func (p *promProvider) Shutdown(ctx context.Context) error {
// close http server
if err := p.server.Shutdown(ctx); err != nil {
return err
}

return nil
}

// NewPromProvider Initialize and return a new promProvider instance
func NewPromProvider(addr string, opts ...Option) *promProvider {
func NewPromProvider(opts ...Option) *promProvider {
cfg := newConfig(opts)
registry := cfg.registry
if registry == nil {
registry = prometheus.NewRegistry()
}
server := &http.Server{
Addr: addr,
}
if cfg.serveMux != nil {
cfg.serveMux = http.DefaultServeMux
}
if !cfg.disableServer {
cfg.serveMux.Handle(cfg.path, promhttp.HandlerFor(registry, promhttp.HandlerOpts{
ErrorHandling: promhttp.ContinueOnError,
Registry: registry,
}))
server.Handler = cfg.serveMux
go func() {
if err := server.ListenAndServe(); err != nil {
log.Fatalf("HTTP server ListenAndServe: %v", err)
return
}
}()
}

var measure metric.Measure
var metrics []metric.Option
if cfg.enableRPC {
Expand Down Expand Up @@ -118,7 +94,6 @@ func NewPromProvider(addr string, opts ...Option) *promProvider {
)
}
if cfg.enableHTTP {

HttpCounterVec := prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: buildName(cfg.name, semantic.Counter),
Expand Down Expand Up @@ -153,10 +128,20 @@ func NewPromProvider(addr string, opts ...Option) *promProvider {

return &promProvider{
registry: registry,
server: server,
}
}

func (p *promProvider) Serve(addr, path string) {
http.Handle(path, promhttp.HandlerFor(p.registry, promhttp.HandlerOpts{
ErrorHandling: promhttp.ContinueOnError,
}))
go func() {
if err := http.ListenAndServe(addr, nil); err != nil {
hlog.Fatalf("HERTZ: Unable to start a http server, err: %s", err.Error())
}
}()
}

func buildName(name, service string) string {
if name != "" {
return fmt.Sprintf("%s_%s", name, service)
Expand Down
Loading

0 comments on commit 8fa3353

Please sign in to comment.