| 1 |
// Code generated by mdatagen. DO NOT EDIT. |
| 2 |
|
| 3 |
package {{ .Package }} |
| 4 |
|
| 5 |
import ( |
| 6 |
"go.opentelemetry.io/collector/component" |
| 7 |
"go.opentelemetry.io/collector/pdata/pcommon" |
| 8 |
"go.opentelemetry.io/collector/pdata/plog" |
| 9 |
{{- if or isReceiver isScraper }} |
| 10 |
"go.opentelemetry.io/collector/{{ .Status.Class }}" |
| 11 |
{{- end }} |
| 12 |
{{- if .SemConvVersion }} |
| 13 |
conventions "go.opentelemetry.io/otel/semconv/v{{ .SemConvVersion }}" |
| 14 |
{{- end }} |
| 15 |
{{- if .Events }} |
| 16 |
"context" |
| 17 |
"go.opentelemetry.io/otel/trace" |
| 18 |
"go.opentelemetry.io/collector/filter" |
| 19 |
{{- end }} |
| 20 |
) |
| 21 |
|
| 22 |
|
| 23 |
{{ if getEventConditionalAttributes .Attributes }} |
| 24 |
type EventAttributeOption interface { |
| 25 |
apply(plog.LogRecord) |
| 26 |
} |
| 27 |
|
| 28 |
type eventAttributeOptionFunc func(plog.LogRecord) |
| 29 |
|
| 30 |
func (eaof eventAttributeOptionFunc) apply(lr plog.LogRecord) { |
| 31 |
eaof(lr) |
| 32 |
} |
| 33 |
|
| 34 |
{{ range getEventConditionalAttributes .Attributes }} |
| 35 |
func With{{ .Render }}EventAttribute({{ .RenderUnexported }}AttributeValue {{ (attributeInfo .).Type.Primitive }}) EventAttributeOption { |
| 36 |
return eventAttributeOptionFunc(func(dp plog.LogRecord) { |
| 37 |
{{- template "putAttribute" . }} |
| 38 |
}) |
| 39 |
} |
| 40 |
{{ end }} |
| 41 |
{{ end }} |
| 42 |
|
| 43 |
{{ range $name, $event := .Events -}} |
| 44 |
type event{{ $name.Render }} struct { |
| 45 |
data plog.LogRecordSlice // data buffer for generated log records. |
| 46 |
config EventConfig // event config provided by user. |
| 47 |
} |
| 48 |
|
| 49 |
func (e *event{{ $name.Render }}) recordEvent(ctx context.Context, timestamp pcommon.Timestamp |
| 50 |
{{- range $event.Attributes -}}{{- if not (attributeInfo .).IsConditional -}}, {{ .RenderUnexported }}AttributeValue {{ (attributeInfo .).Type.Primitive }}{{ end }}{{end}}{{- if $event.HasConditionalAttributes $.Attributes -}}, options ...EventAttributeOption{{- end -}}) { |
| 51 |
if !e.config.Enabled { |
| 52 |
return |
| 53 |
} |
| 54 |
dp := e.data.AppendEmpty() |
| 55 |
dp.SetEventName("{{ $name }}") |
| 56 |
dp.SetTimestamp(timestamp) |
| 57 |
|
| 58 |
if span := trace.SpanContextFromContext(ctx); span.IsValid() { |
| 59 |
dp.SetTraceID(pcommon.TraceID(span.TraceID())) |
| 60 |
dp.SetSpanID(pcommon.SpanID(span.SpanID())) |
| 61 |
} |
| 62 |
|
| 63 |
{{- range $event.Attributes }} |
| 64 |
{{- if not (attributeInfo .).IsConditional -}} |
| 65 |
{{- template "putAttribute" . }} |
| 66 |
{{- end }} |
| 67 |
{{- end }} |
| 68 |
|
| 69 |
{{ if $event.HasConditionalAttributes $.Attributes }} |
| 70 |
for _, op := range options { |
| 71 |
op.apply(dp) |
| 72 |
} |
| 73 |
{{- end }} |
| 74 |
} |
| 75 |
|
| 76 |
// emit appends recorded event data to a events slice and prepares it for recording another set of log records. |
| 77 |
func (e *event{{ $name.Render }}) emit(lrs plog.LogRecordSlice) { |
| 78 |
if e.config.Enabled && e.data.Len() > 0 { |
| 79 |
e.data.MoveAndAppendTo(lrs) |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
func newEvent{{ $name.Render }}(cfg EventConfig) event{{ $name.Render }} { |
| 84 |
e := event{{ $name.Render }}{config: cfg} |
| 85 |
if cfg.Enabled { |
| 86 |
e.data = plog.NewLogRecordSlice() |
| 87 |
} |
| 88 |
return e |
| 89 |
} |
| 90 |
|
| 91 |
{{ end -}} |
| 92 |
|
| 93 |
// LogsBuilder provides an interface for scrapers to report logs while taking care of all the transformations |
| 94 |
// required to produce log representation defined in metadata and user config. |
| 95 |
type LogsBuilder struct { |
| 96 |
{{- if .Events }} |
| 97 |
config LogsBuilderConfig // config of the logs builder. |
| 98 |
{{- end }} |
| 99 |
logsBuffer plog.Logs |
| 100 |
logRecordsBuffer plog.LogRecordSlice |
| 101 |
buildInfo component.BuildInfo // contains version information. |
| 102 |
{{- if and .Events .ResourceAttributes }} |
| 103 |
resourceAttributeIncludeFilter map[string]filter.Filter |
| 104 |
resourceAttributeExcludeFilter map[string]filter.Filter |
| 105 |
{{- end }} |
| 106 |
{{- range $name, $event := .Events }} |
| 107 |
event{{ $name.Render }} event{{ $name.Render }} |
| 108 |
{{- end }} |
| 109 |
} |
| 110 |
|
| 111 |
// LogBuilderOption applies changes to default logs builder. |
| 112 |
type LogBuilderOption interface { |
| 113 |
apply(*LogsBuilder) |
| 114 |
} |
| 115 |
|
| 116 |
{{- if or isReceiver isScraper }} |
| 117 |
func NewLogsBuilder({{ if .Events }}lbc LogsBuilderConfig, {{ end }}settings {{ .Status.Class }}.Settings) *LogsBuilder { |
| 118 |
{{- range $name, $event := .Events }} |
| 119 |
{{- if $event.Warnings.IfEnabled }} |
| 120 |
if lbc.Events.{{ $name.Render }}.Enabled { |
| 121 |
settings.Logger.Warn("[WARNING] `{{ $name }}` should not be enabled: {{ $event.Warnings.IfEnabled }}") |
| 122 |
} |
| 123 |
{{- end }} |
| 124 |
{{- if $event.Warnings.IfEnabledNotSet }} |
| 125 |
if !lbc.Events.{{ $name.Render }}.enabledSetByUser { |
| 126 |
settings.Logger.Warn("[WARNING] Please set `enabled` field explicitly for `{{ $name }}`: {{ $event.Warnings.IfEnabledNotSet }}") |
| 127 |
} |
| 128 |
{{- end }} |
| 129 |
{{- if $event.Warnings.IfConfigured }} |
| 130 |
if lbc.Events.{{ $name.Render }}.enabledSetByUser { |
| 131 |
settings.Logger.Warn("[WARNING] `{{ $name }}` should not be configured: {{ $event.Warnings.IfConfigured }}") |
| 132 |
} |
| 133 |
{{- end }} |
| 134 |
{{- end }} |
| 135 |
{{- if .Events }} |
| 136 |
{{- range $name, $attr := .ResourceAttributes }} |
| 137 |
{{- if $attr.Warnings.IfEnabled }} |
| 138 |
if lbc.ResourceAttributes.{{ $name.Render }}.Enabled { |
| 139 |
settings.Logger.Warn("[WARNING] `{{ $name }}` should not be enabled: {{ $attr.Warnings.IfEnabled }}") |
| 140 |
} |
| 141 |
{{- end }} |
| 142 |
{{- if $attr.Warnings.IfEnabledNotSet }} |
| 143 |
if !lbc.ResourceAttributes.{{ $name.Render }}.enabledSetByUser { |
| 144 |
settings.Logger.Warn("[WARNING] Please set `enabled` field explicitly for `{{ $name }}`: {{ $attr.Warnings.IfEnabledNotSet }}") |
| 145 |
} |
| 146 |
{{- end }} |
| 147 |
{{- if $attr.Warnings.IfConfigured }} |
| 148 |
if lbc.ResourceAttributes.{{ $name.Render }}.enabledSetByUser || lbc.ResourceAttributes.{{ $name.Render }}.EventsInclude != nil || lbc.ResourceAttributes.{{ $name.Render }}.EventsExclude != nil { |
| 149 |
settings.Logger.Warn("[WARNING] `{{ $name }}` should not be configured: {{ $attr.Warnings.IfConfigured }}") |
| 150 |
} |
| 151 |
{{- end }} |
| 152 |
{{- end }} |
| 153 |
{{- end }} |
| 154 |
lb := &LogsBuilder{ |
| 155 |
{{- if .Events }} |
| 156 |
config: lbc, |
| 157 |
{{- end }} |
| 158 |
logsBuffer: plog.NewLogs(), |
| 159 |
logRecordsBuffer: plog.NewLogRecordSlice(), |
| 160 |
buildInfo: settings.BuildInfo, |
| 161 |
{{- range $name, $event := .Events }} |
| 162 |
event{{ $name.Render }}: newEvent{{ $name.Render }}(lbc.Events.{{ $name.Render }}), |
| 163 |
{{- end }} |
| 164 |
{{ if and .Events .ResourceAttributes -}} |
| 165 |
resourceAttributeIncludeFilter: make(map[string]filter.Filter), |
| 166 |
resourceAttributeExcludeFilter: make(map[string]filter.Filter), |
| 167 |
{{- end }} |
| 168 |
} |
| 169 |
{{- if .Events }} |
| 170 |
{{- range $name, $attr := .ResourceAttributes }} |
| 171 |
if lbc.ResourceAttributes.{{ $name.Render }}.EventsInclude != nil { |
| 172 |
lb.resourceAttributeIncludeFilter["{{ $name }}"] = filter.CreateFilter(lbc.ResourceAttributes.{{ $name.Render }}.EventsInclude) |
| 173 |
} |
| 174 |
if lbc.ResourceAttributes.{{ $name.Render }}.EventsExclude != nil { |
| 175 |
lb.resourceAttributeExcludeFilter["{{ $name }}"] = filter.CreateFilter(lbc.ResourceAttributes.{{ $name.Render }}.EventsExclude) |
| 176 |
} |
| 177 |
{{- end }} |
| 178 |
{{- end }} |
| 179 |
|
| 180 |
return lb |
| 181 |
} |
| 182 |
{{- end }} |
| 183 |
|
| 184 |
{{- if .ResourceAttributes }} |
| 185 |
// NewResourceBuilder returns a new resource builder that should be used to build a resource associated with for the emitted logs. |
| 186 |
func (lb *LogsBuilder) NewResourceBuilder() *ResourceBuilder { |
| 187 |
return NewResourceBuilder({{ if .Events }}lb.config.ResourceAttributes{{ else }}ResourceAttributesConfig{}{{ end }}) |
| 188 |
} |
| 189 |
{{- end }} |
| 190 |
|
| 191 |
// ResourceLogsOption applies changes to provided resource logs. |
| 192 |
type ResourceLogsOption interface { |
| 193 |
apply(plog.ResourceLogs) |
| 194 |
} |
| 195 |
|
| 196 |
type resourceLogsOptionFunc func(plog.ResourceLogs) |
| 197 |
|
| 198 |
func (rlof resourceLogsOptionFunc) apply(rl plog.ResourceLogs) { |
| 199 |
rlof(rl) |
| 200 |
} |
| 201 |
|
| 202 |
// WithLogsResource sets the provided resource on the emitted ResourceLogs. |
| 203 |
// It's recommended to use ResourceBuilder to create the resource. |
| 204 |
func WithLogsResource(res pcommon.Resource) ResourceLogsOption { |
| 205 |
return resourceLogsOptionFunc(func(rl plog.ResourceLogs) { |
| 206 |
res.CopyTo(rl.Resource()) |
| 207 |
}) |
| 208 |
} |
| 209 |
|
| 210 |
// AppendLogRecord adds a log record to the logs builder. |
| 211 |
func (lb *LogsBuilder) AppendLogRecord(lr plog.LogRecord) { |
| 212 |
lr.MoveTo(lb.logRecordsBuffer.AppendEmpty()) |
| 213 |
} |
| 214 |
|
| 215 |
// EmitForResource saves all the generated logs under a new resource and updates the internal state to be ready for |
| 216 |
// recording another set of log records as part of another resource. This function can be helpful when one scraper |
| 217 |
// needs to emit logs from several resources. Otherwise calling this function is not required, |
| 218 |
// just `Emit` function can be called instead. |
| 219 |
// Resource attributes should be provided as ResourceLogsOption arguments. |
| 220 |
func (lb *LogsBuilder) EmitForResource(options ...ResourceLogsOption) { |
| 221 |
rl := plog.NewResourceLogs() |
| 222 |
{{- if .SemConvVersion }} |
| 223 |
rl.SetSchemaUrl(conventions.SchemaURL) |
| 224 |
{{- end }} |
| 225 |
ils := rl.ScopeLogs().AppendEmpty() |
| 226 |
ils.Scope().SetName(ScopeName) |
| 227 |
ils.Scope().SetVersion(lb.buildInfo.Version) |
| 228 |
|
| 229 |
{{- range $name, $event := .Events }} |
| 230 |
lb.event{{- $name.Render }}.emit(ils.LogRecords()) |
| 231 |
{{- end }} |
| 232 |
|
| 233 |
for _, op := range options { |
| 234 |
op.apply(rl) |
| 235 |
} |
| 236 |
|
| 237 |
if lb.logRecordsBuffer.Len() > 0 { |
| 238 |
lb.logRecordsBuffer.MoveAndAppendTo(ils.LogRecords()) |
| 239 |
lb.logRecordsBuffer = plog.NewLogRecordSlice() |
| 240 |
} |
| 241 |
|
| 242 |
{{ if and .Events .ResourceAttributes -}} |
| 243 |
for attr, filter := range lb.resourceAttributeIncludeFilter { |
| 244 |
if val, ok := rl.Resource().Attributes().Get(attr); ok && !filter.Matches(val.AsString()) { |
| 245 |
return |
| 246 |
} |
| 247 |
} |
| 248 |
for attr, filter := range lb.resourceAttributeExcludeFilter { |
| 249 |
if val, ok := rl.Resource().Attributes().Get(attr); ok && filter.Matches(val.AsString()) { |
| 250 |
return |
| 251 |
} |
| 252 |
} |
| 253 |
{{- end }} |
| 254 |
|
| 255 |
if ils.LogRecords().Len() > 0 { |
| 256 |
rl.MoveTo(lb.logsBuffer.ResourceLogs().AppendEmpty()) |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
// Emit returns all the logs accumulated by the logs builder and updates the internal state to be ready for |
| 261 |
// recording another set of logs. This function will be responsible for applying all the transformations required to |
| 262 |
// produce logs representation defined in metadata and user config. |
| 263 |
func (lb *LogsBuilder) Emit(options ...ResourceLogsOption) plog.Logs { |
| 264 |
lb.EmitForResource(options...) |
| 265 |
logs := lb.logsBuffer |
| 266 |
lb.logsBuffer = plog.NewLogs() |
| 267 |
return logs |
| 268 |
} |
| 269 |
|
| 270 |
{{ range $name, $event := .Events -}} |
| 271 |
// Record{{ $name.Render }}Event adds a log record of {{ $name }} event. |
| 272 |
func (lb *LogsBuilder) Record{{ $name.Render }}Event(ctx context.Context, timestamp pcommon.Timestamp |
| 273 |
{{- range $event.Attributes -}} |
| 274 |
{{- if not (attributeInfo .).IsConditional -}} |
| 275 |
, {{ .RenderUnexported }}AttributeValue {{ if (attributeInfo .).Enum }}Attribute{{ .Render }}{{ else }}{{ (attributeInfo .).Type.Primitive }}{{ end }} |
| 276 |
{{- end -}} |
| 277 |
{{- end -}} |
| 278 |
{{- if $event.HasConditionalAttributes $.Attributes -}} |
| 279 |
, options... EventAttributeOption |
| 280 |
{{- end -}}) { |
| 281 |
lb.event{{ $name.Render }}.recordEvent(ctx, timestamp |
| 282 |
{{- range $event.Attributes -}} |
| 283 |
{{- if not (attributeInfo .).IsConditional -}} |
| 284 |
, {{ .RenderUnexported }}AttributeValue{{ if (attributeInfo .).Enum }}.String(){{ end }} |
| 285 |
{{- end -}} |
| 286 |
{{- end -}} |
| 287 |
{{- if $event.HasConditionalAttributes $.Attributes -}} |
| 288 |
, options... |
| 289 |
{{- end -}}) |
| 290 |
} |
| 291 |
{{ end }} |