| 1 |
// Code generated by mdatagen. DO NOT EDIT. |
| 2 |
|
| 3 |
package metadata |
| 4 |
|
| 5 |
import ( |
| 6 |
"go.opentelemetry.io/collector/component" |
| 7 |
"go.opentelemetry.io/collector/pdata/pcommon" |
| 8 |
"go.opentelemetry.io/collector/pdata/plog" |
| 9 |
"go.opentelemetry.io/collector/receiver" |
| 10 |
) |
| 11 |
|
| 12 |
// LogsBuilder provides an interface for scrapers to report logs while taking care of all the transformations |
| 13 |
// required to produce log representation defined in metadata and user config. |
| 14 |
type LogsBuilder struct { |
| 15 |
logsBuffer plog.Logs |
| 16 |
logRecordsBuffer plog.LogRecordSlice |
| 17 |
buildInfo component.BuildInfo // contains version information. |
| 18 |
} |
| 19 |
|
| 20 |
// LogBuilderOption applies changes to default logs builder. |
| 21 |
type LogBuilderOption interface { |
| 22 |
apply(*LogsBuilder) |
| 23 |
} |
| 24 |
|
| 25 |
func NewLogsBuilder(settings receiver.Settings) *LogsBuilder { |
| 26 |
lb := &LogsBuilder{ |
| 27 |
logsBuffer: plog.NewLogs(), |
| 28 |
logRecordsBuffer: plog.NewLogRecordSlice(), |
| 29 |
buildInfo: settings.BuildInfo, |
| 30 |
} |
| 31 |
|
| 32 |
return lb |
| 33 |
} |
| 34 |
|
| 35 |
// ResourceLogsOption applies changes to provided resource logs. |
| 36 |
type ResourceLogsOption interface { |
| 37 |
apply(plog.ResourceLogs) |
| 38 |
} |
| 39 |
|
| 40 |
type resourceLogsOptionFunc func(plog.ResourceLogs) |
| 41 |
|
| 42 |
func (rlof resourceLogsOptionFunc) apply(rl plog.ResourceLogs) { |
| 43 |
rlof(rl) |
| 44 |
} |
| 45 |
|
| 46 |
// WithLogsResource sets the provided resource on the emitted ResourceLogs. |
| 47 |
// It's recommended to use ResourceBuilder to create the resource. |
| 48 |
func WithLogsResource(res pcommon.Resource) ResourceLogsOption { |
| 49 |
return resourceLogsOptionFunc(func(rl plog.ResourceLogs) { |
| 50 |
res.CopyTo(rl.Resource()) |
| 51 |
}) |
| 52 |
} |
| 53 |
|
| 54 |
// AppendLogRecord adds a log record to the logs builder. |
| 55 |
func (lb *LogsBuilder) AppendLogRecord(lr plog.LogRecord) { |
| 56 |
lr.MoveTo(lb.logRecordsBuffer.AppendEmpty()) |
| 57 |
} |
| 58 |
|
| 59 |
// EmitForResource saves all the generated logs under a new resource and updates the internal state to be ready for |
| 60 |
// recording another set of log records as part of another resource. This function can be helpful when one scraper |
| 61 |
// needs to emit logs from several resources. Otherwise calling this function is not required, |
| 62 |
// just `Emit` function can be called instead. |
| 63 |
// Resource attributes should be provided as ResourceLogsOption arguments. |
| 64 |
func (lb *LogsBuilder) EmitForResource(options ...ResourceLogsOption) { |
| 65 |
rl := plog.NewResourceLogs() |
| 66 |
ils := rl.ScopeLogs().AppendEmpty() |
| 67 |
ils.Scope().SetName(ScopeName) |
| 68 |
ils.Scope().SetVersion(lb.buildInfo.Version) |
| 69 |
|
| 70 |
for _, op := range options { |
| 71 |
op.apply(rl) |
| 72 |
} |
| 73 |
|
| 74 |
if lb.logRecordsBuffer.Len() > 0 { |
| 75 |
lb.logRecordsBuffer.MoveAndAppendTo(ils.LogRecords()) |
| 76 |
lb.logRecordsBuffer = plog.NewLogRecordSlice() |
| 77 |
} |
| 78 |
|
| 79 |
if ils.LogRecords().Len() > 0 { |
| 80 |
rl.MoveTo(lb.logsBuffer.ResourceLogs().AppendEmpty()) |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
// Emit returns all the logs accumulated by the logs builder and updates the internal state to be ready for |
| 85 |
// recording another set of logs. This function will be responsible for applying all the transformations required to |
| 86 |
// produce logs representation defined in metadata and user config. |
| 87 |
func (lb *LogsBuilder) Emit(options ...ResourceLogsOption) plog.Logs { |
| 88 |
lb.EmitForResource(options...) |
| 89 |
logs := lb.logsBuffer |
| 90 |
lb.logsBuffer = plog.NewLogs() |
| 91 |
return logs |
| 92 |
} |