Skip to content

Commit

Permalink
[mdatagen] generate wrapper for otel calls
Browse files Browse the repository at this point in the history
Part of #10801

Signed-off-by: Alex Boten <[email protected]>
  • Loading branch information
codeboten committed Aug 21, 2024
1 parent 343f449 commit da608a9
Show file tree
Hide file tree
Showing 13 changed files with 187 additions and 51 deletions.
2 changes: 1 addition & 1 deletion cmd/mdatagen/internal/samplereceiver/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func createMetrics(ctx context.Context, set receiver.Settings, _ component.Confi
if err != nil {
return nil, err
}
telemetryBuilder.BatchSizeTriggerSend.Add(ctx, 1)
telemetryBuilder.RecordBatchSizeTriggerSend(ctx, 1)
return nopReceiver{telemetryBuilder: telemetryBuilder}, nil
}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions cmd/mdatagen/templates/telemetry.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,14 @@ func NewTelemetryBuilder(settings component.TelemetrySettings, options ...teleme
return &builder, errs
}

{{- range $name, $metric := .Telemetry.Metrics }}
{{- if not $metric.Data.Async }}

func (b *TelemetryBuilder) Record{{ $name.Render }}(ctx context.Context, val {{ $metric.Data.BasicType }}, opts ...metric.{{- if eq $metric.Data.Type "Histogram" -}}Record{{- else -}}Add{{- end -}}Option) {
b.{{ $name.Render }}.{{- if eq $metric.Data.Type "Histogram" -}}Record{{- else -}}Add{{- end -}}(ctx, val, opts...)
}

{{- end }}
{{- end }}

{{- end }}
36 changes: 36 additions & 0 deletions exporter/exporterhelper/internal/metadata/generated_telemetry.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 9 additions & 16 deletions exporter/exporterhelper/obsexporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,21 +116,17 @@ func (or *obsReport) recordMetrics(ctx context.Context, dataType component.DataT
if or.level == configtelemetry.LevelNone {
return
}
var sentMeasure, failedMeasure metric.Int64Counter
switch dataType {
case component.DataTypeTraces:
sentMeasure = or.telemetryBuilder.ExporterSentSpans
failedMeasure = or.telemetryBuilder.ExporterSendFailedSpans
or.telemetryBuilder.RecordExporterSentSpans(ctx, sent, metric.WithAttributes(or.otelAttrs...))
or.telemetryBuilder.RecordExporterSendFailedSpans(ctx, failed, metric.WithAttributes(or.otelAttrs...))
case component.DataTypeMetrics:
sentMeasure = or.telemetryBuilder.ExporterSentMetricPoints
failedMeasure = or.telemetryBuilder.ExporterSendFailedMetricPoints
or.telemetryBuilder.RecordExporterSentMetricPoints(ctx, sent, metric.WithAttributes(or.otelAttrs...))
or.telemetryBuilder.RecordExporterSendFailedMetricPoints(ctx, failed, metric.WithAttributes(or.otelAttrs...))
case component.DataTypeLogs:
sentMeasure = or.telemetryBuilder.ExporterSentLogRecords
failedMeasure = or.telemetryBuilder.ExporterSendFailedLogRecords
or.telemetryBuilder.RecordExporterSentLogRecords(ctx, sent, metric.WithAttributes(or.otelAttrs...))
or.telemetryBuilder.RecordExporterSendFailedLogRecords(ctx, failed, metric.WithAttributes(or.otelAttrs...))
}

sentMeasure.Add(ctx, sent, metric.WithAttributes(or.otelAttrs...))
failedMeasure.Add(ctx, failed, metric.WithAttributes(or.otelAttrs...))
}

