-
Notifications
You must be signed in to change notification settings - Fork 232
feat: add configurable metric export interval to lambda telemetry API receiver #2114
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -81,6 +81,9 @@ type telemetryAPIReceiver struct { | |||||
| faaSMetricBuilders *FaaSMetricBuilders | ||||||
| currentFaasInvocationID string | ||||||
| logReport bool | ||||||
| exportInterval time.Duration | ||||||
| stopCh chan struct{} | ||||||
| wg sync.WaitGroup | ||||||
| } | ||||||
|
|
||||||
| func (r *telemetryAPIReceiver) Start(ctx context.Context, host component.Host) error { | ||||||
|
|
@@ -94,6 +97,11 @@ func (r *telemetryAPIReceiver) Start(ctx context.Context, host component.Host) e | |||||
| _ = r.httpServer.ListenAndServe() | ||||||
| }() | ||||||
|
|
||||||
| if r.exportInterval > 0 { | ||||||
| r.wg.Add(1) | ||||||
| go r.startMetricsExporter() | ||||||
| } | ||||||
|
|
||||||
| telemetryClient := telemetryapi.NewClient(r.logger) | ||||||
| if len(r.types) > 0 { | ||||||
| _, err := telemetryClient.Subscribe(ctx, r.types, r.extensionID, fmt.Sprintf("http://%s/", address)) | ||||||
|
|
@@ -106,9 +114,60 @@ func (r *telemetryAPIReceiver) Start(ctx context.Context, host component.Host) e | |||||
| } | ||||||
|
|
||||||
| func (r *telemetryAPIReceiver) Shutdown(ctx context.Context) error { | ||||||
| close(r.stopCh) | ||||||
| r.wg.Wait() | ||||||
| if r.exportInterval > 0 { | ||||||
| r.flushMetrics(ctx) | ||||||
| } | ||||||
| return nil | ||||||
| } | ||||||
|
|
||||||
| func (r *telemetryAPIReceiver) startMetricsExporter() { | ||||||
| defer r.wg.Done() | ||||||
| ticker := time.NewTicker(r.exportInterval) | ||||||
| defer ticker.Stop() | ||||||
|
|
||||||
| for { | ||||||
| select { | ||||||
| case <-ticker.C: | ||||||
| r.flushMetrics(context.Background()) | ||||||
| case <-r.stopCh: | ||||||
| return | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| func (r *telemetryAPIReceiver) flushMetrics(ctx context.Context) { | ||||||
| r.mu.Lock() | ||||||
| defer r.mu.Unlock() | ||||||
| r.flushMetricsLocked(ctx) | ||||||
| } | ||||||
|
|
||||||
| func (r *telemetryAPIReceiver) flushMetricsLocked(ctx context.Context) { | ||||||
| metric := pmetric.NewMetrics() | ||||||
| resourceMetric := metric.ResourceMetrics().AppendEmpty() | ||||||
| r.resource.CopyTo(resourceMetric.Resource()) | ||||||
| scopeMetric := resourceMetric.ScopeMetrics().AppendEmpty() | ||||||
| scopeMetric.Scope().SetName(scopeName) | ||||||
| scopeMetric.SetSchemaUrl(semconv.SchemaURL) | ||||||
|
|
||||||
| ts := pcommon.NewTimestampFromTime(time.Now()) | ||||||
| r.faaSMetricBuilders.coldstartsMetric.AppendDataPoints(scopeMetric, ts) | ||||||
| r.faaSMetricBuilders.errorsMetric.AppendDataPoints(scopeMetric, ts) | ||||||
| r.faaSMetricBuilders.timeoutsMetric.AppendDataPoints(scopeMetric, ts) | ||||||
| r.faaSMetricBuilders.initDurationMetric.AppendDataPoints(scopeMetric, ts) | ||||||
| r.faaSMetricBuilders.memUsageMetric.AppendDataPoints(scopeMetric, ts) | ||||||
| r.faaSMetricBuilders.invocationsMetric.AppendDataPoints(scopeMetric, ts) | ||||||
| r.faaSMetricBuilders.invokeDurationMetric.AppendDataPoints(scopeMetric, ts) | ||||||
|
|
||||||
| if metric.MetricCount() > 0 { | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Edge case here but think of the following scenario:
receivers:
telemetryapi:
types:
- platform
metrics_temporality: "delta"
...
service:
pipelines:
traces:
receivers: [telemetryapi]
exporters: [otlp]This leds to Whenever the ticker fires and
I believe that is problematic, because now following calls inside that same
So the check for
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, even when properly using an actual metrics pipeline We run the risk of just producing metrics with 0 datapoints when temporality is delta, which seems useless / wasteful. |
||||||
| err := r.nextMetrics.ConsumeMetrics(ctx, metric) | ||||||
| if err != nil { | ||||||
| r.logger.Error("error flushing metrics", zap.Error(err)) | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| func newSpanID() pcommon.SpanID { | ||||||
| sid := pcommon.SpanID{} | ||||||
| _, _ = crand.Read(sid[:]) | ||||||
|
|
@@ -198,13 +257,9 @@ func (r *telemetryAPIReceiver) httpHandler(w http.ResponseWriter, req *http.Requ | |||||
| } | ||||||
| // Metrics | ||||||
| if r.nextMetrics != nil { | ||||||
| if metrics, err := r.createMetrics(slice); err == nil { | ||||||
| if metrics.MetricCount() > 0 { | ||||||
| err := r.nextMetrics.ConsumeMetrics(context.Background(), metrics) | ||||||
| if err != nil { | ||||||
| r.logger.Error("error receiving metrics", zap.Error(err)) | ||||||
| } | ||||||
| } | ||||||
| r.recordMetrics(slice) | ||||||
| if r.exportInterval == 0 { | ||||||
| r.flushMetricsLocked(context.Background()) | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
|
|
@@ -233,37 +288,22 @@ func (r *telemetryAPIReceiver) getRecordRequestId(record map[string]interface{}) | |||||
| return "" | ||||||
| } | ||||||
|
|
||||||
| func (r *telemetryAPIReceiver) createMetrics(slice []event) (pmetric.Metrics, error) { | ||||||
| metric := pmetric.NewMetrics() | ||||||
| resourceMetric := metric.ResourceMetrics().AppendEmpty() | ||||||
| r.resource.CopyTo(resourceMetric.Resource()) | ||||||
| scopeMetric := resourceMetric.ScopeMetrics().AppendEmpty() | ||||||
| scopeMetric.Scope().SetName(scopeName) | ||||||
| scopeMetric.SetSchemaUrl(semconv.SchemaURL) | ||||||
|
|
||||||
| func (r *telemetryAPIReceiver) recordMetrics(slice []event) { | ||||||
| for _, el := range slice { | ||||||
| r.logger.Debug(fmt.Sprintf("Event: %s", el.Type), zap.Any("event", el)) | ||||||
| record, ok := el.Record.(map[string]any) | ||||||
| if !ok { | ||||||
| continue | ||||||
| } | ||||||
| ts, err := time.Parse(time.RFC3339, el.Time) | ||||||
| if err != nil { | ||||||
| continue | ||||||
| } | ||||||
|
|
||||||
| switch el.Type { | ||||||
| case string(telemetryapi.PlatformInitStart): | ||||||
| r.faaSMetricBuilders.coldstartsMetric.Add(1) | ||||||
| r.faaSMetricBuilders.coldstartsMetric.AppendDataPoints(scopeMetric, pcommon.NewTimestampFromTime(ts)) | ||||||
| case string(telemetryapi.PlatformInitReport): | ||||||
| status, _ := record["status"].(string) | ||||||
| if status == telemetryFailureStatus || status == telemetryErrorStatus { | ||||||
| r.faaSMetricBuilders.errorsMetric.Add(1) | ||||||
| r.faaSMetricBuilders.errorsMetric.AppendDataPoints(scopeMetric, pcommon.NewTimestampFromTime(ts)) | ||||||
| } else if status == telemetryTimeoutStatus { | ||||||
| r.faaSMetricBuilders.timeoutsMetric.Add(1) | ||||||
| r.faaSMetricBuilders.timeoutsMetric.AppendDataPoints(scopeMetric, pcommon.NewTimestampFromTime(ts)) | ||||||
| } | ||||||
|
|
||||||
| metrics, ok := record["metrics"].(map[string]any) | ||||||
|
|
@@ -277,7 +317,6 @@ func (r *telemetryAPIReceiver) createMetrics(slice []event) (pmetric.Metrics, er | |||||
| } | ||||||
|
|
||||||
| r.faaSMetricBuilders.initDurationMetric.Record(durationMs / 1000.0) | ||||||
| r.faaSMetricBuilders.initDurationMetric.AppendDataPoints(scopeMetric, pcommon.NewTimestampFromTime(ts)) | ||||||
| case string(telemetryapi.PlatformReport): | ||||||
| metrics, ok := record["metrics"].(map[string]any) | ||||||
| if !ok { | ||||||
|
|
@@ -287,20 +326,16 @@ func (r *telemetryAPIReceiver) createMetrics(slice []event) (pmetric.Metrics, er | |||||
| maxMemoryUsedMb, ok := metrics["maxMemoryUsedMB"].(float64) | ||||||
| if ok { | ||||||
| r.faaSMetricBuilders.memUsageMetric.Record(maxMemoryUsedMb * 1000000.0) | ||||||
| r.faaSMetricBuilders.memUsageMetric.AppendDataPoints(scopeMetric, pcommon.NewTimestampFromTime(ts)) | ||||||
| } | ||||||
| case string(telemetryapi.PlatformRuntimeDone): | ||||||
| status, _ := record["status"].(string) | ||||||
|
|
||||||
| if status == telemetrySuccessStatus { | ||||||
| r.faaSMetricBuilders.invocationsMetric.Add(1) | ||||||
| r.faaSMetricBuilders.invocationsMetric.AppendDataPoints(scopeMetric, pcommon.NewTimestampFromTime(ts)) | ||||||
| } else if status == telemetryFailureStatus || status == telemetryErrorStatus { | ||||||
| r.faaSMetricBuilders.errorsMetric.Add(1) | ||||||
| r.faaSMetricBuilders.errorsMetric.AppendDataPoints(scopeMetric, pcommon.NewTimestampFromTime(ts)) | ||||||
| } else if status == telemetryTimeoutStatus { | ||||||
| r.faaSMetricBuilders.timeoutsMetric.Add(1) | ||||||
| r.faaSMetricBuilders.timeoutsMetric.AppendDataPoints(scopeMetric, pcommon.NewTimestampFromTime(ts)) | ||||||
| } | ||||||
|
|
||||||
| metrics, ok := record["metrics"].(map[string]any) | ||||||
|
|
@@ -311,11 +346,9 @@ func (r *telemetryAPIReceiver) createMetrics(slice []event) (pmetric.Metrics, er | |||||
| durationMs, ok := metrics["durationMs"].(float64) | ||||||
| if ok { | ||||||
| r.faaSMetricBuilders.invokeDurationMetric.Record(durationMs / 1000.0) | ||||||
| r.faaSMetricBuilders.invokeDurationMetric.AppendDataPoints(scopeMetric, pcommon.NewTimestampFromTime(ts)) | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| return metric, nil | ||||||
| } | ||||||
|
|
||||||
| func (r *telemetryAPIReceiver) createLogs(slice []event) (plog.Logs, error) { | ||||||
|
|
@@ -675,6 +708,8 @@ func newTelemetryAPIReceiver( | |||||
| resource: r, | ||||||
| faaSMetricBuilders: NewFaaSMetricBuilders(pcommon.NewTimestampFromTime(time.Now()), getMetricsTemporality(cfg)), | ||||||
| logReport: cfg.LogReport, | ||||||
| exportInterval: time.Duration(cfg.ExportInterval) * time.Millisecond, | ||||||
| stopCh: make(chan struct{}), | ||||||
| }, nil | ||||||
| } | ||||||
|
|
||||||
|
|
||||||
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.
Should we be shutting down the httpServer here first?