From 921e850997ec50d488639446dc8776680e1d067c Mon Sep 17 00:00:00 2001 From: Matthew Wear Date: Thu, 1 Jun 2023 11:08:55 -0700 Subject: [PATCH] [connector/*] generate status from metadata.yaml (#22800) This PR adds the ability to generate status from metadata.yaml for connectors. The format for connectors differs in that stability is per connection rather than per signal as it is for other components. In addition to the code to generate status, this uses the count connector as a demonstration of how this will work in the real world. --- cmd/mdatagen/main.go | 16 +++++++-- cmd/mdatagen/templates/readme.md.tmpl | 17 +++++++++ cmd/mdatagen/templates/status.go.tmpl | 4 +-- connector/countconnector/README.md | 35 +++++++++---------- connector/countconnector/config_test.go | 4 ++- connector/countconnector/factory.go | 16 ++++----- .../internal/metadata/generated_status.go | 14 ++++++++ connector/countconnector/metadata.yaml | 7 ++++ 8 files changed, 80 insertions(+), 33 deletions(-) create mode 100644 connector/countconnector/internal/metadata/generated_status.go create mode 100644 connector/countconnector/metadata.yaml diff --git a/cmd/mdatagen/main.go b/cmd/mdatagen/main.go index 78face14bde0..e5f9ac4ec8ea 100644 --- a/cmd/mdatagen/main.go +++ b/cmd/mdatagen/main.go @@ -134,9 +134,19 @@ func templatize(tmplFile string, md metadata) *template.Template { } return false }, - "stringsJoin": strings.Join, - "casesTitle": cases.Title(language.English).String, - "inc": func(i int) int { return i + 1 }, + "stringsJoin": strings.Join, + "stringsSplit": strings.Split, + "casesTitle": cases.Title(language.English).String, + "toCamelCase": func(s string) string { + caser := cases.Title(language.English).String + parts := strings.Split(s, "_") + result := "" + for _, part := range parts { + result += caser(part) + } + return result + }, + "inc": func(i int) int { return i + 1 }, "distroURL": func(name string) string { return distros[name] }, diff --git a/cmd/mdatagen/templates/readme.md.tmpl b/cmd/mdatagen/templates/readme.md.tmpl index 08073a95c0d0..6c646f14e89d 100644 --- a/cmd/mdatagen/templates/readme.md.tmpl +++ b/cmd/mdatagen/templates/readme.md.tmpl @@ -3,11 +3,13 @@ | Status | | | ------------- |-----------| {{- $class := .Status.Class }} +{{- if ne $class "connector" }} {{- $idx := 0 }} {{- range $stability, $value := .Status.Stability }} | {{ if not $idx }}Stability{{ else }} {{ end }} | [{{ $stability }}]{{ if ne $class "extension" }}: {{ stringsJoin $value ", " }} {{ end }} | {{- $idx = inc $idx }} {{- end }} +{{- end}} {{- if ne $class "cmd" }} | Distributions | [{{ stringsJoin .Status.SortedDistributions "], [" }}] | {{- end }} @@ -20,5 +22,20 @@ {{- range .Status.SortedDistributions }} [{{.}}]: {{ distroURL . }} {{- end }} +{{- if eq $class "connector"}} +## Supported Pipeline Types +| [Exporter Pipeline Type] | [Receiver Pipeline Type] | [Stability Level] | +| ------------------------ | ------------------------ | ----------------- | +{{- range $stability, $pipelines := .Status.Stability }} +{{- range $pipeline := $pipelines }} +{{- $parts := stringsSplit $pipeline "_to_" }} +| {{index $parts 0}} | {{index $parts 1}} | [{{$stability}}] | +{{- end }} +{{- end }} + +[Exporter Pipeline Type]: /~https://github.com/open-telemetry/opentelemetry-collector/blob/main/connector/README.md#exporter-pipeline-type +[Receiver Pipeline Type]: /~https://github.com/open-telemetry/opentelemetry-collector/blob/main/connector/README.md#receiver-pipeline-type +[Stability Level]: /~https://github.com/open-telemetry/opentelemetry-collector#stability-levels +{{- end }} {{- end }} \ No newline at end of file diff --git a/cmd/mdatagen/templates/status.go.tmpl b/cmd/mdatagen/templates/status.go.tmpl index 041b26ad3ab9..d91529049404 100644 --- a/cmd/mdatagen/templates/status.go.tmpl +++ b/cmd/mdatagen/templates/status.go.tmpl @@ -9,8 +9,8 @@ import ( const ( Type = "{{ .Type }}" {{- range $stability, $signals := .Status.Stability }} - {{- range $signal := $signals }} - {{ casesTitle $signal }}Stability = component.StabilityLevel{{ casesTitle $stability }} + {{- range $signal := $signals }} + {{ toCamelCase $signal }}Stability = component.StabilityLevel{{ casesTitle $stability }} {{- end }} {{- end }} ) \ No newline at end of file diff --git a/connector/countconnector/README.md b/connector/countconnector/README.md index 3be72b6125cf..24721cc942b4 100644 --- a/connector/countconnector/README.md +++ b/connector/countconnector/README.md @@ -1,21 +1,25 @@ # Count Connector + +| Status | | +| ------------- |-----------| +| Distributions | [contrib], [sumo] | -| Status | | -| ------------------------ | --------------------------------------------------------- | -| Stability | [in development] | -| Supported pipeline types | See [Supported Pipeline Types](#supported-pipeline-types) | -| Distributions | [contrib], [sumo] | - -The `count` connector can be used to count spans, span events, metrics, data points, and log records. - +[development]: /~https://github.com/open-telemetry/opentelemetry-collector#development +[contrib]: /~https://github.com/open-telemetry/opentelemetry-collector-releases/tree/main/distributions/otelcol-contrib +[sumo]: /~https://github.com/SumoLogic/sumologic-otel-collector ## Supported Pipeline Types +| [Exporter Pipeline Type] | [Receiver Pipeline Type] | [Stability Level] | +| ------------------------ | ------------------------ | ----------------- | +| traces | metrics | [development] | +| metrics | metrics | [development] | +| logs | metrics | [development] | -| [Exporter Pipeline Type] | [Receiver Pipeline Type] | -| ------------------------ | ------------------------ | -| traces | metrics | -| metrics | metrics | -| logs | metrics | +[Exporter Pipeline Type]: /~https://github.com/open-telemetry/opentelemetry-collector/blob/main/connector/README.md#exporter-pipeline-type +[Receiver Pipeline Type]: /~https://github.com/open-telemetry/opentelemetry-collector/blob/main/connector/README.md#receiver-pipeline-type +[Stability Level]: /~https://github.com/open-telemetry/opentelemetry-collector#stability-levels + +The `count` connector can be used to count spans, span events, metrics, data points, and log records. ## Configuration If you are not already familiar with connectors, you may find it helpful to first visit the [Connectors README]. @@ -268,9 +272,4 @@ service: exporters: [bar/counts_only] ``` -[in development]: /~https://github.com/open-telemetry/opentelemetry-collector#in-development -[contrib]: /~https://github.com/open-telemetry/opentelemetry-collector-releases/tree/main/distributions/otelcol-contrib -[sumo]: /~https://github.com/SumoLogic/sumologic-otel-collector [Connectors README]: /~https://github.com/open-telemetry/opentelemetry-collector/blob/main/connector/README.md -[Exporter Pipeline Type]: /~https://github.com/open-telemetry/opentelemetry-collector/blob/main/connector/README.md#exporter-pipeline-type -[Receiver Pipeline Type]: /~https://github.com/open-telemetry/opentelemetry-collector/blob/main/connector/README.md#receiver-pipeline-type diff --git a/connector/countconnector/config_test.go b/connector/countconnector/config_test.go index 3c1a431a6250..1ee183f3b094 100644 --- a/connector/countconnector/config_test.go +++ b/connector/countconnector/config_test.go @@ -12,6 +12,8 @@ import ( "github.com/stretchr/testify/require" "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/confmap/confmaptest" + + "github.com/open-telemetry/opentelemetry-collector-contrib/connector/countconnector/internal/metadata" ) func TestLoadConfig(t *testing.T) { @@ -331,7 +333,7 @@ func TestLoadConfig(t *testing.T) { factory := NewFactory() cfg := factory.CreateDefaultConfig() - sub, err := cm.Sub(component.NewIDWithName(typeStr, tc.name).String()) + sub, err := cm.Sub(component.NewIDWithName(metadata.Type, tc.name).String()) require.NoError(t, err) require.NoError(t, component.UnmarshalConfig(sub, cfg)) diff --git a/connector/countconnector/factory.go b/connector/countconnector/factory.go index 2ed11f35896c..271f9bbb9e56 100644 --- a/connector/countconnector/factory.go +++ b/connector/countconnector/factory.go @@ -1,6 +1,8 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 +//go:generate mdatagen metadata.yaml + package countconnector // import "github.com/open-telemetry/opentelemetry-collector-contrib/connector/countconnector" import ( @@ -10,6 +12,7 @@ import ( "go.opentelemetry.io/collector/connector" "go.opentelemetry.io/collector/consumer" + "github.com/open-telemetry/opentelemetry-collector-contrib/connector/countconnector/internal/metadata" "github.com/open-telemetry/opentelemetry-collector-contrib/internal/filter/expr" "github.com/open-telemetry/opentelemetry-collector-contrib/internal/filter/filterottl" "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl" @@ -20,19 +23,14 @@ import ( "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/ottlspanevent" ) -const ( - typeStr = "count" - stability = component.StabilityLevelDevelopment -) - // NewFactory returns a ConnectorFactory. func NewFactory() connector.Factory { return connector.NewFactory( - typeStr, + metadata.Type, createDefaultConfig, - connector.WithTracesToMetrics(createTracesToMetrics, component.StabilityLevelDevelopment), - connector.WithMetricsToMetrics(createMetricsToMetrics, component.StabilityLevelDevelopment), - connector.WithLogsToMetrics(createLogsToMetrics, component.StabilityLevelDevelopment), + connector.WithTracesToMetrics(createTracesToMetrics, metadata.TracesToMetricsStability), + connector.WithMetricsToMetrics(createMetricsToMetrics, metadata.MetricsToMetricsStability), + connector.WithLogsToMetrics(createLogsToMetrics, metadata.LogsToMetricsStability), ) } diff --git a/connector/countconnector/internal/metadata/generated_status.go b/connector/countconnector/internal/metadata/generated_status.go new file mode 100644 index 000000000000..614bd16ddcdc --- /dev/null +++ b/connector/countconnector/internal/metadata/generated_status.go @@ -0,0 +1,14 @@ +// Code generated by mdatagen. DO NOT EDIT. + +package metadata + +import ( + "go.opentelemetry.io/collector/component" +) + +const ( + Type = "count" + TracesToMetricsStability = component.StabilityLevelDevelopment + MetricsToMetricsStability = component.StabilityLevelDevelopment + LogsToMetricsStability = component.StabilityLevelDevelopment +) diff --git a/connector/countconnector/metadata.yaml b/connector/countconnector/metadata.yaml new file mode 100644 index 000000000000..48358f5bef00 --- /dev/null +++ b/connector/countconnector/metadata.yaml @@ -0,0 +1,7 @@ +type: count + +status: + class: connector + stability: + development: [traces_to_metrics, metrics_to_metrics, logs_to_metrics] + distributions: [contrib, sumo]