|
| 1 | +package zap |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "math" |
| 7 | + |
| 8 | + "go.opentelemetry.io/otel/log" |
| 9 | + "go.opentelemetry.io/otel/log/noop" |
| 10 | + "go.uber.org/zap/zapcore" |
| 11 | +) |
| 12 | + |
| 13 | +const ( |
| 14 | + bridgeName = "go.opentelemetry.io/contrib/bridge/zapcore" |
| 15 | + bridgeVersion = "0.0.1-alpha" |
| 16 | +) |
| 17 | + |
| 18 | +type OtelZapCore struct { |
| 19 | + zapcore.Core |
| 20 | + logger log.Logger |
| 21 | + attr []log.KeyValue |
| 22 | +} |
| 23 | + |
| 24 | +var ( |
| 25 | + _ zapcore.Core = (*OtelZapCore)(nil) |
| 26 | +) |
| 27 | + |
| 28 | +// this function creates a new zapcore.Core that can be used with zap.New() |
| 29 | +// this instance will translate zap logs to opentelemetry logs and export them |
| 30 | + |
| 31 | +func NewOtelZapCore(lp log.LoggerProvider, opts ...log.LoggerOption) zapcore.Core { |
| 32 | + if lp == nil { |
| 33 | + // Do not panic. |
| 34 | + lp = noop.NewLoggerProvider() |
| 35 | + } |
| 36 | + // these options |
| 37 | + return &OtelZapCore{ |
| 38 | + logger: lp.Logger(bridgeName, |
| 39 | + log.WithInstrumentationVersion(bridgeVersion), |
| 40 | + ), |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +// TODO: Enabled method being added to Logger (WIP) |
| 45 | +func (o *OtelZapCore) Enabled(zapcore.Level) bool { |
| 46 | + return true |
| 47 | + // return o.logger.Enabled() |
| 48 | +} |
| 49 | + |
| 50 | +// return new zapcore with provided attr |
| 51 | +func (o *OtelZapCore) With(fields []zapcore.Field) zapcore.Core { |
| 52 | + clone := o.clone() |
| 53 | + for _, field := range fields { |
| 54 | + clone.attr = append(clone.attr, convertAttr(field)) |
| 55 | + } |
| 56 | + return clone |
| 57 | +} |
| 58 | + |
| 59 | +// TODO |
| 60 | +func (o *OtelZapCore) Sync() error { |
| 61 | + return nil |
| 62 | +} |
| 63 | +func (o *OtelZapCore) Check(ent zapcore.Entry, ce *zapcore.CheckedEntry) *zapcore.CheckedEntry { |
| 64 | + return ce.AddCore(ent, o) |
| 65 | +} |
| 66 | + |
| 67 | +func (o *OtelZapCore) Write(ent zapcore.Entry, fields []zapcore.Field) error { |
| 68 | + // add fields to encoder |
| 69 | + for _, field := range fields { |
| 70 | + o.attr = append(o.attr, convertAttr(field)) |
| 71 | + } |
| 72 | + |
| 73 | + // we create record here to avoid heap allocation |
| 74 | + r := log.Record{} |
| 75 | + r.SetTimestamp(ent.Time) |
| 76 | + r.SetBody(log.StringValue(ent.Message)) |
| 77 | + |
| 78 | + // should confirm this |
| 79 | + sevOffset := 4*(ent.Level+2) + 1 |
| 80 | + r.SetSeverity(log.Severity(ent.Level + sevOffset)) |
| 81 | + |
| 82 | + // should map this as well |
| 83 | + r.AddAttributes(o.attr...) |
| 84 | + |
| 85 | + // need to check how to pass context here |
| 86 | + ctx := context.Background() |
| 87 | + o.logger.Emit(ctx, r) |
| 88 | + return nil |
| 89 | +} |
| 90 | + |
| 91 | +func (o *OtelZapCore) clone() *OtelZapCore { |
| 92 | + return &OtelZapCore{ |
| 93 | + logger: o.logger, |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +func convertAttr(f zapcore.Field) log.KeyValue { |
| 98 | + val := convertValue(f) |
| 99 | + return log.KeyValue{Key: f.Key, Value: val} |
| 100 | +} |
| 101 | + |
| 102 | +// this does not cover all Field types yet |
| 103 | +func convertValue(f zapcore.Field) log.Value { |
| 104 | + switch f.Type { |
| 105 | + case zapcore.ArrayMarshalerType: |
| 106 | + return log.StringValue("ArrayMarshalerType") |
| 107 | + //return log.SliceValue(f.Interface.(zapcore.ArrayMarshaler)) |
| 108 | + case zapcore.ObjectMarshalerType: |
| 109 | + return log.StringValue("ObjectMarshalerType") |
| 110 | + // f.Interface.(ObjectMarshaler)) |
| 111 | + case zapcore.InlineMarshalerType: |
| 112 | + return log.StringValue("InlineMarshalerType") |
| 113 | + //return log f.Interface.(ObjectMarshaler).MarshalLogObject(enc) |
| 114 | + case zapcore.BinaryType: |
| 115 | + return log.BytesValue(f.Interface.([]byte)) |
| 116 | + case zapcore.BoolType: |
| 117 | + return log.BoolValue(f.Integer == 1) |
| 118 | + case zapcore.ByteStringType: |
| 119 | + return log.BytesValue(f.Interface.([]byte)) |
| 120 | + case zapcore.Complex128Type: |
| 121 | + return log.StringValue("complex128type") |
| 122 | + //return log.Float64Value(f.Interface.(complex128)) |
| 123 | + case zapcore.Complex64Type: |
| 124 | + return log.StringValue("Complex64Type") |
| 125 | + //return log.Float64Value(f.Interface.(complex64)) |
| 126 | + case zapcore.DurationType: |
| 127 | + return log.Int64Value(f.Integer) // do we have log.Duration? |
| 128 | + case zapcore.Float64Type: |
| 129 | + return log.Float64Value(math.Float64frombits(uint64(f.Integer))) |
| 130 | + case zapcore.Float32Type: |
| 131 | + return log.Float64Value(math.Float64frombits(uint64(f.Integer))) |
| 132 | + case zapcore.Int64Type: |
| 133 | + return log.Int64Value(f.Integer) |
| 134 | + case zapcore.Int32Type: |
| 135 | + return log.Int64Value(f.Integer) |
| 136 | + case zapcore.Int16Type: |
| 137 | + return log.Int64Value(f.Integer) |
| 138 | + case zapcore.Int8Type: |
| 139 | + return log.Int64Value(f.Integer) |
| 140 | + case zapcore.StringType: |
| 141 | + return log.StringValue(f.String) |
| 142 | + case zapcore.TimeType: |
| 143 | + // if f.Interface != nil { |
| 144 | + // return time.Unix(0, f.Integer).In(f.Interface.(*time.Location)) |
| 145 | + // } else { |
| 146 | + // // Fall back to UTC if location is nil. |
| 147 | + // return time.Unix(0, f.Integer) |
| 148 | + // } |
| 149 | + return log.Int64Value(f.Integer) |
| 150 | + case zapcore.TimeFullType: |
| 151 | + return log.StringValue("TimeFullType") |
| 152 | + // enc.AddTime(f.Key, f.Interface.(time.Time)) |
| 153 | + case zapcore.Uint64Type: |
| 154 | + return log.Int64Value(f.Integer) |
| 155 | + case zapcore.Uint32Type: |
| 156 | + return log.Int64Value(f.Integer) |
| 157 | + case zapcore.Uint16Type: |
| 158 | + return log.Int64Value(f.Integer) |
| 159 | + case zapcore.Uint8Type: |
| 160 | + return log.Int64Value(f.Integer) |
| 161 | + |
| 162 | + // how to handle these types |
| 163 | + // case zapcore.SkipType: |
| 164 | + // break |
| 165 | + // case UintptrType: |
| 166 | + // enc.AddUintptr(f.Key, uintptr(f.Integer)) |
| 167 | + // case ReflectType: |
| 168 | + // err = enc.AddReflected(f.Key, f.Interface) |
| 169 | + // case NamespaceType: |
| 170 | + // enc.OpenNamespace(f.Key) |
| 171 | + // case StringerType: |
| 172 | + // err = encodeStringer(f.Key, f.Interface, enc) |
| 173 | + // case ErrorType: |
| 174 | + // err = encodeError(f.Key, f.Interface.(error), enc) |
| 175 | + default: |
| 176 | + panic(fmt.Sprintf("unhandled attribute kind: %c", f.Type)) |
| 177 | + } |
| 178 | +} |
0 commit comments