Skip to content
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

[cmd/mdatagen] Pull new changes from contrib #9683

Merged
merged 6 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 27 additions & 0 deletions .chloggen/mdatagen-tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
component: cmd/mdatagen

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Generate the lifecycle tests for components by default.

# One or more tracking issues or pull requests related to the change
issues: [9683]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext: |
It's encouraged to have lifecycle tests for all components enadled, but they can be disabled if needed
in metadata.yaml with `skip_lifecycle: true` and `skip_shutdown: true` under `tests` section.

# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: []
7 changes: 0 additions & 7 deletions cmd/mdatagen/doc.go

This file was deleted.

3 changes: 2 additions & 1 deletion cmd/mdatagen/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ require (
go.opentelemetry.io/collector/component v0.96.0
go.opentelemetry.io/collector/confmap v0.96.0
go.opentelemetry.io/collector/confmap/provider/fileprovider v0.96.0
go.opentelemetry.io/collector/consumer v0.96.0
go.opentelemetry.io/collector/pdata v1.3.0
go.opentelemetry.io/collector/receiver v0.96.0
go.opentelemetry.io/collector/semconv v0.96.0
go.opentelemetry.io/otel/metric v1.24.0
go.opentelemetry.io/otel/trace v1.24.0
go.uber.org/goleak v1.3.0
go.uber.org/multierr v1.11.0
go.uber.org/zap v1.27.0
golang.org/x/text v0.14.0
Expand Down Expand Up @@ -41,7 +43,6 @@ require (
github.com/prometheus/common v0.48.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
go.opentelemetry.io/collector/config/configtelemetry v0.96.0 // indirect
go.opentelemetry.io/collector/consumer v0.96.0 // indirect
go.opentelemetry.io/otel v1.24.0 // indirect
go.opentelemetry.io/otel/exporters/prometheus v0.46.0 // indirect
go.opentelemetry.io/otel/sdk v1.24.0 // indirect
Expand Down
10 changes: 10 additions & 0 deletions cmd/mdatagen/internal/samplereceiver/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

// Generate a test metrics builder from a sample metrics set covering all configuration options.
//go:generate mdatagen metadata.yaml

// Deprecated: This package is moving to https://github.com/open-telemetry/opentelemetry-collector and will eventually be removed.
// Please see https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/30497
// This is a sample receiver package used to showcase how mdatagen is applied.
package samplereceiver // import "go.opentelemetry.io/collector/cmd/mdatagen/internal/samplereceiver"
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[comment]: <> (Code generated by mdatagen. DO NOT EDIT.)

# file
# sample

## Default Metrics

Expand Down
42 changes: 42 additions & 0 deletions cmd/mdatagen/internal/samplereceiver/factory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package samplereceiver // import "go.opentelemetry.io/collector/cmd/mdatagen/internal/samplereceiver"

import (
"context"

"go.opentelemetry.io/collector/cmd/mdatagen/internal/samplereceiver/internal/metadata"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/receiver"
)

// NewFactory returns a receiver.Factory for sample receiver.
func NewFactory() receiver.Factory {
return receiver.NewFactory(
metadata.Type,
func() component.Config { return &struct{}{} },
receiver.WithTraces(createTraces, metadata.TracesStability),
receiver.WithMetrics(createMetrics, metadata.MetricsStability),
receiver.WithLogs(createLogs, metadata.LogsStability))
}

func createTraces(context.Context, receiver.CreateSettings, component.Config, consumer.Traces) (receiver.Traces, error) {
return nopInstance, nil
}

func createMetrics(context.Context, receiver.CreateSettings, component.Config, consumer.Metrics) (receiver.Metrics, error) {
return nopInstance, nil
}

func createLogs(context.Context, receiver.CreateSettings, component.Config, consumer.Logs) (receiver.Logs, error) {
return nopInstance, nil
}

var nopInstance = &nopReceiver{}

type nopReceiver struct {
component.StartFunc
component.ShutdownFunc
}

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

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

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

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

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Sample metric metadata file with all available configurations.
# Sample metadata file with all available configurations for a receiver.

type: file
type: sample

sem_conv_version: 1.9.0

Expand All @@ -10,7 +10,9 @@ status:
development: [logs]
beta: [traces]
stable: [metrics]
distributions: [contrib]
distributions: []
codeowners:
active: [dmitryax]
warnings:
- Any additional information that should be brought to the consumer's attention

Expand Down
20 changes: 20 additions & 0 deletions cmd/mdatagen/internal/samplereceiver/metrics_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package samplereceiver

import (
"testing"

"github.com/stretchr/testify/require"

"go.opentelemetry.io/collector/cmd/mdatagen/internal/samplereceiver/internal/metadata"
"go.opentelemetry.io/collector/receiver/receivertest"
)

// TestGeneratedMetrics verifies that the internal/metadata API is generated correctly.
func TestGeneratedMetrics(t *testing.T) {
mb := metadata.NewMetricsBuilder(metadata.DefaultMetricsBuilderConfig(), receivertest.NewNopCreateSettings())
m := mb.Emit()
require.Equal(t, 0, m.ResourceMetrics().Len())
}
2 changes: 1 addition & 1 deletion cmd/mdatagen/lint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/stretchr/testify/require"
)

func Test_formatIdentifier(t *testing.T) {
func TestFormatIdentifier(t *testing.T) {
var tests = []struct {
input string
want string
Expand Down
3 changes: 2 additions & 1 deletion cmd/mdatagen/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ func (a attribute) TestValue() string {
type tests struct {
Config any `mapstructure:"config"`
SkipLifecycle bool `mapstructure:"skip_lifecycle"`
SkipShutdown bool `mapstructure:"skip_shutdown"`
ExpectConsumerError bool `mapstructure:"expect_consumer_error"`
}

Expand All @@ -225,7 +226,7 @@ type metadata struct {
// ShortFolderName is the shortened folder name of the component, removing class if present
ShortFolderName string `mapstructure:"-"`

Tests *tests `mapstructure:"tests"`
Tests tests `mapstructure:"tests"`
}

func setAttributesFullName(attrs map[attributeName]attribute) {
Expand Down
26 changes: 15 additions & 11 deletions cmd/mdatagen/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,34 @@ import (

"github.com/stretchr/testify/require"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/pmetric"
)

func Test_loadMetadata(t *testing.T) {
func TestLoadMetadata(t *testing.T) {
tests := []struct {
name string
want metadata
wantErr string
}{
{
name: "metadata-sample.yaml",
name: "internal/samplereceiver/metadata.yaml",
want: metadata{
Type: "file",
Type: "sample",
SemConvVersion: "1.9.0",
Status: &Status{
Class: "receiver",
Stability: map[string][]string{
"development": {"logs"},
"beta": {"traces"},
"stable": {"metrics"},
Stability: map[component.StabilityLevel][]string{
component.StabilityLevelDevelopment: {"logs"},
component.StabilityLevelBeta: {"traces"},
component.StabilityLevelStable: {"metrics"},
},
Distributions: []string{"contrib"},
Warnings: []string{"Any additional information that should be brought to the consumer's attention"},
Distributions: []string{},
Codeowners: &Codeowners{
Active: []string{"dmitryax"},
},
Warnings: []string{"Any additional information that should be brought to the consumer's attention"},
},
ResourceAttributes: map[attributeName]attribute{
"string.resource.attr": {
Expand Down Expand Up @@ -214,8 +218,8 @@ func Test_loadMetadata(t *testing.T) {
},
},
},
ScopeName: "go.opentelemetry.io/collector",
ShortFolderName: ".",
ScopeName: "go.opentelemetry.io/collector/samplereceiver",
ShortFolderName: "sample",
},
},
{
Expand Down