Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use protojson.Marshal for produce json from the proto messages #540

Merged
merged 1 commit into from
Feb 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 12 additions & 14 deletions carbonserver/capability.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package carbonserver

import (
"encoding/json"
"io"
"net/http"
_ "net/http/pprof" // skipcq: GO-S2108
Expand All @@ -10,6 +9,7 @@ import (
"time"

"go.uber.org/zap"
"google.golang.org/protobuf/encoding/protojson"

"github.com/go-graphite/carbonzipper/zipper/httpHeaders"
protov3 "github.com/go-graphite/protocol/carbonapi_v3_pb"
Expand Down Expand Up @@ -83,24 +83,22 @@ func (listener *CarbonserverListener) capabilityHandler(wr http.ResponseWriter,
switch formatCode {
case jsonFormat:
contentType = httpHeaders.ContentTypeJSON
//skipcq: VET-V0008
//nolint:govet
data, _ = json.Marshal(pvResponse)
data, err = protojson.Marshal(pvResponse.ProtoReflect().Interface())
case protoV3Format:
contentType = httpHeaders.ContentTypeCarbonAPIv3PB
data, err = pvResponse.MarshalVT()
if err != nil {
accessLogger.Error("capability failed",
zap.Duration("runtime_seconds", time.Since(t0)),
zap.String("reason", err.Error()),
zap.Int("http_code", http.StatusBadRequest),
)
http.Error(wr, "Bad request (unsupported format)",
http.StatusBadRequest,
)
}
}

if err != nil {
accessLogger.Error("capability failed",
zap.Duration("runtime_seconds", time.Since(t0)),
zap.String("reason", err.Error()),
zap.Int("http_code", http.StatusBadRequest),
)
http.Error(wr, "Bad request (unsupported format)",
http.StatusBadRequest,
)
}
wr.Header().Set("Content-Type", contentType)
wr.Write(data)
} else {
Expand Down
15 changes: 5 additions & 10 deletions carbonserver/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/peer"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/encoding/protojson"
)

type findResponse struct {
Expand Down Expand Up @@ -251,23 +252,17 @@ func (listener *CarbonserverListener) findMetrics(ctx context.Context, logger *z
}
listener.populateMetricsFoundStat(expandedGlobs)

logger.Debug("will send out response",
//skipcq: VET-V0008
//nolint:govet
zap.Any("response", multiResponse),
)

switch format {
case jsonFormat:
result.contentType = httpHeaders.ContentTypeJSON
//skipcq: VET-V0008
//nolint:govet
result.data, err = json.Marshal(multiResponse)
result.data, err = protojson.Marshal(multiResponse.ProtoReflect().Interface())
case protoV3Format:
result.contentType = httpHeaders.ContentTypeCarbonAPIv3PB
result.data, err = multiResponse.MarshalVT()
}

logger.Debug("will send out response",
zap.Any("response", json.RawMessage(result.data)),
)
if err != nil {
logger.Error("find failed",
zap.Duration("runtime_seconds", time.Since(t0)),
Expand Down
7 changes: 2 additions & 5 deletions carbonserver/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package carbonserver

import (
"context"
"encoding/json"
"io"
"net/http"
_ "net/http/pprof" // skipcq: GO-S2108
Expand All @@ -18,6 +17,7 @@ import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/peer"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/encoding/protojson"
)

func (listener *CarbonserverListener) infoHandler(wr http.ResponseWriter, req *http.Request) {
Expand Down Expand Up @@ -149,12 +149,9 @@ func (listener *CarbonserverListener) infoHandler(wr http.ResponseWriter, req *h
switch formatCode {
case jsonFormat:
contentType = httpHeaders.ContentTypeJSON
//skipcq: VET-V0008
//nolint:govet
b, err = json.Marshal(response)
b, err = protojson.Marshal(response.ProtoReflect().Interface())
case protoV2Format:
contentType = httpHeaders.ContentTypeCarbonAPIv2PB

r := response.Metrics[0]
response := protov2.InfoResponse{
Name: r.Name,
Expand Down
6 changes: 2 additions & 4 deletions carbonserver/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"context"
"crypto/md5"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"math"
Expand All @@ -20,6 +19,7 @@ import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/peer"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/encoding/protojson"

"github.com/go-graphite/carbonzipper/zipper/httpHeaders"
"github.com/go-graphite/go-whisper"
Expand Down Expand Up @@ -539,9 +539,7 @@ func (listener *CarbonserverListener) prepareDataProto(ctx context.Context, logg
// We still keep old json format, because it's painful to deal with math.NaN that can occur in new format.
case jsonFormat:
contentType = "application/json"
//skipcq: VET-V0008
//nolint:govet
b, err = json.Marshal(multiv2)
b, err = protojson.Marshal(multiv2.ProtoReflect().Interface())
case protoV2Format:
contentType = httpHeaders.ContentTypeCarbonAPIv2PB
b, err = multiv2.MarshalVT()
Expand Down