Skip to content

Commit

Permalink
[connector/*] generate status from metadata.yaml (#22800)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mwear authored Jun 1, 2023
1 parent a25c147 commit 921e850
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 33 deletions.
16 changes: 13 additions & 3 deletions cmd/mdatagen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
},
Expand Down
17 changes: 17 additions & 0 deletions cmd/mdatagen/templates/readme.md.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand All @@ -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 }}
<!-- end autogenerated section -->
4 changes: 2 additions & 2 deletions cmd/mdatagen/templates/status.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
)
35 changes: 17 additions & 18 deletions connector/countconnector/README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
# Count Connector
<!-- status autogenerated section -->
| 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
<!-- end autogenerated section -->

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].
Expand Down Expand Up @@ -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
4 changes: 3 additions & 1 deletion connector/countconnector/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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))

Expand Down
16 changes: 7 additions & 9 deletions connector/countconnector/factory.go
Original file line number Diff line number Diff line change
@@ -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 (
Expand All @@ -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"
Expand All @@ -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),
)
}

Expand Down
14 changes: 14 additions & 0 deletions connector/countconnector/internal/metadata/generated_status.go

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

7 changes: 7 additions & 0 deletions connector/countconnector/metadata.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
type: count

status:
class: connector
stability:
development: [traces_to_metrics, metrics_to_metrics, logs_to_metrics]
distributions: [contrib, sumo]

0 comments on commit 921e850

Please sign in to comment.