Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[mdatagen] Generate wrappers for recording telemetry metrics #12182

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .chloggen/tel_metric_attributes.yaml
Original file line number Diff line number Diff line change
@@ -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 wrappers for telemetry metrics as a first step towards configurable attributes

# One or more tracking issues or pull requests related to the change
issues: [10801]

# (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: []
1 change: 1 addition & 0 deletions cmd/mdatagen/internal/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ func TestRunContents(t *testing.T) {
},
{
yml: "with_telemetry.yaml",
wantMetricsContext: true,
wantStatusGenerated: true,
wantTelemetryGenerated: true,
wantReadmeGenerated: true,
Expand Down
2 changes: 1 addition & 1 deletion cmd/mdatagen/internal/samplereceiver/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func createMetrics(ctx context.Context, set receiver.Settings, _ component.Confi
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.

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

18 changes: 11 additions & 7 deletions cmd/mdatagen/internal/templates/telemetry.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,9 @@ package {{ .Package }}

import (
{{- if .Telemetry.Metrics }}
{{- range $_, $metric := .Telemetry.Metrics }}
{{- if $metric.Data.Async }}
"context"
"sync"

{{- break}}
{{- end }}
{{- end }}
"errors"
"sync"
{{- end }}

"go.opentelemetry.io/otel/metric"
Expand Down Expand Up @@ -143,3 +137,13 @@ func NewTelemetryBuilder(settings component.TelemetrySettings, options ...Teleme
}

{{- end }}

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

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

{{- end }}
{{- end }}
8 changes: 2 additions & 6 deletions cmd/mdatagen/internal/templates/telemetrytest_test.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,8 @@ func TestSetupTelemetry(t *testing.T) {
{{- end }}

{{- range $name, $metric := .Telemetry.Metrics }}
{{- if not $metric.Data.Async }}
{{- if eq $metric.Data.Type "Sum" }}
tb.{{ $name.Render }}.Add(context.Background(), 1)
{{- else }}
tb.{{ $name.Render }}.Record(context.Background(), 1)
{{- end }}
{{- if (and (not $metric.Optional) (not $metric.Data.Async)) }}
tb.Record{{ $name.Render }}(context.Background(), 1)
{{- end }}
{{- end }}

Expand Down
2 changes: 1 addition & 1 deletion docs/coding-guidelines.md
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ To record the measurement, you can then call the metric stored in the telemetry
builder:

```go
tsp.telemetry.ProcessorTailsamplingSamplingdecisionLatency.Record(ctx, ...)
tsp.telemetry.RecordProcessorTailsamplingSamplingdecisionLatency(ctx, ...)
```

## Resource Usage
Expand Down

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

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

46 changes: 19 additions & 27 deletions exporter/exporterhelper/internal/obs_report_sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ type obsReportSender[K request.Request] struct {
component.StartFunc
component.ShutdownFunc

spanName string
tracer trace.Tracer
spanAttrs trace.SpanStartEventOption
metricAttr metric.MeasurementOption
itemsSentInst metric.Int64Counter
itemsFailedInst metric.Int64Counter
next sender.Sender[K]
spanName string
tracer trace.Tracer
signal pipeline.Signal
tb *metadata.TelemetryBuilder
spanAttrs trace.SpanStartEventOption
metricAttr metric.MeasurementOption
next sender.Sender[K]
}

func newObsReportSender[K request.Request](set exporter.Settings, signal pipeline.Signal, next sender.Sender[K]) (sender.Sender[K], error) {
Expand All @@ -60,25 +60,13 @@ func newObsReportSender[K request.Request](set exporter.Settings, signal pipelin
or := &obsReportSender[K]{
spanName: ExporterKey + spanNameSep + idStr + spanNameSep + signal.String(),
tracer: metadata.Tracer(set.TelemetrySettings),
signal: signal,
tb: telemetryBuilder,
spanAttrs: trace.WithAttributes(expAttr, attribute.String(DataTypeKey, signal.String())),
metricAttr: metric.WithAttributeSet(attribute.NewSet(expAttr)),
next: next,
}

switch signal {
case pipeline.SignalTraces:
or.itemsSentInst = telemetryBuilder.ExporterSentSpans
or.itemsFailedInst = telemetryBuilder.ExporterSendFailedSpans

case pipeline.SignalMetrics:
or.itemsSentInst = telemetryBuilder.ExporterSentMetricPoints
or.itemsFailedInst = telemetryBuilder.ExporterSendFailedMetricPoints

case pipeline.SignalLogs:
or.itemsSentInst = telemetryBuilder.ExporterSentLogRecords
or.itemsFailedInst = telemetryBuilder.ExporterSendFailedLogRecords
}

return or, nil
}

Expand All @@ -105,12 +93,16 @@ func (ors *obsReportSender[K]) endOp(ctx context.Context, numLogRecords int, err
numSent, numFailedToSend := toNumItems(numLogRecords, err)

// No metrics recorded for profiles.
if ors.itemsSentInst != nil {
ors.itemsSentInst.Add(ctx, numSent, ors.metricAttr)
}
// No metrics recorded for profiles.
if ors.itemsFailedInst != nil {
ors.itemsFailedInst.Add(ctx, numFailedToSend, ors.metricAttr)
switch ors.signal {
case pipeline.SignalTraces:
ors.tb.RecordExporterSentSpans(ctx, numSent, ors.metricAttr)
ors.tb.RecordExporterSendFailedSpans(ctx, numFailedToSend, ors.metricAttr)
case pipeline.SignalMetrics:
ors.tb.RecordExporterSentMetricPoints(ctx, numSent, ors.metricAttr)
ors.tb.RecordExporterSendFailedMetricPoints(ctx, numFailedToSend, ors.metricAttr)
case pipeline.SignalLogs:
ors.tb.RecordExporterSentLogRecords(ctx, numSent, ors.metricAttr)
ors.tb.RecordExporterSendFailedLogRecords(ctx, numFailedToSend, ors.metricAttr)
}

span := trace.SpanFromContext(ctx)
Expand Down
27 changes: 13 additions & 14 deletions exporter/exporterhelper/internal/queuebatch/obs_queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ const (
// obsQueue is a helper to add observability to a queue.
type obsQueue[T request.Request] struct {
Queue[T]
tb *metadata.TelemetryBuilder
metricAttr metric.MeasurementOption
enqueueFailedInst metric.Int64Counter
tb *metadata.TelemetryBuilder
metricAttr metric.MeasurementOption
signal pipeline.Signal
}

func newObsQueue[T request.Request](set Settings[T], delegate Queue[T]) (Queue[T], error) {
Expand Down Expand Up @@ -58,15 +58,7 @@ func newObsQueue[T request.Request](set Settings[T], delegate Queue[T]) (Queue[T
Queue: delegate,
tb: tb,
metricAttr: metric.WithAttributeSet(attribute.NewSet(exporterAttr)),
}

switch set.Signal {
case pipeline.SignalTraces:
or.enqueueFailedInst = tb.ExporterEnqueueFailedSpans
case pipeline.SignalMetrics:
or.enqueueFailedInst = tb.ExporterEnqueueFailedMetricPoints
case pipeline.SignalLogs:
or.enqueueFailedInst = tb.ExporterEnqueueFailedLogRecords
signal: set.Signal,
}

return or, nil
Expand All @@ -83,8 +75,15 @@ func (or *obsQueue[T]) Offer(ctx context.Context, req T) error {
numItems := req.ItemsCount()
err := or.Queue.Offer(ctx, req)
// No metrics recorded for profiles, remove enqueueFailedInst check with nil when profiles metrics available.
if err != nil && or.enqueueFailedInst != nil {
or.enqueueFailedInst.Add(ctx, int64(numItems), or.metricAttr)
if err != nil {
switch or.signal {
case pipeline.SignalTraces:
or.tb.RecordExporterEnqueueFailedSpans(ctx, int64(numItems), or.metricAttr)
case pipeline.SignalMetrics:
or.tb.RecordExporterEnqueueFailedMetricPoints(ctx, int64(numItems), or.metricAttr)
case pipeline.SignalLogs:
or.tb.RecordExporterEnqueueFailedLogRecords(ctx, int64(numItems), or.metricAttr)
}
}
return err
}

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

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 @@ -53,11 +53,11 @@ 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, bpt.processorAttr)
bpt.telemetryBuilder.RecordProcessorBatchBatchSizeTriggerSend(bpt.exportCtx, 1, bpt.processorAttr)
case triggerTimeout:
bpt.telemetryBuilder.ProcessorBatchTimeoutTriggerSend.Add(bpt.exportCtx, 1, bpt.processorAttr)
bpt.telemetryBuilder.RecordProcessorBatchTimeoutTriggerSend(bpt.exportCtx, 1, bpt.processorAttr)
}

bpt.telemetryBuilder.ProcessorBatchBatchSendSize.Record(bpt.exportCtx, sent, bpt.processorAttr)
bpt.telemetryBuilder.ProcessorBatchBatchSendSizeBytes.Record(bpt.exportCtx, bytes, bpt.processorAttr)
bpt.telemetryBuilder.RecordProcessorBatchBatchSendSize(bpt.exportCtx, sent, bpt.processorAttr)
bpt.telemetryBuilder.RecordProcessorBatchBatchSendSizeBytes(bpt.exportCtx, bytes, bpt.processorAttr)
}

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

Loading
Loading