-
Notifications
You must be signed in to change notification settings - Fork 503
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
metrics: send metrics to the otel collector endpoint when active
Introduce a meter provider to the buildx cli that will send metrics to the otel-collector included in docker desktop if enabled. This is primarily used for sending usage metrics to the desktop application if this has been enabled. In a future version, we might have the option to send these same usage statistics to a user-provided otel metrics endpoint for personal use. This introduces a single metric which is the cli count for build and bake along with the command name and a few additional attributes. Signed-off-by: Jonathan A. Sternberg <jonathan.sternberg@docker.com>
- Loading branch information
1 parent
1cdefbe
commit c1a1d48
Showing
477 changed files
with
1,901 additions
and
1,839 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package metrics | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/docker/cli/cli/command" | ||
"github.com/moby/buildkit/util/tracing/detect" | ||
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc" | ||
"go.opentelemetry.io/otel/metric" | ||
sdkmetric "go.opentelemetry.io/otel/sdk/metric" | ||
"go.opentelemetry.io/otel/sdk/metric/metricdata" | ||
) | ||
|
||
const ( | ||
otelConfigFieldName = "otel" | ||
shutdownTimeout = time.Second | ||
) | ||
|
||
// ReportFunc is invoked to signal the metrics should be sent to the | ||
// desired endpoint. It should be invoked on application shutdown. | ||
type ReportFunc func() | ||
|
||
// MeterProvider returns a MeterProvider suitable for CLI usage. | ||
// The primary difference between this metric reader and a more typical | ||
// usage is that metric reporting only happens once when ReportFunc | ||
// is invoked. | ||
func MeterProvider(cli command.Cli) (metric.MeterProvider, ReportFunc, error) { | ||
exp, err := dockerOtelExporter(cli) | ||
if exp == nil || err != nil { | ||
return sdkmetric.NewMeterProvider(), func() {}, err | ||
} | ||
|
||
// Use delta temporality because, since this is a CLI program, we can never | ||
// know the cumulative value. | ||
reader := sdkmetric.NewManualReader( | ||
sdkmetric.WithTemporalitySelector(deltaTemporality), | ||
) | ||
mp := sdkmetric.NewMeterProvider( | ||
sdkmetric.WithResource(detect.Resource()), | ||
sdkmetric.WithReader(reader), | ||
) | ||
return mp, reportFunc(reader, exp), nil | ||
} | ||
|
||
// reportFunc returns a ReportFunc for collecting ResourceMetrics and then | ||
// exporting them to the configured Exporter. | ||
func reportFunc(reader sdkmetric.Reader, exp sdkmetric.Exporter) ReportFunc { | ||
return func() { | ||
ctx, cancel := context.WithTimeout(context.Background(), shutdownTimeout) | ||
defer cancel() | ||
|
||
var rm metricdata.ResourceMetrics | ||
if err := reader.Collect(ctx, &rm); err != nil { | ||
// Error when collecting metrics. Do not send any. | ||
return | ||
} | ||
_ = exp.Export(ctx, &rm) | ||
_ = exp.Shutdown(ctx) | ||
} | ||
} | ||
|
||
// dockerOtelExporter reads the CLI metadata to determine an OTLP exporter | ||
// endpoint for docker metrics to be sent. | ||
// | ||
// This location, configuration, and usage is hard-coded as part of | ||
// sending usage statistics so this metric reporting is not meant to be | ||
// user facing. | ||
func dockerOtelExporter(cli command.Cli) (sdkmetric.Exporter, error) { | ||
meta, err := cli.ContextStore().GetMetadata(cli.CurrentContext()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var otelCfg interface{} | ||
switch m := meta.Metadata.(type) { | ||
case command.DockerContext: | ||
otelCfg = m.AdditionalFields[otelConfigFieldName] | ||
case map[string]interface{}: | ||
otelCfg = m[otelConfigFieldName] | ||
} | ||
|
||
if otelCfg == nil { | ||
return nil, nil | ||
} | ||
|
||
otelMap, ok := otelCfg.(map[string]interface{}) | ||
if !ok { | ||
return nil, fmt.Errorf( | ||
"unexpected type for field %q: %T (expected: %T)", | ||
otelConfigFieldName, | ||
otelCfg, | ||
otelMap, | ||
) | ||
} | ||
|
||
// keys from https://opentelemetry.io/docs/concepts/sdk-configuration/otlp-exporter-configuration/ | ||
endpoint, _ := otelMap["OTEL_EXPORTER_OTLP_ENDPOINT"].(string) | ||
if endpoint == "" { | ||
return nil, nil | ||
} | ||
|
||
// Hardcoded endpoint from the endpoint. | ||
exp, err := otlpmetricgrpc.New(context.Background(), | ||
otlpmetricgrpc.WithEndpoint(endpoint), | ||
otlpmetricgrpc.WithInsecure()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return exp, nil | ||
} | ||
|
||
// deltaTemporality sets the Temporality of every instrument to delta. | ||
func deltaTemporality(_ sdkmetric.InstrumentKind) metricdata.Temporality { | ||
return metricdata.DeltaTemporality | ||
} |
Oops, something went wrong.