-
Notifications
You must be signed in to change notification settings - Fork 108
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
Export internal metrics using OTEL metrics #1425
Open
marctc
wants to merge
5
commits into
main
Choose a base branch
from
otel_internal_metrics
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+216
−17
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
package otel | ||
|
||
import ( | ||
"context" | ||
"log/slog" | ||
"runtime" | ||
"time" | ||
|
||
"github.com/google/uuid" | ||
"go.opentelemetry.io/otel/attribute" | ||
instrument "go.opentelemetry.io/otel/metric" | ||
"go.opentelemetry.io/otel/sdk/metric" | ||
"go.opentelemetry.io/otel/sdk/resource" | ||
semconv "go.opentelemetry.io/otel/semconv/v1.19.0" | ||
|
||
"github.com/grafana/beyla/pkg/buildinfo" | ||
"github.com/grafana/beyla/pkg/internal/pipe/global" | ||
) | ||
|
||
// InternalMetricsReporter is an internal metrics Reporter that exports to OTEL | ||
type InternalMetricsReporter struct { | ||
ctx context.Context | ||
tracerFlushes instrument.Float64Histogram | ||
otelMetricExports instrument.Float64Counter | ||
otelMetricExportErrs instrument.Float64Counter | ||
otelTraceExports instrument.Float64Counter | ||
otelTraceExportErrs instrument.Float64Counter | ||
instrumentedProcesses instrument.Int64UpDownCounter | ||
beylaInfo instrument.Int64Gauge | ||
} | ||
|
||
func imlog() *slog.Logger { | ||
return slog.With("component", "otel.InternalMetricsReporter") | ||
} | ||
|
||
func NewInternalMetricsReporter(ctx context.Context, ctxInfo *global.ContextInfo, metrics *MetricsConfig) (*InternalMetricsReporter, error) { | ||
log := imlog() | ||
log.Debug("instantiating internal metrics exporter provider") | ||
exporter, err := InstantiateMetricsExporter(context.Background(), metrics, log) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
res := newResourceInternal(ctxInfo.HostID) | ||
provider, err := newInternalMeterProvider(res, &exporter, metrics.Interval) | ||
meter := provider.Meter("beyla_internal") | ||
|
||
if err != nil { | ||
log.Error("", "error", err) | ||
return nil, err | ||
} | ||
tracerFlushes, err := meter.Float64Histogram( | ||
"beyla.ebpf.tracer.flushes", | ||
instrument.WithDescription("Length of the groups of traces flushed from the eBPF tracer to the next pipeline stage"), | ||
instrument.WithUnit("1"), | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
otelMetricExports, err := meter.Float64Counter( | ||
"beyla.otel.metric.exports", | ||
instrument.WithDescription("Length of the metric batches submitted to the remote OTEL collector"), | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
otelMetricExportErrs, err := meter.Float64Counter( | ||
"beyla.otel.metric.export.errors", | ||
instrument.WithDescription("Error count on each failed OTEL metric export"), | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
otelTraceExports, err := meter.Float64Counter( | ||
"beyla.otel.trace.exports", | ||
instrument.WithDescription("Length of the trace batches submitted to the remote OTEL collector"), | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
otelTraceExportErrs, err := meter.Float64Counter( | ||
"beyla.otel.trace.export.errors", | ||
instrument.WithDescription("Error count on each failed OTEL trace export"), | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
instrumentedProcesses, err := meter.Int64UpDownCounter( | ||
"beyla.instrumented.processes", | ||
instrument.WithDescription("Instrumented processes by Beyla"), | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
beylaInfo, err := meter.Int64Gauge( | ||
"beyla.internal.build.info", | ||
instrument.WithDescription("A metric with a constant '1' value labeled by version, revision, branch, goversion from which Beyla was built, the goos and goarch for the build."), | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &InternalMetricsReporter{ | ||
ctx: ctx, | ||
tracerFlushes: tracerFlushes, | ||
otelMetricExports: otelMetricExports, | ||
otelMetricExportErrs: otelMetricExportErrs, | ||
otelTraceExports: otelTraceExports, | ||
otelTraceExportErrs: otelTraceExportErrs, | ||
instrumentedProcesses: instrumentedProcesses, | ||
beylaInfo: beylaInfo, | ||
}, nil | ||
} | ||
|
||
func newResourceInternal(hostID string) *resource.Resource { | ||
attrs := []attribute.KeyValue{ | ||
semconv.ServiceName("beyla-internal"), | ||
semconv.ServiceInstanceID(uuid.New().String()), | ||
semconv.TelemetrySDKLanguageKey.String(semconv.TelemetrySDKLanguageGo.Value.AsString()), | ||
// We set the SDK name as Beyla, so we can distinguish beyla generated metrics from other SDKs | ||
semconv.TelemetrySDKNameKey.String("beyla"), | ||
semconv.HostID(hostID), | ||
} | ||
|
||
return resource.NewWithAttributes(semconv.SchemaURL, attrs...) | ||
} | ||
|
||
func newInternalMeterProvider(res *resource.Resource, exporter *metric.Exporter, interval time.Duration) (*metric.MeterProvider, error) { | ||
meterProvider := metric.NewMeterProvider( | ||
metric.WithResource(res), | ||
metric.WithReader(metric.NewPeriodicReader(*exporter, metric.WithInterval(interval))), | ||
) | ||
return meterProvider, nil | ||
} | ||
|
||
func (p *InternalMetricsReporter) Start(ctx context.Context) { | ||
p.beylaInfo.Record(ctx, 1, instrument.WithAttributes(attribute.String("goarch", runtime.GOARCH), attribute.String("goos", runtime.GOOS), attribute.String("goversion", runtime.Version()), attribute.String("version", buildinfo.Version), attribute.String("revision", buildinfo.Revision))) | ||
} | ||
|
||
func (p *InternalMetricsReporter) TracerFlush(len int) { | ||
p.tracerFlushes.Record(p.ctx, float64(len)) | ||
} | ||
|
||
func (p *InternalMetricsReporter) OTELMetricExport(len int) { | ||
p.otelMetricExports.Add(p.ctx, float64(len)) | ||
} | ||
|
||
func (p *InternalMetricsReporter) OTELMetricExportError(err error) { | ||
p.otelMetricExportErrs.Add(p.ctx, 1, instrument.WithAttributes(attribute.String("error", err.Error()))) | ||
} | ||
|
||
func (p *InternalMetricsReporter) OTELTraceExport(len int) { | ||
p.otelTraceExports.Add(p.ctx, float64(len)) | ||
} | ||
|
||
func (p *InternalMetricsReporter) OTELTraceExportError(err error) { | ||
p.otelTraceExportErrs.Add(p.ctx, 1, instrument.WithAttributes(attribute.String("error", err.Error()))) | ||
} | ||
|
||
func (p *InternalMetricsReporter) PrometheusRequest(_, _ string) { | ||
} | ||
|
||
func (p *InternalMetricsReporter) InstrumentProcess(processName string) { | ||
p.instrumentedProcesses.Add(p.ctx, 1, instrument.WithAttributes(attribute.String("process_name", processName))) | ||
} | ||
|
||
func (p *InternalMetricsReporter) UninstrumentProcess(processName string) { | ||
p.instrumentedProcesses.Add(p.ctx, -1, instrument.WithAttributes(attribute.String("process_name", processName))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe to be less redundant, rename YAML property to
otel
oruse_otel
and the env var toBEYLA_INTERNAL_METRICS_OTEL
orBEYLA_INTERNAL_METRICS_USE_OTEL
?Even, to rely less on booleans for config, replace this property for something like
protocol
orexporter
, which can benone
,prometheus
orotel
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is not a bad idea, but that would imply breaking changes, no? basically we have to force everyone to set
exporter
andprometheus
config for internal metrics.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess we can document that breaking change in Beyla 2.0.
If we want to keep backwards compatibility, maybe we could default it to
prometheus
, and explain that it will only have effect if theprometheus
subsection is set.But anyway that was just a suggestion. I'm fine with current implementation.