| 1 |
// Copyright 2024, Google Inc. |
| 2 |
// All rights reserved. |
| 3 |
// |
| 4 |
// Redistribution and use in source and binary forms, with or without |
| 5 |
// modification, are permitted provided that the following conditions are |
| 6 |
// met: |
| 7 |
// |
| 8 |
// * Redistributions of source code must retain the above copyright |
| 9 |
// notice, this list of conditions and the following disclaimer. |
| 10 |
// * Redistributions in binary form must reproduce the above |
| 11 |
// copyright notice, this list of conditions and the following disclaimer |
| 12 |
// in the documentation and/or other materials provided with the |
| 13 |
// distribution. |
| 14 |
// * Neither the name of Google Inc. nor the names of its |
| 15 |
// contributors may be used to endorse or promote products derived from |
| 16 |
// this software without specific prior written permission. |
| 17 |
// |
| 18 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 |
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 |
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 |
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 |
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 |
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 |
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 |
|
| 30 |
// Package grpclog in intended for internal use by generated clients only. |
| 31 |
package grpclog |
| 32 |
|
| 33 |
import ( |
| 34 |
"context" |
| 35 |
"encoding/json" |
| 36 |
"log/slog" |
| 37 |
"strings" |
| 38 |
|
| 39 |
"google.golang.org/grpc/metadata" |
| 40 |
"google.golang.org/protobuf/encoding/protojson" |
| 41 |
"google.golang.org/protobuf/proto" |
| 42 |
) |
| 43 |
|
| 44 |
// ProtoMessageRequest returns a lazily evaluated [slog.LogValuer] for |
| 45 |
// the provided message. The context is used to extract outgoing headers. |
| 46 |
func ProtoMessageRequest(ctx context.Context, msg proto.Message) slog.LogValuer { |
| 47 |
return &protoMessage{ctx: ctx, msg: msg} |
| 48 |
} |
| 49 |
|
| 50 |
// ProtoMessageResponse returns a lazily evaluated [slog.LogValuer] for |
| 51 |
// the provided message. |
| 52 |
func ProtoMessageResponse(msg proto.Message) slog.LogValuer { |
| 53 |
return &protoMessage{msg: msg} |
| 54 |
} |
| 55 |
|
| 56 |
type protoMessage struct { |
| 57 |
ctx context.Context |
| 58 |
msg proto.Message |
| 59 |
} |
| 60 |
|
| 61 |
func (m *protoMessage) LogValue() slog.Value { |
| 62 |
if m == nil || m.msg == nil { |
| 63 |
return slog.Value{} |
| 64 |
} |
| 65 |
|
| 66 |
var groupValueAttrs []slog.Attr |
| 67 |
|
| 68 |
if m.ctx != nil { |
| 69 |
var headerAttr []slog.Attr |
| 70 |
if m, ok := metadata.FromOutgoingContext(m.ctx); ok { |
| 71 |
for k, v := range m { |
| 72 |
headerAttr = append(headerAttr, slog.String(k, strings.Join(v, ","))) |
| 73 |
} |
| 74 |
} |
| 75 |
if len(headerAttr) > 0 { |
| 76 |
groupValueAttrs = append(groupValueAttrs, slog.Any("headers", headerAttr)) |
| 77 |
} |
| 78 |
} |
| 79 |
mo := protojson.MarshalOptions{AllowPartial: true, UseEnumNumbers: true} |
| 80 |
if b, err := mo.Marshal(m.msg); err == nil { |
| 81 |
var m map[string]any |
| 82 |
if err := json.Unmarshal(b, &m); err == nil { |
| 83 |
groupValueAttrs = append(groupValueAttrs, slog.Any("payload", m)) |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
return slog.GroupValue(groupValueAttrs...) |
| 88 |
} |