|
| 1 | +// Copyright 2024 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +// The googlecloud package supports telemetry (tracing, metrics and logging) using |
| 16 | +// Google Cloud services. |
| 17 | +package googlecloud |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "log/slog" |
| 22 | + |
| 23 | + "cloud.google.com/go/logging" |
| 24 | + "github.com/jba/slog/withsupport" |
| 25 | +) |
| 26 | + |
| 27 | +func newHandler(level slog.Leveler, f func(logging.Entry)) *handler { |
| 28 | + if level == nil { |
| 29 | + level = slog.LevelInfo |
| 30 | + } |
| 31 | + return &handler{ |
| 32 | + level: level, |
| 33 | + handleEntry: f, |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +type handler struct { |
| 38 | + level slog.Leveler |
| 39 | + handleEntry func(logging.Entry) |
| 40 | + goa *withsupport.GroupOrAttrs |
| 41 | +} |
| 42 | + |
| 43 | +func (h *handler) Enabled(ctx context.Context, level slog.Level) bool { |
| 44 | + return level >= h.level.Level() |
| 45 | +} |
| 46 | + |
| 47 | +func (h *handler) WithAttrs(as []slog.Attr) slog.Handler { |
| 48 | + h2 := *h |
| 49 | + h2.goa = h2.goa.WithAttrs(as) |
| 50 | + return &h2 |
| 51 | +} |
| 52 | + |
| 53 | +func (h *handler) WithGroup(name string) slog.Handler { |
| 54 | + h2 := *h |
| 55 | + h2.goa = h2.goa.WithGroup(name) |
| 56 | + return &h2 |
| 57 | +} |
| 58 | + |
| 59 | +func (h *handler) Handle(ctx context.Context, r slog.Record) error { |
| 60 | + h.handleEntry(h.recordToEntry(ctx, r)) |
| 61 | + return nil |
| 62 | +} |
| 63 | + |
| 64 | +func (h *handler) recordToEntry(ctx context.Context, r slog.Record) logging.Entry { |
| 65 | + return logging.Entry{ |
| 66 | + Timestamp: r.Time, |
| 67 | + Severity: levelToSeverity(r.Level), |
| 68 | + Payload: recordToMap(r, h.goa.Collect()), |
| 69 | + Labels: map[string]string{"module": "genkit"}, |
| 70 | + // TODO: add a monitored resource |
| 71 | + // Resource: &monitoredres.MonitoredResource{}, |
| 72 | + // TODO: add trace information from the context. |
| 73 | + // Trace: "", |
| 74 | + // SpanID: "", |
| 75 | + // TraceSampled: false, |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +func levelToSeverity(l slog.Level) logging.Severity { |
| 80 | + switch { |
| 81 | + case l < slog.LevelInfo: |
| 82 | + return logging.Debug |
| 83 | + case l == slog.LevelInfo: |
| 84 | + return logging.Info |
| 85 | + case l < slog.LevelWarn: |
| 86 | + return logging.Notice |
| 87 | + case l < slog.LevelError: |
| 88 | + return logging.Warning |
| 89 | + case l == slog.LevelError: |
| 90 | + return logging.Error |
| 91 | + case l <= slog.LevelError+4: |
| 92 | + return logging.Critical |
| 93 | + case l <= slog.LevelError+8: |
| 94 | + return logging.Alert |
| 95 | + default: |
| 96 | + return logging.Emergency |
| 97 | + } |
| 98 | +} |
| 99 | +func recordToMap(r slog.Record, goras []*withsupport.GroupOrAttrs) map[string]any { |
| 100 | + root := map[string]any{} |
| 101 | + root[slog.MessageKey] = r.Message |
| 102 | + |
| 103 | + m := root |
| 104 | + for i, gora := range goras { |
| 105 | + if gora.Group != "" { |
| 106 | + if i == len(goras)-1 && r.NumAttrs() == 0 { |
| 107 | + continue |
| 108 | + } |
| 109 | + m2 := map[string]any{} |
| 110 | + m[gora.Group] = m2 |
| 111 | + m = m2 |
| 112 | + } else { |
| 113 | + for _, a := range gora.Attrs { |
| 114 | + handleAttr(a, m) |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + r.Attrs(func(a slog.Attr) bool { |
| 119 | + handleAttr(a, m) |
| 120 | + return true |
| 121 | + }) |
| 122 | + return root |
| 123 | +} |
| 124 | + |
| 125 | +func handleAttr(a slog.Attr, m map[string]any) { |
| 126 | + if a.Equal(slog.Attr{}) { |
| 127 | + return |
| 128 | + } |
| 129 | + v := a.Value.Resolve() |
| 130 | + if v.Kind() == slog.KindGroup { |
| 131 | + gas := v.Group() |
| 132 | + if len(gas) == 0 { |
| 133 | + return |
| 134 | + } |
| 135 | + if a.Key == "" { |
| 136 | + for _, ga := range gas { |
| 137 | + handleAttr(ga, m) |
| 138 | + } |
| 139 | + } else { |
| 140 | + gm := map[string]any{} |
| 141 | + for _, ga := range gas { |
| 142 | + handleAttr(ga, gm) |
| 143 | + } |
| 144 | + m[a.Key] = gm |
| 145 | + } |
| 146 | + } else { |
| 147 | + m[a.Key] = v.Any() |
| 148 | + } |
| 149 | +} |
0 commit comments