func endSpan(ctx context.Context, err error, numSent, numFailedToSend int64, sentItemsKey, failedToSendItemsKey string) {
Expand All @@ -156,15 +152,12 @@ func toNumItems(numExportedItems int, err error) (int64, int64) {
}

func (or *obsReport) recordEnqueueFailure(ctx context.Context, dataType component.DataType, failed int64) {
var enqueueFailedMeasure metric.Int64Counter
switch dataType {
case component.DataTypeTraces:
enqueueFailedMeasure = or.telemetryBuilder.ExporterEnqueueFailedSpans
or.telemetryBuilder.RecordExporterEnqueueFailedSpans(ctx, failed, metric.WithAttributes(or.otelAttrs...))
case component.DataTypeMetrics:
enqueueFailedMeasure = or.telemetryBuilder.ExporterEnqueueFailedMetricPoints
or.telemetryBuilder.RecordExporterEnqueueFailedMetricPoints(ctx, failed, metric.WithAttributes(or.otelAttrs...))
case component.DataTypeLogs:
enqueueFailedMeasure = or.telemetryBuilder.ExporterEnqueueFailedLogRecords
or.telemetryBuilder.RecordExporterEnqueueFailedLogRecords(ctx, failed, metric.WithAttributes(or.otelAttrs...))
}

enqueueFailedMeasure.Add(ctx, failed, metric.WithAttributes(or.otelAttrs...))
}
16 changes: 16 additions & 0 deletions processor/batchprocessor/internal/metadata/generated_telemetry.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions processor/batchprocessor/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ func newBatchProcessorTelemetry(set processor.Settings, currentMetadataCardinali
func (bpt *batchProcessorTelemetry) record(trigger trigger, sent, bytes int64) {
switch trigger {
case triggerBatchSize:
bpt.telemetryBuilder.ProcessorBatchBatchSizeTriggerSend.Add(bpt.exportCtx, 1, metric.WithAttributeSet(bpt.processorAttr))
bpt.telemetryBuilder.RecordProcessorBatchBatchSizeTriggerSend(bpt.exportCtx, 1, metric.WithAttributeSet(bpt.processorAttr))
case triggerTimeout:
bpt.telemetryBuilder.ProcessorBatchTimeoutTriggerSend.Add(bpt.exportCtx, 1, metric.WithAttributeSet(bpt.processorAttr))
bpt.telemetryBuilder.RecordProcessorBatchTimeoutTriggerSend(bpt.exportCtx, 1, metric.WithAttributeSet(bpt.processorAttr))
}

bpt.telemetryBuilder.ProcessorBatchBatchSendSize.Record(bpt.exportCtx, sent, metric.WithAttributeSet(bpt.processorAttr))
bpt.telemetryBuilder.RecordProcessorBatchBatchSendSize(bpt.exportCtx, sent, metric.WithAttributeSet(bpt.processorAttr))
if bpt.detailed {
bpt.telemetryBuilder.ProcessorBatchBatchSendSizeBytes.Record(bpt.exportCtx, bytes, metric.WithAttributeSet(bpt.processorAttr))
bpt.telemetryBuilder.RecordProcessorBatchBatchSendSizeBytes(bpt.exportCtx, bytes, metric.WithAttributeSet(bpt.processorAttr))
}
}
49 changes: 49 additions & 0 deletions processor/processorhelper/internal/metadata/generated_telemetry.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 12 additions & 18 deletions processor/processorhelper/obsreport.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,29 +61,23 @@ func newObsReport(cfg ObsReportSettings) (*ObsReport, error) {
}

func (or *ObsReport) recordData(ctx context.Context, dataType component.DataType, accepted, refused, dropped, inserted int64) {
var acceptedCount, refusedCount, droppedCount, insertedCount metric.Int64Counter
switch dataType {
case component.DataTypeTraces:
acceptedCount = or.telemetryBuilder.ProcessorAcceptedSpans
refusedCount = or.telemetryBuilder.ProcessorRefusedSpans
droppedCount = or.telemetryBuilder.ProcessorDroppedSpans
insertedCount = or.telemetryBuilder.ProcessorInsertedSpans
or.telemetryBuilder.RecordProcessorAcceptedSpans(ctx, accepted, metric.WithAttributes(or.otelAttrs...))
or.telemetryBuilder.RecordProcessorRefusedSpans(ctx, refused, metric.WithAttributes(or.otelAttrs...))
or.telemetryBuilder.RecordProcessorDroppedSpans(ctx, dropped, metric.WithAttributes(or.otelAttrs...))
or.telemetryBuilder.RecordProcessorInsertedSpans(ctx, inserted, metric.WithAttributes(or.otelAttrs...))
case component.DataTypeMetrics:
acceptedCount = or.telemetryBuilder.ProcessorAcceptedMetricPoints
refusedCount = or.telemetryBuilder.ProcessorRefusedMetricPoints
droppedCount = or.telemetryBuilder.ProcessorDroppedMetricPoints
insertedCount = or.telemetryBuilder.ProcessorInsertedMetricPoints
or.telemetryBuilder.RecordProcessorAcceptedMetricPoints(ctx, accepted, metric.WithAttributes(or.otelAttrs...))
or.telemetryBuilder.RecordProcessorRefusedMetricPoints(ctx, refused, metric.WithAttributes(or.otelAttrs...))
or.telemetryBuilder.RecordProcessorDroppedMetricPoints(ctx, dropped, metric.WithAttributes(or.otelAttrs...))
or.telemetryBuilder.RecordProcessorInsertedMetricPoints(ctx, inserted, metric.WithAttributes(or.otelAttrs...))
case component.DataTypeLogs:
acceptedCount = or.telemetryBuilder.ProcessorAcceptedLogRecords
refusedCount = or.telemetryBuilder.ProcessorRefusedLogRecords
droppedCount = or.telemetryBuilder.ProcessorDroppedLogRecords
insertedCount = or.telemetryBuilder.ProcessorInsertedLogRecords
or.telemetryBuilder.RecordProcessorAcceptedLogRecords(ctx, accepted, metric.WithAttributes(or.otelAttrs...))
or.telemetryBuilder.RecordProcessorRefusedLogRecords(ctx, refused, metric.WithAttributes(or.otelAttrs...))
or.telemetryBuilder.RecordProcessorDroppedLogRecords(ctx, dropped, metric.WithAttributes(or.otelAttrs...))
or.telemetryBuilder.RecordProcessorInsertedLogRecords(ctx, inserted, metric.WithAttributes(or.otelAttrs...))
}

acceptedCount.Add(ctx, accepted, metric.WithAttributes(or.otelAttrs...))
refusedCount.Add(ctx, refused, metric.WithAttributes(or.otelAttrs...))
droppedCount.Add(ctx, dropped, metric.WithAttributes(or.otelAttrs...))
insertedCount.Add(ctx, inserted, metric.WithAttributes(or.otelAttrs...))
}

// TracesAccepted reports that the trace data was accepted.
Expand Down
25 changes: 25 additions & 0 deletions receiver/receiverhelper/internal/metadata/generated_telemetry.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 6 additions & 10 deletions receiver/receiverhelper/obsreport.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,19 +198,15 @@ func (rec *ObsReport) endOp(
}

func (rec *ObsReport) recordMetrics(receiverCtx context.Context, dataType component.DataType, numAccepted, numRefused int) {
var acceptedMeasure, refusedMeasure metric.Int64Counter
switch dataType {
case component.DataTypeTraces:
acceptedMeasure = rec.telemetryBuilder.ReceiverAcceptedSpans
refusedMeasure = rec.telemetryBuilder.ReceiverRefusedSpans
rec.telemetryBuilder.RecordReceiverAcceptedSpans(receiverCtx, int64(numAccepted), metric.WithAttributes(rec.otelAttrs...))
rec.telemetryBuilder.RecordReceiverRefusedSpans(receiverCtx, int64(numRefused), metric.WithAttributes(rec.otelAttrs...))
case component.DataTypeMetrics:
acceptedMeasure = rec.telemetryBuilder.ReceiverAcceptedMetricPoints
refusedMeasure = rec.telemetryBuilder.ReceiverRefusedMetricPoints
rec.telemetryBuilder.RecordReceiverAcceptedMetricPoints(receiverCtx, int64(numAccepted), metric.WithAttributes(rec.otelAttrs...))
rec.telemetryBuilder.RecordReceiverRefusedMetricPoints(receiverCtx, int64(numRefused), metric.WithAttributes(rec.otelAttrs...))
case component.DataTypeLogs:
acceptedMeasure = rec.telemetryBuilder.ReceiverAcceptedLogRecords
refusedMeasure = rec.telemetryBuilder.ReceiverRefusedLogRecords
rec.telemetryBuilder.RecordReceiverAcceptedLogRecords(receiverCtx, int64(numAccepted), metric.WithAttributes(rec.otelAttrs...))
rec.telemetryBuilder.RecordReceiverRefusedLogRecords(receiverCtx, int64(numRefused), metric.WithAttributes(rec.otelAttrs...))
}

acceptedMeasure.Add(receiverCtx, int64(numAccepted), metric.WithAttributes(rec.otelAttrs...))
refusedMeasure.Add(receiverCtx, int64(numRefused), metric.WithAttributes(rec.otelAttrs...))
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions receiver/scraperhelper/obsreport.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,6 @@ func (s *ObsReport) EndMetricsOp(
}

func (s *ObsReport) recordMetrics(scraperCtx context.Context, numScrapedMetrics, numErroredMetrics int) {
s.telemetryBuilder.ScraperScrapedMetricPoints.Add(scraperCtx, int64(numScrapedMetrics), metric.WithAttributes(s.otelAttrs...))
s.telemetryBuilder.ScraperErroredMetricPoints.Add(scraperCtx, int64(numErroredMetrics), metric.WithAttributes(s.otelAttrs...))
s.telemetryBuilder.RecordScraperScrapedMetricPoints(scraperCtx, int64(numScrapedMetrics), metric.WithAttributes(s.otelAttrs...))
s.telemetryBuilder.RecordScraperErroredMetricPoints(scraperCtx, int64(numErroredMetrics), metric.WithAttributes(s.otelAttrs...))
}

0 comments on commit da608a9

Please sign in to comment.