Skip to content

Commit

Permalink
[chore] change JSON marshaler to goccy-json (#3660)
Browse files Browse the repository at this point in the history
  • Loading branch information
atoulme authored Feb 5, 2025
1 parent af567cd commit 98e4cfe
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 23 deletions.
6 changes: 2 additions & 4 deletions cmd/otel-allocator/server/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ func BenchmarkScrapeConfigsHandler(b *testing.B) {
func BenchmarkCollectorMapJSONHandler(b *testing.B) {
random := rand.New(rand.NewSource(time.Now().UnixNano())) // nolint: gosec
s := &Server{
logger: logger,
jsonMarshaller: jsonConfig,
logger: logger,
}

tests := []struct {
Expand Down Expand Up @@ -142,8 +141,7 @@ func BenchmarkCollectorMapJSONHandler(b *testing.B) {
func BenchmarkTargetItemsJSONHandler(b *testing.B) {
random := rand.New(rand.NewSource(time.Now().UnixNano())) // nolint: gosec
s := &Server{
logger: logger,
jsonMarshaller: jsonConfig,
logger: logger,
}

tests := []struct {
Expand Down
27 changes: 8 additions & 19 deletions cmd/otel-allocator/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package server
import (
"context"
"crypto/tls"
"encoding/json"
"fmt"
"net/http"
"net/http/pprof"
Expand All @@ -18,7 +17,7 @@ import (
yaml2 "github.com/ghodss/yaml"
"github.com/gin-gonic/gin"
"github.com/go-logr/logr"
jsoniter "github.com/json-iterator/go"
"github.com/goccy/go-json"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
Expand All @@ -38,14 +37,6 @@ var (
}, []string{"path"})
)

var (
jsonConfig = jsoniter.Config{
EscapeHTML: false,
MarshalFloatWith6Digits: true,
ObjectFieldMustBeSimpleString: true,
}.Froze()
)

type collectorJSON struct {
Link string `json:"_link"`
Jobs []*targetJSON `json:"targets"`
Expand All @@ -61,11 +52,10 @@ type targetJSON struct {
}

type Server struct {
logger logr.Logger
allocator allocation.Allocator
server *http.Server
httpsServer *http.Server
jsonMarshaller jsoniter.API
logger logr.Logger
allocator allocation.Allocator
server *http.Server
httpsServer *http.Server

// Use RWMutex to protect scrapeConfigResponse, since it
// will be predominantly read and only written when config
Expand Down Expand Up @@ -105,9 +95,8 @@ func (s *Server) setRouter(router *gin.Engine) {

func NewServer(log logr.Logger, allocator allocation.Allocator, listenAddr string, options ...Option) *Server {
s := &Server{
logger: log,
allocator: allocator,
jsonMarshaller: jsonConfig,
logger: log,
allocator: allocator,
}

gin.SetMode(gin.ReleaseMode)
Expand Down Expand Up @@ -312,7 +301,7 @@ func (s *Server) errorHandler(w http.ResponseWriter, err error) {

func (s *Server) jsonHandler(w http.ResponseWriter, data interface{}) {
w.Header().Set("Content-Type", "application/json")
err := s.jsonMarshaller.NewEncoder(w).Encode(data)
err := json.NewEncoder(w).Encode(data)
if err != nil {
s.logger.Error(err, "failed to encode data for http response")
}
Expand Down

0 comments on commit 98e4cfe

Please sign in to comment.