| 16 | ) |
| 17 | |
| 18 | func LogsToPB(dbLog []Log) []*otlplogsv1.ResourceLogs { |
| 19 | if len(dbLog) == 0 { |
| 20 | return nil |
| 21 | } |
| 22 | |
| 23 | rsm := make(map[attribute.Distinct]*otlplogsv1.ResourceLogs) |
| 24 | |
| 25 | type key struct { |
| 26 | r attribute.Distinct |
| 27 | is instrumentation.Scope |
| 28 | } |
| 29 | ssm := make(map[key]*otlplogsv1.ScopeLogs) |
| 30 | |
| 31 | var resources int |
| 32 | for _, sd := range dbLog { |
| 33 | var res *sdkresource.Resource |
| 34 | var resPb otlpresourcev1.Resource |
| 35 | if err := protojson.Unmarshal(sd.Resource, &resPb); err != nil { |
| 36 | slog.Error("failed to unmarshal log resource", "error", err, "log", sd) |
| 37 | continue |
| 38 | } else { |
| 39 | res = telemetry.ResourceFromPB(sd.ResourceSchemaUrl, &resPb) |
| 40 | } |
| 41 | var scope instrumentation.Scope |
| 42 | var scopePb otlpcommonv1.InstrumentationScope |
| 43 | if err := protojson.Unmarshal(sd.InstrumentationScope, &scopePb); err != nil { |
| 44 | slog.Error("failed to unmarshal instrumentation scope", "error", err, "log", sd) |
| 45 | continue |
| 46 | } else { |
| 47 | scope = telemetry.InstrumentationScopeFromPB(&scopePb) |
| 48 | } |
| 49 | rKey := res.Equivalent() |
| 50 | k := key{ |
| 51 | r: rKey, |
| 52 | is: scope, |
| 53 | } |
| 54 | scopeLog, iOk := ssm[k] |
| 55 | if !iOk { |
| 56 | // Either the resource or instrumentation scope were unknown. |
| 57 | scopeLog = &otlplogsv1.ScopeLogs{ |
| 58 | Scope: &scopePb, |
| 59 | LogRecords: []*otlplogsv1.LogRecord{}, |
| 60 | SchemaUrl: scope.SchemaURL, |
| 61 | } |
| 62 | } |
| 63 | var bodyPb otlpcommonv1.AnyValue |
| 64 | if err := proto.Unmarshal(sd.Body, &bodyPb); err != nil { |
| 65 | slog.Warn("failed to unmarshal log body", "error", err, "log", sd) |
| 66 | continue |
| 67 | } |
| 68 | var attrs []*otlpcommonv1.KeyValue |
| 69 | if err := UnmarshalProtoJSONs(sd.Attributes, &otlpcommonv1.KeyValue{}, &attrs); err != nil { |
| 70 | slog.Warn("failed to unmarshal log attributes", "error", err) |
| 71 | continue |
| 72 | } |
| 73 | tid, err := trace.TraceIDFromHex(sd.TraceID.String) |
| 74 | if err != nil { |
| 75 | slog.Error("failed to unmarshal trace id", "error", err) |