|
| 1 | +// Copyright 2026 The Parca Authors |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +package reporter |
| 15 | + |
| 16 | +import ( |
| 17 | + "fmt" |
| 18 | + "strings" |
| 19 | + "time" |
| 20 | + |
| 21 | + "github.com/sirupsen/logrus" |
| 22 | + "go.opentelemetry.io/collector/pdata/plog" |
| 23 | +) |
| 24 | + |
| 25 | +// otlpSkipField is a logrus entry field name. When present and true on an |
| 26 | +// entry, OTLPLogrusHook drops the entry instead of forwarding it through |
| 27 | +// ReportLogEvents. The log streamer tags its own error/partial-success warns |
| 28 | +// with this field to break the feedback loop that would otherwise grow the |
| 29 | +// streamer's queue every time an export fails. |
| 30 | +const otlpSkipField = "otlp_skip" |
| 31 | + |
| 32 | +// OTLPLogrusHook is a logrus.Hook that ships every captured entry through the |
| 33 | +// supplied ParcaReporter as an OTLP log record. Install with |
| 34 | +// logrus.AddHook(reporter.NewOTLPLogrusHook(rep)) after the reporter is |
| 35 | +// constructed; entries logged before installation are not captured. |
| 36 | +// |
| 37 | +// Fire is non-blocking on the steady state: ReportLogEvents performs a single |
| 38 | +// non-blocking channel send into the streamer's queue. Entries are silently |
| 39 | +// dropped if the queue is saturated (accounted in the streamer's queueDrops |
| 40 | +// counter); Fire never returns a non-nil error so it cannot interfere with |
| 41 | +// the logrus call site. |
| 42 | +type OTLPLogrusHook struct { |
| 43 | + rep ParcaReporter |
| 44 | +} |
| 45 | + |
| 46 | +// NewOTLPLogrusHook returns a hook bound to rep. The hook fires on every |
| 47 | +// logrus level by default; logrus's own level filter is what actually |
| 48 | +// gates which entries reach Fire. |
| 49 | +func NewOTLPLogrusHook(rep ParcaReporter) *OTLPLogrusHook { |
| 50 | + return &OTLPLogrusHook{rep: rep} |
| 51 | +} |
| 52 | + |
| 53 | +// Levels returns logrus.AllLevels so the hook fires for every level the |
| 54 | +// underlying logger emits. Filtering by level should be done by configuring |
| 55 | +// the logger itself (e.g. --log-level). |
| 56 | +func (h *OTLPLogrusHook) Levels() []logrus.Level { |
| 57 | + return logrus.AllLevels |
| 58 | +} |
| 59 | + |
| 60 | +// Fire converts the logrus entry to a LogEvent and enqueues it via the |
| 61 | +// reporter. Entries carrying the otlp_skip=true field are dropped to break |
| 62 | +// the streamer→logrus→streamer feedback loop. |
| 63 | +func (h *OTLPLogrusHook) Fire(e *logrus.Entry) error { |
| 64 | + if v, ok := e.Data[otlpSkipField]; ok { |
| 65 | + if b, ok := v.(bool); ok && b { |
| 66 | + return nil |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + // Always emit at least `level` as a per-record attribute. |
| 71 | + attrs := make(map[string]LogAttr, len(e.Data)+1) |
| 72 | + attrs["level"] = LogAttr{Str: strings.ToUpper(e.Level.String())} |
| 73 | + for k, v := range e.Data { |
| 74 | + if k == otlpSkipField { |
| 75 | + continue |
| 76 | + } |
| 77 | + attrs[k] = toLogAttr(v) |
| 78 | + } |
| 79 | + |
| 80 | + ev := LogEvent{ |
| 81 | + TimestampNs: e.Time.UnixNano(), |
| 82 | + ObservedTimestampNs: time.Now().UnixNano(), |
| 83 | + Body: e.Message, |
| 84 | + Severity: logrusLevelToSeverity(e.Level), |
| 85 | + SeverityText: strings.ToUpper(e.Level.String()), |
| 86 | + Attributes: attrs, |
| 87 | + } |
| 88 | + |
| 89 | + // ReportLogEvents itself never returns an error in the current |
| 90 | + // implementation; even if it did we'd swallow it here to avoid |
| 91 | + // propagating into the logrus call site. |
| 92 | + _ = h.rep.ReportLogEvents([]LogEvent{ev}) |
| 93 | + return nil |
| 94 | +} |
| 95 | + |
| 96 | +// toLogAttr maps a logrus field value to the tagged LogAttr union. Integer |
| 97 | +// types are preserved; everything else is stringified via fmt.Sprint so we |
| 98 | +// don't lose information for types that the OTLP attribute set could in |
| 99 | +// principle represent (floats, bools, errors, structs) but our minimal |
| 100 | +// LogAttr does not yet model. |
| 101 | +func toLogAttr(v any) LogAttr { |
| 102 | + switch x := v.(type) { |
| 103 | + case string: |
| 104 | + return LogAttr{Str: x} |
| 105 | + case int: |
| 106 | + return LogAttr{Int: int64(x), IsInt: true} |
| 107 | + case int32: |
| 108 | + return LogAttr{Int: int64(x), IsInt: true} |
| 109 | + case int64: |
| 110 | + return LogAttr{Int: x, IsInt: true} |
| 111 | + case uint: |
| 112 | + return LogAttr{Int: int64(x), IsInt: true} |
| 113 | + case uint32: |
| 114 | + return LogAttr{Int: int64(x), IsInt: true} |
| 115 | + case uint64: |
| 116 | + return LogAttr{Int: int64(x), IsInt: true} |
| 117 | + case error: |
| 118 | + return LogAttr{Str: x.Error()} |
| 119 | + default: |
| 120 | + return LogAttr{Str: fmt.Sprint(v)} |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +// logrusLevelToSeverity maps a logrus.Level to the closest OTLP severity |
| 125 | +// number. Panic level shares Fatal since OTLP has no distinct panic bucket. |
| 126 | +func logrusLevelToSeverity(l logrus.Level) plog.SeverityNumber { |
| 127 | + switch l { |
| 128 | + case logrus.TraceLevel: |
| 129 | + return plog.SeverityNumberTrace |
| 130 | + case logrus.DebugLevel: |
| 131 | + return plog.SeverityNumberDebug |
| 132 | + case logrus.InfoLevel: |
| 133 | + return plog.SeverityNumberInfo |
| 134 | + case logrus.WarnLevel: |
| 135 | + return plog.SeverityNumberWarn |
| 136 | + case logrus.ErrorLevel: |
| 137 | + return plog.SeverityNumberError |
| 138 | + case logrus.FatalLevel, logrus.PanicLevel: |
| 139 | + return plog.SeverityNumberFatal |
| 140 | + default: |
| 141 | + return plog.SeverityNumberUnspecified |
| 142 | + } |
| 143 | +} |
0 commit comments