Bumps the go-modules-updates group in /backend with 5 updates: | Package | From | To | | --- | --- | --- | | [github.com/PuerkitoBio/goquery](https://github.com/PuerkitoBio/goquery) | `1.10.0` | `1.10.1` | | [github.com/go-chi/chi/v5](https://github.com/go-chi/chi) | `5.1.0` | `5.2.0` | | [github.com/go-pkgz/rest](https://github.com/go-pkgz/rest) | `1.19.0` | `1.20.2` | | [golang.org/x/image](https://github.com/golang/image) | `0.22.0` | `0.23.0` | | [golang.org/x/net](https://github.com/golang/net) | `0.31.0` | `0.33.0` | Updates `github.com/PuerkitoBio/goquery` from 1.10.0 to 1.10.1 - [Release notes](https://github.com/PuerkitoBio/goquery/releases) - [Commits](https://github.com/PuerkitoBio/goquery/compare/v1.10.0...v1.10.1) Updates `github.com/go-chi/chi/v5` from 5.1.0 to 5.2.0 - [Release notes](https://github.com/go-chi/chi/releases) - [Changelog](https://github.com/go-chi/chi/blob/master/CHANGELOG.md) - [Commits](https://github.com/go-chi/chi/compare/v5.1.0...v5.2.0) Updates `github.com/go-pkgz/rest` from 1.19.0 to 1.20.2 - [Release notes](https://github.com/go-pkgz/rest/releases) - [Commits](https://github.com/go-pkgz/rest/compare/v1.19.0...v1.20.2) Updates `golang.org/x/image` from 0.22.0 to 0.23.0 - [Commits](https://github.com/golang/image/compare/v0.22.0...v0.23.0) Updates `golang.org/x/net` from 0.31.0 to 0.33.0 - [Commits](https://github.com/golang/net/compare/v0.31.0...v0.33.0) --- updated-dependencies: - dependency-name: github.com/PuerkitoBio/goquery dependency-type: direct:production update-type: version-update:semver-patch dependency-group: go-modules-updates - dependency-name: github.com/go-chi/chi/v5 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: go-modules-updates - dependency-name: github.com/go-pkgz/rest dependency-type: direct:production update-type: version-update:semver-minor dependency-group: go-modules-updates - dependency-name: golang.org/x/image dependency-type: direct:production update-type: version-update:semver-minor dependency-group: go-modules-updates - dependency-name: golang.org/x/net dependency-type: direct:production update-type: version-update:semver-minor dependency-group: go-modules-updates ... Signed-off-by: dependabot[bot] <support@github.com>
150 lines
3.8 KiB
Go
150 lines
3.8 KiB
Go
// Copyright 2024 Google LLC
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package metadata
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"log/slog"
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
// Code below this point is copied from github.com/googleapis/gax-go/v2/internallog
|
|
// to avoid the dependency. The compute/metadata module is used by too many
|
|
// non-client library modules that can't justify the dependency.
|
|
|
|
// The handler returned if logging is not enabled.
|
|
type noOpHandler struct{}
|
|
|
|
func (h noOpHandler) Enabled(_ context.Context, _ slog.Level) bool {
|
|
return false
|
|
}
|
|
|
|
func (h noOpHandler) Handle(_ context.Context, _ slog.Record) error {
|
|
return nil
|
|
}
|
|
|
|
func (h noOpHandler) WithAttrs(_ []slog.Attr) slog.Handler {
|
|
return h
|
|
}
|
|
|
|
func (h noOpHandler) WithGroup(_ string) slog.Handler {
|
|
return h
|
|
}
|
|
|
|
// httpRequest returns a lazily evaluated [slog.LogValuer] for a
|
|
// [http.Request] and the associated body.
|
|
func httpRequest(req *http.Request, body []byte) slog.LogValuer {
|
|
return &request{
|
|
req: req,
|
|
payload: body,
|
|
}
|
|
}
|
|
|
|
type request struct {
|
|
req *http.Request
|
|
payload []byte
|
|
}
|
|
|
|
func (r *request) LogValue() slog.Value {
|
|
if r == nil || r.req == nil {
|
|
return slog.Value{}
|
|
}
|
|
var groupValueAttrs []slog.Attr
|
|
groupValueAttrs = append(groupValueAttrs, slog.String("method", r.req.Method))
|
|
groupValueAttrs = append(groupValueAttrs, slog.String("url", r.req.URL.String()))
|
|
|
|
var headerAttr []slog.Attr
|
|
for k, val := range r.req.Header {
|
|
headerAttr = append(headerAttr, slog.String(k, strings.Join(val, ",")))
|
|
}
|
|
if len(headerAttr) > 0 {
|
|
groupValueAttrs = append(groupValueAttrs, slog.Any("headers", headerAttr))
|
|
}
|
|
|
|
if len(r.payload) > 0 {
|
|
if attr, ok := processPayload(r.payload); ok {
|
|
groupValueAttrs = append(groupValueAttrs, attr)
|
|
}
|
|
}
|
|
return slog.GroupValue(groupValueAttrs...)
|
|
}
|
|
|
|
// httpResponse returns a lazily evaluated [slog.LogValuer] for a
|
|
// [http.Response] and the associated body.
|
|
func httpResponse(resp *http.Response, body []byte) slog.LogValuer {
|
|
return &response{
|
|
resp: resp,
|
|
payload: body,
|
|
}
|
|
}
|
|
|
|
type response struct {
|
|
resp *http.Response
|
|
payload []byte
|
|
}
|
|
|
|
func (r *response) LogValue() slog.Value {
|
|
if r == nil {
|
|
return slog.Value{}
|
|
}
|
|
var groupValueAttrs []slog.Attr
|
|
groupValueAttrs = append(groupValueAttrs, slog.String("status", fmt.Sprint(r.resp.StatusCode)))
|
|
|
|
var headerAttr []slog.Attr
|
|
for k, val := range r.resp.Header {
|
|
headerAttr = append(headerAttr, slog.String(k, strings.Join(val, ",")))
|
|
}
|
|
if len(headerAttr) > 0 {
|
|
groupValueAttrs = append(groupValueAttrs, slog.Any("headers", headerAttr))
|
|
}
|
|
|
|
if len(r.payload) > 0 {
|
|
if attr, ok := processPayload(r.payload); ok {
|
|
groupValueAttrs = append(groupValueAttrs, attr)
|
|
}
|
|
}
|
|
return slog.GroupValue(groupValueAttrs...)
|
|
}
|
|
|
|
func processPayload(payload []byte) (slog.Attr, bool) {
|
|
peekChar := payload[0]
|
|
if peekChar == '{' {
|
|
// JSON object
|
|
var m map[string]any
|
|
if err := json.Unmarshal(payload, &m); err == nil {
|
|
return slog.Any("payload", m), true
|
|
}
|
|
} else if peekChar == '[' {
|
|
// JSON array
|
|
var m []any
|
|
if err := json.Unmarshal(payload, &m); err == nil {
|
|
return slog.Any("payload", m), true
|
|
}
|
|
} else {
|
|
// Everything else
|
|
buf := &bytes.Buffer{}
|
|
if err := json.Compact(buf, payload); err != nil {
|
|
// Write raw payload incase of error
|
|
buf.Write(payload)
|
|
}
|
|
return slog.String("payload", buf.String()), true
|
|
}
|
|
return slog.Attr{}, false
|
|
}
|