From da608a9ca5a3993bd5d5048fe572368075cd7087 Mon Sep 17 00:00:00 2001 From: Alex Boten <223565+codeboten@users.noreply.github.com> Date: Mon, 19 Aug 2024 12:24:57 -0700 Subject: [PATCH 1/4] [mdatagen] generate wrapper for otel calls Part of #10801 Signed-off-by: Alex Boten <223565+codeboten@users.noreply.github.com> --- .../internal/samplereceiver/factory.go | 2 +- .../internal/metadata/generated_telemetry.go | 8 +++ cmd/mdatagen/templates/telemetry.go.tmpl | 10 ++++ .../internal/metadata/generated_telemetry.go | 36 ++++++++++++++ exporter/exporterhelper/obsexporter.go | 25 ++++------ .../internal/metadata/generated_telemetry.go | 16 ++++++ processor/batchprocessor/metrics.go | 8 +-- .../internal/metadata/generated_telemetry.go | 49 +++++++++++++++++++ processor/processorhelper/obsreport.go | 30 +++++------- .../internal/metadata/generated_telemetry.go | 25 ++++++++++ receiver/receiverhelper/obsreport.go | 16 +++--- .../internal/metadata/generated_telemetry.go | 9 ++++ receiver/scraperhelper/obsreport.go | 4 +- 13 files changed, 187 insertions(+), 51 deletions(-) diff --git a/cmd/mdatagen/internal/samplereceiver/factory.go b/cmd/mdatagen/internal/samplereceiver/factory.go index 7a3c9bb170d..2199758dfbe 100644 --- a/cmd/mdatagen/internal/samplereceiver/factory.go +++ b/cmd/mdatagen/internal/samplereceiver/factory.go @@ -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 } diff --git a/cmd/mdatagen/internal/samplereceiver/internal/metadata/generated_telemetry.go b/cmd/mdatagen/internal/samplereceiver/internal/metadata/generated_telemetry.go index ea6bfee53e9..67ebb4a2c3d 100644 --- a/cmd/mdatagen/internal/samplereceiver/internal/metadata/generated_telemetry.go +++ b/cmd/mdatagen/internal/samplereceiver/internal/metadata/generated_telemetry.go @@ -112,3 +112,11 @@ func NewTelemetryBuilder(settings component.TelemetrySettings, options ...teleme errs = errors.Join(errs, err) return &builder, errs } + +func (b *TelemetryBuilder) RecordBatchSizeTriggerSend(ctx context.Context, val int64, opts ...metric.AddOption) { + b.BatchSizeTriggerSend.Add(ctx, val, opts...) +} + +func (b *TelemetryBuilder) RecordRequestDuration(ctx context.Context, val float64, opts ...metric.RecordOption) { + b.RequestDuration.Record(ctx, val, opts...) +} diff --git a/cmd/mdatagen/templates/telemetry.go.tmpl b/cmd/mdatagen/templates/telemetry.go.tmpl index 1727e823e61..a0faa1db289 100644 --- a/cmd/mdatagen/templates/telemetry.go.tmpl +++ b/cmd/mdatagen/templates/telemetry.go.tmpl @@ -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 }} diff --git a/exporter/exporterhelper/internal/metadata/generated_telemetry.go b/exporter/exporterhelper/internal/metadata/generated_telemetry.go index 7155f256b58..d708cd79d83 100644 --- a/exporter/exporterhelper/internal/metadata/generated_telemetry.go +++ b/exporter/exporterhelper/internal/metadata/generated_telemetry.go @@ -160,3 +160,39 @@ func NewTelemetryBuilder(settings component.TelemetrySettings, options ...teleme errs = errors.Join(errs, err) return &builder, errs } + +func (b *TelemetryBuilder) RecordExporterEnqueueFailedLogRecords(ctx context.Context, val int64, opts ...metric.AddOption) { + b.ExporterEnqueueFailedLogRecords.Add(ctx, val, opts...) +} + +func (b *TelemetryBuilder) RecordExporterEnqueueFailedMetricPoints(ctx context.Context, val int64, opts ...metric.AddOption) { + b.ExporterEnqueueFailedMetricPoints.Add(ctx, val, opts...) +} + +func (b *TelemetryBuilder) RecordExporterEnqueueFailedSpans(ctx context.Context, val int64, opts ...metric.AddOption) { + b.ExporterEnqueueFailedSpans.Add(ctx, val, opts...) +} + +func (b *TelemetryBuilder) RecordExporterSendFailedLogRecords(ctx context.Context, val int64, opts ...metric.AddOption) { + b.ExporterSendFailedLogRecords.Add(ctx, val, opts...) +} + +func (b *TelemetryBuilder) RecordExporterSendFailedMetricPoints(ctx context.Context, val int64, opts ...metric.AddOption) { + b.ExporterSendFailedMetricPoints.Add(ctx, val, opts...) +} + +func (b *TelemetryBuilder) RecordExporterSendFailedSpans(ctx context.Context, val int64, opts ...metric.AddOption) { + b.ExporterSendFailedSpans.Add(ctx, val, opts...) +} + +func (b *TelemetryBuilder) RecordExporterSentLogRecords(ctx context.Context, val int64, opts ...metric.AddOption) { + b.ExporterSentLogRecords.Add(ctx, val, opts...) +} + +func (b *TelemetryBuilder) RecordExporterSentMetricPoints(ctx context.Context, val int64, opts ...metric.AddOption) { + b.ExporterSentMetricPoints.Add(ctx, val, opts...) +} + +func (b *TelemetryBuilder) RecordExporterSentSpans(ctx context.Context, val int64, opts ...metric.AddOption) { + b.ExporterSentSpans.Add(ctx, val, opts...) +} diff --git a/exporter/exporterhelper/obsexporter.go b/exporter/exporterhelper/obsexporter.go index b54ad23b428..ee8c6945dfc 100644 --- a/exporter/exporterhelper/obsexporter.go +++ b/exporter/exporterhelper/obsexporter.go @@ -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) { @@ -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...)) } diff --git a/processor/batchprocessor/internal/metadata/generated_telemetry.go b/processor/batchprocessor/internal/metadata/generated_telemetry.go index 87bfa10e369..3aaeb40e6a9 100644 --- a/processor/batchprocessor/internal/metadata/generated_telemetry.go +++ b/processor/batchprocessor/internal/metadata/generated_telemetry.go @@ -107,3 +107,19 @@ func NewTelemetryBuilder(settings component.TelemetrySettings, options ...teleme errs = errors.Join(errs, err) return &builder, errs } + +func (b *TelemetryBuilder) RecordProcessorBatchBatchSendSize(ctx context.Context, val int64, opts ...metric.RecordOption) { + b.ProcessorBatchBatchSendSize.Record(ctx, val, opts...) +} + +func (b *TelemetryBuilder) RecordProcessorBatchBatchSendSizeBytes(ctx context.Context, val int64, opts ...metric.RecordOption) { + b.ProcessorBatchBatchSendSizeBytes.Record(ctx, val, opts...) +} + +func (b *TelemetryBuilder) RecordProcessorBatchBatchSizeTriggerSend(ctx context.Context, val int64, opts ...metric.AddOption) { + b.ProcessorBatchBatchSizeTriggerSend.Add(ctx, val, opts...) +} + +func (b *TelemetryBuilder) RecordProcessorBatchTimeoutTriggerSend(ctx context.Context, val int64, opts ...metric.AddOption) { + b.ProcessorBatchTimeoutTriggerSend.Add(ctx, val, opts...) +} diff --git a/processor/batchprocessor/metrics.go b/processor/batchprocessor/metrics.go index d9983022830..4a462eaaa6c 100644 --- a/processor/batchprocessor/metrics.go +++ b/processor/batchprocessor/metrics.go @@ -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)) } } diff --git a/processor/processorhelper/internal/metadata/generated_telemetry.go b/processor/processorhelper/internal/metadata/generated_telemetry.go index 432f3703f88..b5e37a14d08 100644 --- a/processor/processorhelper/internal/metadata/generated_telemetry.go +++ b/processor/processorhelper/internal/metadata/generated_telemetry.go @@ -3,6 +3,7 @@ package metadata import ( + "context" "errors" "go.opentelemetry.io/otel/metric" @@ -142,3 +143,51 @@ func NewTelemetryBuilder(settings component.TelemetrySettings, options ...teleme errs = errors.Join(errs, err) return &builder, errs } + +func (b *TelemetryBuilder) RecordProcessorAcceptedLogRecords(ctx context.Context, val int64, opts ...metric.AddOption) { + b.ProcessorAcceptedLogRecords.Add(ctx, val, opts...) +} + +func (b *TelemetryBuilder) RecordProcessorAcceptedMetricPoints(ctx context.Context, val int64, opts ...metric.AddOption) { + b.ProcessorAcceptedMetricPoints.Add(ctx, val, opts...) +} + +func (b *TelemetryBuilder) RecordProcessorAcceptedSpans(ctx context.Context, val int64, opts ...metric.AddOption) { + b.ProcessorAcceptedSpans.Add(ctx, val, opts...) +} + +func (b *TelemetryBuilder) RecordProcessorDroppedLogRecords(ctx context.Context, val int64, opts ...metric.AddOption) { + b.ProcessorDroppedLogRecords.Add(ctx, val, opts...) +} + +func (b *TelemetryBuilder) RecordProcessorDroppedMetricPoints(ctx context.Context, val int64, opts ...metric.AddOption) { + b.ProcessorDroppedMetricPoints.Add(ctx, val, opts...) +} + +func (b *TelemetryBuilder) RecordProcessorDroppedSpans(ctx context.Context, val int64, opts ...metric.AddOption) { + b.ProcessorDroppedSpans.Add(ctx, val, opts...) +} + +func (b *TelemetryBuilder) RecordProcessorInsertedLogRecords(ctx context.Context, val int64, opts ...metric.AddOption) { + b.ProcessorInsertedLogRecords.Add(ctx, val, opts...) +} + +func (b *TelemetryBuilder) RecordProcessorInsertedMetricPoints(ctx context.Context, val int64, opts ...metric.AddOption) { + b.ProcessorInsertedMetricPoints.Add(ctx, val, opts...) +} + +func (b *TelemetryBuilder) RecordProcessorInsertedSpans(ctx context.Context, val int64, opts ...metric.AddOption) { + b.ProcessorInsertedSpans.Add(ctx, val, opts...) +} + +func (b *TelemetryBuilder) RecordProcessorRefusedLogRecords(ctx context.Context, val int64, opts ...metric.AddOption) { + b.ProcessorRefusedLogRecords.Add(ctx, val, opts...) +} + +func (b *TelemetryBuilder) RecordProcessorRefusedMetricPoints(ctx context.Context, val int64, opts ...metric.AddOption) { + b.ProcessorRefusedMetricPoints.Add(ctx, val, opts...) +} + +func (b *TelemetryBuilder) RecordProcessorRefusedSpans(ctx context.Context, val int64, opts ...metric.AddOption) { + b.ProcessorRefusedSpans.Add(ctx, val, opts...) +} diff --git a/processor/processorhelper/obsreport.go b/processor/processorhelper/obsreport.go index 2a3bd0dc754..f35555dc693 100644 --- a/processor/processorhelper/obsreport.go +++ b/processor/processorhelper/obsreport.go @@ -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. diff --git a/receiver/receiverhelper/internal/metadata/generated_telemetry.go b/receiver/receiverhelper/internal/metadata/generated_telemetry.go index b0e676d1961..145ef64fa32 100644 --- a/receiver/receiverhelper/internal/metadata/generated_telemetry.go +++ b/receiver/receiverhelper/internal/metadata/generated_telemetry.go @@ -3,6 +3,7 @@ package metadata import ( + "context" "errors" "go.opentelemetry.io/otel/metric" @@ -100,3 +101,27 @@ func NewTelemetryBuilder(settings component.TelemetrySettings, options ...teleme errs = errors.Join(errs, err) return &builder, errs } + +func (b *TelemetryBuilder) RecordReceiverAcceptedLogRecords(ctx context.Context, val int64, opts ...metric.AddOption) { + b.ReceiverAcceptedLogRecords.Add(ctx, val, opts...) +} + +func (b *TelemetryBuilder) RecordReceiverAcceptedMetricPoints(ctx context.Context, val int64, opts ...metric.AddOption) { + b.ReceiverAcceptedMetricPoints.Add(ctx, val, opts...) +} + +func (b *TelemetryBuilder) RecordReceiverAcceptedSpans(ctx context.Context, val int64, opts ...metric.AddOption) { + b.ReceiverAcceptedSpans.Add(ctx, val, opts...) +} + +func (b *TelemetryBuilder) RecordReceiverRefusedLogRecords(ctx context.Context, val int64, opts ...metric.AddOption) { + b.ReceiverRefusedLogRecords.Add(ctx, val, opts...) +} + +func (b *TelemetryBuilder) RecordReceiverRefusedMetricPoints(ctx context.Context, val int64, opts ...metric.AddOption) { + b.ReceiverRefusedMetricPoints.Add(ctx, val, opts...) +} + +func (b *TelemetryBuilder) RecordReceiverRefusedSpans(ctx context.Context, val int64, opts ...metric.AddOption) { + b.ReceiverRefusedSpans.Add(ctx, val, opts...) +} diff --git a/receiver/receiverhelper/obsreport.go b/receiver/receiverhelper/obsreport.go index f3904061329..dda2579ec94 100644 --- a/receiver/receiverhelper/obsreport.go +++ b/receiver/receiverhelper/obsreport.go @@ -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...)) } diff --git a/receiver/scraperhelper/internal/metadata/generated_telemetry.go b/receiver/scraperhelper/internal/metadata/generated_telemetry.go index 204dcb0ff55..f7df6921b79 100644 --- a/receiver/scraperhelper/internal/metadata/generated_telemetry.go +++ b/receiver/scraperhelper/internal/metadata/generated_telemetry.go @@ -3,6 +3,7 @@ package metadata import ( + "context" "errors" "go.opentelemetry.io/otel/metric" @@ -72,3 +73,11 @@ func NewTelemetryBuilder(settings component.TelemetrySettings, options ...teleme errs = errors.Join(errs, err) return &builder, errs } + +func (b *TelemetryBuilder) RecordScraperErroredMetricPoints(ctx context.Context, val int64, opts ...metric.AddOption) { + b.ScraperErroredMetricPoints.Add(ctx, val, opts...) +} + +func (b *TelemetryBuilder) RecordScraperScrapedMetricPoints(ctx context.Context, val int64, opts ...metric.AddOption) { + b.ScraperScrapedMetricPoints.Add(ctx, val, opts...) +} diff --git a/receiver/scraperhelper/obsreport.go b/receiver/scraperhelper/obsreport.go index 1ffe87f77fe..66314d52442 100644 --- a/receiver/scraperhelper/obsreport.go +++ b/receiver/scraperhelper/obsreport.go @@ -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...)) } From f549fe2e08df2005a9b5502fbadacbc1bb55878f Mon Sep 17 00:00:00 2001 From: Alex Boten <223565+codeboten@users.noreply.github.com> Date: Mon, 19 Aug 2024 12:43:35 -0700 Subject: [PATCH 2/4] changelog Signed-off-by: Alex Boten <223565+codeboten@users.noreply.github.com> --- .../codeboten_wrap-otel-api-mdatagen.yaml | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 .chloggen/codeboten_wrap-otel-api-mdatagen.yaml diff --git a/.chloggen/codeboten_wrap-otel-api-mdatagen.yaml b/.chloggen/codeboten_wrap-otel-api-mdatagen.yaml new file mode 100644 index 00000000000..d6ac749722e --- /dev/null +++ b/.chloggen/codeboten_wrap-otel-api-mdatagen.yaml @@ -0,0 +1,25 @@ +# Use this changelog template to create an entry for release notes. + +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: enhancement + +# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver) +component: mdatagen + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Generate wrapper for OTel calls as a first step towards configurable attributes + +# One or more tracking issues or pull requests related to the change +issues: [10911] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: + +# Optional: The change log or logs in which this entry should be included. +# e.g. '[user]' or '[user, api]' +# Include 'user' if the change is relevant to end users. +# Include 'api' if there is a change to a library API. +# Default: '[user]' +change_logs: [] From 92d0d365285d6982df542f64cbbc4312a88b0e82 Mon Sep 17 00:00:00 2001 From: Alex Boten <223565+codeboten@users.noreply.github.com> Date: Wed, 21 Aug 2024 12:10:23 -0700 Subject: [PATCH 3/4] add test for generated methods Signed-off-by: Alex Boten <223565+codeboten@users.noreply.github.com> --- cmd/mdatagen/main_test.go | 122 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) diff --git a/cmd/mdatagen/main_test.go b/cmd/mdatagen/main_test.go index 1372c31d5c5..ed7346cb1cd 100644 --- a/cmd/mdatagen/main_test.go +++ b/cmd/mdatagen/main_test.go @@ -15,6 +15,7 @@ import ( "github.com/stretchr/testify/require" "go.opentelemetry.io/collector/component" + "go.opentelemetry.io/collector/pdata/pmetric" ) func TestRunContents(t *testing.T) { @@ -620,6 +621,127 @@ func LeveledMeter(settings component.TelemetrySettings, level configtelemetry.Le func Tracer(settings component.TelemetrySettings) trace.Tracer { return settings.TracerProvider.Tracer("") } +`, + }, + { + name: "foo component with internal telemetry", + md: metadata{ + Type: "foo", + Status: &Status{ + Stability: map[component.StabilityLevel][]string{ + component.StabilityLevelAlpha: {"metrics"}, + }, + Distributions: []string{"contrib"}, + Class: "receiver", + }, + Telemetry: telemetry{ + Metrics: map[metricName]metric{ + "metric1": { + Enabled: true, + Sum: &sum{ + Async: false, + Mono: Mono{true}, + MetricValueType: MetricValueType{ValueType: pmetric.NumberDataPointValueTypeInt}, + }, + }, + "metric2": { + Enabled: true, + Sum: &sum{ + Async: true, + Mono: Mono{false}, + MetricValueType: MetricValueType{ValueType: pmetric.NumberDataPointValueTypeDouble}, + }, + }, + }, + }, + }, + expected: `// Code generated by mdatagen. DO NOT EDIT. + +package metadata + +import ( + "context" + "errors" + + "go.opentelemetry.io/otel/metric" + "go.opentelemetry.io/otel/metric/noop" + "go.opentelemetry.io/otel/trace" + + "go.opentelemetry.io/collector/component" + "go.opentelemetry.io/collector/config/configtelemetry" +) + +func Meter(settings component.TelemetrySettings) metric.Meter { + return settings.MeterProvider.Meter("") +} + +func Tracer(settings component.TelemetrySettings) trace.Tracer { + return settings.TracerProvider.Tracer("") +} + +// TelemetryBuilder provides an interface for components to report telemetry +// as defined in metadata and user config. +type TelemetryBuilder struct { + meter metric.Meter + Metric1 metric.Int64Counter + Metric2 metric.Float64ObservableUpDownCounter + observeMetric2 func(context.Context, metric.Observer) error + level configtelemetry.Level +} + +// telemetryBuilderOption applies changes to default builder. +type telemetryBuilderOption func(*TelemetryBuilder) + +// WithLevel sets the current telemetry level for the component. +func WithLevel(lvl configtelemetry.Level) telemetryBuilderOption { + return func(builder *TelemetryBuilder) { + builder.level = lvl + } +} + +// WithMetric2Callback sets callback for observable Metric2 metric. +func WithMetric2Callback(cb func() float64, opts ...metric.ObserveOption) telemetryBuilderOption { + return func(builder *TelemetryBuilder) { + builder.observeMetric2 = func(_ context.Context, o metric.Observer) error { + o.ObserveFloat64(builder.Metric2, cb(), opts...) + return nil + } + } +} + +// NewTelemetryBuilder provides a struct with methods to update all internal telemetry +// for a component +func NewTelemetryBuilder(settings component.TelemetrySettings, options ...telemetryBuilderOption) (*TelemetryBuilder, error) { + builder := TelemetryBuilder{level: configtelemetry.LevelBasic} + for _, op := range options { + op(&builder) + } + var err, errs error + if builder.level >= configtelemetry.LevelBasic { + builder.meter = Meter(settings) + } else { + builder.meter = noop.Meter{} + } + builder.Metric1, err = builder.meter.Int64Counter( + "otelcol_metric1", + metric.WithDescription(""), + metric.WithUnit(""), + ) + errs = errors.Join(errs, err) + builder.Metric2, err = builder.meter.Float64ObservableUpDownCounter( + "otelcol_metric2", + metric.WithDescription(""), + metric.WithUnit(""), + ) + errs = errors.Join(errs, err) + _, err = builder.meter.RegisterCallback(builder.observeMetric2, builder.Metric2) + errs = errors.Join(errs, err) + return &builder, errs +} + +func (b *TelemetryBuilder) RecordMetric1(ctx context.Context, val int64, opts ...metric.AddOption) { + b.Metric1.Add(ctx, val, opts...) +} `, }, } From 88dca196a9beba09e6a5ae478bde6d796582189d Mon Sep 17 00:00:00 2001 From: Alex Boten <223565+codeboten@users.noreply.github.com> Date: Wed, 21 Aug 2024 12:17:00 -0700 Subject: [PATCH 4/4] update test Signed-off-by: Alex Boten <223565+codeboten@users.noreply.github.com> --- cmd/mdatagen/main_test.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/cmd/mdatagen/main_test.go b/cmd/mdatagen/main_test.go index ed7346cb1cd..e51c4f0def3 100644 --- a/cmd/mdatagen/main_test.go +++ b/cmd/mdatagen/main_test.go @@ -671,10 +671,15 @@ import ( "go.opentelemetry.io/collector/config/configtelemetry" ) +// Deprecated: [v0.108.0] use LeveledMeter instead. func Meter(settings component.TelemetrySettings) metric.Meter { return settings.MeterProvider.Meter("") } +func LeveledMeter(settings component.TelemetrySettings, level configtelemetry.Level) metric.Meter { + return settings.LeveledMeterProvider(level).Meter("") +} + func Tracer(settings component.TelemetrySettings) trace.Tracer { return settings.TracerProvider.Tracer("") }