github.com/yankunsam/loki/v2@v2.6.3-0.20220817130409-389df5235c27/pkg/logproto/types.go (about) 1 package logproto 2 3 import ( 4 fmt "fmt" 5 io "io" 6 "time" 7 8 "github.com/prometheus/common/model" 9 ) 10 11 // Stream contains a unique labels set as a string and a set of entries for it. 12 // We are not using the proto generated version but this custom one so that we 13 // can improve serialization see benchmark. 14 type Stream struct { 15 Labels string `protobuf:"bytes,1,opt,name=labels,proto3" json:"labels"` 16 Entries []Entry `protobuf:"bytes,2,rep,name=entries,proto3,customtype=EntryAdapter" json:"entries"` 17 Hash uint64 `protobuf:"varint,3,opt,name=hash,proto3" json:"-"` 18 } 19 20 // Entry is a log entry with a timestamp. 21 type Entry struct { 22 Timestamp time.Time `protobuf:"bytes,1,opt,name=timestamp,proto3,stdtime" json:"ts"` 23 Line string `protobuf:"bytes,2,opt,name=line,proto3" json:"line"` 24 } 25 26 func (m *Stream) Marshal() (dAtA []byte, err error) { 27 size := m.Size() 28 dAtA = make([]byte, size) 29 n, err := m.MarshalToSizedBuffer(dAtA[:size]) 30 if err != nil { 31 return nil, err 32 } 33 return dAtA[:n], nil 34 } 35 36 func (m *Stream) MarshalTo(dAtA []byte) (int, error) { 37 size := m.Size() 38 return m.MarshalToSizedBuffer(dAtA[:size]) 39 } 40 41 func (m *Stream) MarshalToSizedBuffer(dAtA []byte) (int, error) { 42 i := len(dAtA) 43 _ = i 44 var l int 45 _ = l 46 if m.Hash != 0 { 47 i = encodeVarintLogproto(dAtA, i, m.Hash) 48 i-- 49 dAtA[i] = 0x18 50 } 51 if len(m.Entries) > 0 { 52 for iNdEx := len(m.Entries) - 1; iNdEx >= 0; iNdEx-- { 53 { 54 size, err := m.Entries[iNdEx].MarshalToSizedBuffer(dAtA[:i]) 55 if err != nil { 56 return 0, err 57 } 58 i -= size 59 i = encodeVarintLogproto(dAtA, i, uint64(size)) 60 } 61 i-- 62 dAtA[i] = 0x12 63 } 64 } 65 if len(m.Labels) > 0 { 66 i -= len(m.Labels) 67 copy(dAtA[i:], m.Labels) 68 i = encodeVarintLogproto(dAtA, i, uint64(len(m.Labels))) 69 i-- 70 dAtA[i] = 0xa 71 } 72 return len(dAtA) - i, nil 73 } 74 75 func (m *Entry) Marshal() (dAtA []byte, err error) { 76 size := m.Size() 77 dAtA = make([]byte, size) 78 n, err := m.MarshalToSizedBuffer(dAtA[:size]) 79 if err != nil { 80 return nil, err 81 } 82 return dAtA[:n], nil 83 } 84 85 func (m *Entry) MarshalTo(dAtA []byte) (int, error) { 86 size := m.Size() 87 return m.MarshalToSizedBuffer(dAtA[:size]) 88 } 89 90 func (m *Entry) MarshalToSizedBuffer(dAtA []byte) (int, error) { 91 i := len(dAtA) 92 _ = i 93 var l int 94 _ = l 95 if len(m.Line) > 0 { 96 i -= len(m.Line) 97 copy(dAtA[i:], m.Line) 98 i = encodeVarintLogproto(dAtA, i, uint64(len(m.Line))) 99 i-- 100 dAtA[i] = 0x12 101 } 102 n7, err7 := StdTimeMarshalTo(m.Timestamp, dAtA[i-SizeOfStdTime(m.Timestamp):]) 103 if err7 != nil { 104 return 0, err7 105 } 106 i -= n7 107 i = encodeVarintLogproto(dAtA, i, uint64(n7)) 108 i-- 109 dAtA[i] = 0xa 110 return len(dAtA) - i, nil 111 } 112 113 func (m *Stream) Unmarshal(dAtA []byte) error { 114 l := len(dAtA) 115 iNdEx := 0 116 for iNdEx < l { 117 preIndex := iNdEx 118 var wire uint64 119 for shift := uint(0); ; shift += 7 { 120 if shift >= 64 { 121 return ErrIntOverflowLogproto 122 } 123 if iNdEx >= l { 124 return io.ErrUnexpectedEOF 125 } 126 b := dAtA[iNdEx] 127 iNdEx++ 128 wire |= uint64(b&0x7F) << shift 129 if b < 0x80 { 130 break 131 } 132 } 133 fieldNum := int32(wire >> 3) 134 wireType := int(wire & 0x7) 135 if wireType == 4 { 136 return fmt.Errorf("proto: StreamAdapter: wiretype end group for non-group") 137 } 138 if fieldNum <= 0 { 139 return fmt.Errorf("proto: StreamAdapter: illegal tag %d (wire type %d)", fieldNum, wire) 140 } 141 switch fieldNum { 142 case 1: 143 if wireType != 2 { 144 return fmt.Errorf("proto: wrong wireType = %d for field Labels", wireType) 145 } 146 var stringLen uint64 147 for shift := uint(0); ; shift += 7 { 148 if shift >= 64 { 149 return ErrIntOverflowLogproto 150 } 151 if iNdEx >= l { 152 return io.ErrUnexpectedEOF 153 } 154 b := dAtA[iNdEx] 155 iNdEx++ 156 stringLen |= uint64(b&0x7F) << shift 157 if b < 0x80 { 158 break 159 } 160 } 161 intStringLen := int(stringLen) 162 if intStringLen < 0 { 163 return ErrInvalidLengthLogproto 164 } 165 postIndex := iNdEx + intStringLen 166 if postIndex < 0 { 167 return ErrInvalidLengthLogproto 168 } 169 if postIndex > l { 170 return io.ErrUnexpectedEOF 171 } 172 m.Labels = string(dAtA[iNdEx:postIndex]) 173 iNdEx = postIndex 174 case 2: 175 if wireType != 2 { 176 return fmt.Errorf("proto: wrong wireType = %d for field Entries", wireType) 177 } 178 var msglen int 179 for shift := uint(0); ; shift += 7 { 180 if shift >= 64 { 181 return ErrIntOverflowLogproto 182 } 183 if iNdEx >= l { 184 return io.ErrUnexpectedEOF 185 } 186 b := dAtA[iNdEx] 187 iNdEx++ 188 msglen |= int(b&0x7F) << shift 189 if b < 0x80 { 190 break 191 } 192 } 193 if msglen < 0 { 194 return ErrInvalidLengthLogproto 195 } 196 postIndex := iNdEx + msglen 197 if postIndex < 0 { 198 return ErrInvalidLengthLogproto 199 } 200 if postIndex > l { 201 return io.ErrUnexpectedEOF 202 } 203 m.Entries = append(m.Entries, Entry{}) 204 if err := m.Entries[len(m.Entries)-1].Unmarshal(dAtA[iNdEx:postIndex]); err != nil { 205 return err 206 } 207 iNdEx = postIndex 208 case 3: 209 if wireType != 0 { 210 return fmt.Errorf("proto: wrong wireType = %d for field Hash", wireType) 211 } 212 m.Hash = 0 213 for shift := uint(0); ; shift += 7 { 214 if shift >= 64 { 215 return ErrIntOverflowLogproto 216 } 217 if iNdEx >= l { 218 return io.ErrUnexpectedEOF 219 } 220 b := dAtA[iNdEx] 221 iNdEx++ 222 m.Hash |= uint64(b&0x7F) << shift 223 if b < 0x80 { 224 break 225 } 226 } 227 default: 228 iNdEx = preIndex 229 skippy, err := skipLogproto(dAtA[iNdEx:]) 230 if err != nil { 231 return err 232 } 233 if skippy < 0 { 234 return ErrInvalidLengthLogproto 235 } 236 if (iNdEx + skippy) < 0 { 237 return ErrInvalidLengthLogproto 238 } 239 if (iNdEx + skippy) > l { 240 return io.ErrUnexpectedEOF 241 } 242 iNdEx += skippy 243 } 244 } 245 246 if iNdEx > l { 247 return io.ErrUnexpectedEOF 248 } 249 return nil 250 } 251 252 func (m *Entry) Unmarshal(dAtA []byte) error { 253 l := len(dAtA) 254 iNdEx := 0 255 for iNdEx < l { 256 preIndex := iNdEx 257 var wire uint64 258 for shift := uint(0); ; shift += 7 { 259 if shift >= 64 { 260 return ErrIntOverflowLogproto 261 } 262 if iNdEx >= l { 263 return io.ErrUnexpectedEOF 264 } 265 b := dAtA[iNdEx] 266 iNdEx++ 267 wire |= uint64(b&0x7F) << shift 268 if b < 0x80 { 269 break 270 } 271 } 272 fieldNum := int32(wire >> 3) 273 wireType := int(wire & 0x7) 274 if wireType == 4 { 275 return fmt.Errorf("proto: EntryAdapter: wiretype end group for non-group") 276 } 277 if fieldNum <= 0 { 278 return fmt.Errorf("proto: EntryAdapter: illegal tag %d (wire type %d)", fieldNum, wire) 279 } 280 switch fieldNum { 281 case 1: 282 if wireType != 2 { 283 return fmt.Errorf("proto: wrong wireType = %d for field Timestamp", wireType) 284 } 285 var msglen int 286 for shift := uint(0); ; shift += 7 { 287 if shift >= 64 { 288 return ErrIntOverflowLogproto 289 } 290 if iNdEx >= l { 291 return io.ErrUnexpectedEOF 292 } 293 b := dAtA[iNdEx] 294 iNdEx++ 295 msglen |= int(b&0x7F) << shift 296 if b < 0x80 { 297 break 298 } 299 } 300 if msglen < 0 { 301 return ErrInvalidLengthLogproto 302 } 303 postIndex := iNdEx + msglen 304 if postIndex < 0 { 305 return ErrInvalidLengthLogproto 306 } 307 if postIndex > l { 308 return io.ErrUnexpectedEOF 309 } 310 if err := StdTimeUnmarshal(&m.Timestamp, dAtA[iNdEx:postIndex]); err != nil { 311 return err 312 } 313 iNdEx = postIndex 314 case 2: 315 if wireType != 2 { 316 return fmt.Errorf("proto: wrong wireType = %d for field Line", wireType) 317 } 318 var stringLen uint64 319 for shift := uint(0); ; shift += 7 { 320 if shift >= 64 { 321 return ErrIntOverflowLogproto 322 } 323 if iNdEx >= l { 324 return io.ErrUnexpectedEOF 325 } 326 b := dAtA[iNdEx] 327 iNdEx++ 328 stringLen |= uint64(b&0x7F) << shift 329 if b < 0x80 { 330 break 331 } 332 } 333 intStringLen := int(stringLen) 334 if intStringLen < 0 { 335 return ErrInvalidLengthLogproto 336 } 337 postIndex := iNdEx + intStringLen 338 if postIndex < 0 { 339 return ErrInvalidLengthLogproto 340 } 341 if postIndex > l { 342 return io.ErrUnexpectedEOF 343 } 344 m.Line = string(dAtA[iNdEx:postIndex]) 345 iNdEx = postIndex 346 default: 347 iNdEx = preIndex 348 skippy, err := skipLogproto(dAtA[iNdEx:]) 349 if err != nil { 350 return err 351 } 352 if skippy < 0 { 353 return ErrInvalidLengthLogproto 354 } 355 if (iNdEx + skippy) < 0 { 356 return ErrInvalidLengthLogproto 357 } 358 if (iNdEx + skippy) > l { 359 return io.ErrUnexpectedEOF 360 } 361 iNdEx += skippy 362 } 363 } 364 365 if iNdEx > l { 366 return io.ErrUnexpectedEOF 367 } 368 return nil 369 } 370 371 func (m *Stream) Size() (n int) { 372 if m == nil { 373 return 0 374 } 375 var l int 376 _ = l 377 l = len(m.Labels) 378 if l > 0 { 379 n += 1 + l + sovLogproto(uint64(l)) 380 } 381 if len(m.Entries) > 0 { 382 for _, e := range m.Entries { 383 l = e.Size() 384 n += 1 + l + sovLogproto(uint64(l)) 385 } 386 } 387 if m.Hash != 0 { 388 n += 1 + sovLogproto(m.Hash) 389 } 390 return n 391 } 392 393 func (m *Entry) Size() (n int) { 394 if m == nil { 395 return 0 396 } 397 var l int 398 _ = l 399 l = SizeOfStdTime(m.Timestamp) 400 n += 1 + l + sovLogproto(uint64(l)) 401 l = len(m.Line) 402 if l > 0 { 403 n += 1 + l + sovLogproto(uint64(l)) 404 } 405 return n 406 } 407 408 func (m *Stream) Equal(that interface{}) bool { 409 if that == nil { 410 return m == nil 411 } 412 413 that1, ok := that.(*Stream) 414 if !ok { 415 that2, ok := that.(Stream) 416 if ok { 417 that1 = &that2 418 } else { 419 return false 420 } 421 } 422 if that1 == nil { 423 return m == nil 424 } else if m == nil { 425 return false 426 } 427 if m.Labels != that1.Labels { 428 return false 429 } 430 if len(m.Entries) != len(that1.Entries) { 431 return false 432 } 433 for i := range m.Entries { 434 if !m.Entries[i].Equal(that1.Entries[i]) { 435 return false 436 } 437 } 438 return m.Hash == that1.Hash 439 } 440 441 func (m *Entry) Equal(that interface{}) bool { 442 if that == nil { 443 return m == nil 444 } 445 446 that1, ok := that.(*Entry) 447 if !ok { 448 that2, ok := that.(Entry) 449 if ok { 450 that1 = &that2 451 } else { 452 return false 453 } 454 } 455 if that1 == nil { 456 return m == nil 457 } else if m == nil { 458 return false 459 } 460 if !m.Timestamp.Equal(that1.Timestamp) { 461 return false 462 } 463 if m.Line != that1.Line { 464 return false 465 } 466 return true 467 } 468 469 func (c *ChunkRef) FingerprintModel() model.Fingerprint { 470 return model.Fingerprint(c.Fingerprint) 471 }