Skip to content

Commit

Permalink
[receiver/file] add file receiver skeleton 1/3 (open-telemetry#16827)
Browse files Browse the repository at this point in the history
* add file receiver skeleton
  • Loading branch information
pmcollins committed Jan 27, 2023
1 parent c3d68f6 commit e2dfaa5
Show file tree
Hide file tree
Showing 16 changed files with 748 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .chloggen/filereceiver.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: new_component

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: filereceiver

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: A receiver for reading the output of a File Exporter

# One or more tracking issues related to the change
issues: [14638]

# (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: This component is not yet enabled.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ receiver/dockerstatsreceiver/ @open-telemetry/collect
receiver/elasticsearchreceiver/ @open-telemetry/collector-contrib-approvers @djaglowski @binaryfissiongames
receiver/expvarreceiver/ @open-telemetry/collector-contrib-approvers @jamesmoessis @MovieStoreGuy
receiver/filelogreceiver/ @open-telemetry/collector-contrib-approvers @djaglowski
receiver/filereceiver/ @open-telemetry/collector-contrib-approvers @pmcollins @djaglowski
receiver/flinkmetricsreceiver/ @open-telemetry/collector-contrib-approvers @jonathanwamsley @djaglowski
receiver/fluentforwardreceiver/ @open-telemetry/collector-contrib-approvers @dmitryax
receiver/googlecloudpubsubreceiver/ @open-telemetry/collector-contrib-approvers @alexvanboxel
Expand Down
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/bug_report.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ body:
- receiver/dockerstats
- receiver/elasticsearch
- receiver/expvar
- receiver/file
- receiver/filelog
- receiver/flinkmetrics
- receiver/fluentforward
Expand Down
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/feature_request.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ body:
- receiver/dockerstats
- receiver/elasticsearch
- receiver/expvar
- receiver/file
- receiver/filelog
- receiver/flinkmetrics
- receiver/fluentforward
Expand Down
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/other.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ body:
- receiver/dockerstats
- receiver/elasticsearch
- receiver/expvar
- receiver/file
- receiver/filelog
- receiver/flinkmetrics
- receiver/fluentforward
Expand Down
1 change: 1 addition & 0 deletions receiver/filereceiver/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include ../../Makefile.Common
28 changes: 28 additions & 0 deletions receiver/filereceiver/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# File Receiver

| Status | |
|--------------------------|-----------------------|
| Stability | [development] |
| Supported pipeline types | metrics, traces, logs |
| Distributions | [contrib] |

The File Receiver reads the output of a
[File Exporter](https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/exporter/fileexporter),
converting that output to metrics, and sending the metrics down the pipeline.

Currently, the only file format supported is the File Exporter's JSON format.
Reading compressed output, rotated files, or telemetry other than metrics are not supported at this time.

## Getting Started

The following settings are required:

- `path` [no default]: the file containing the metrics written by a File Exporter.

## Example

```yaml
receivers:
file:
path: ./foo
```
38 changes: 38 additions & 0 deletions receiver/filereceiver/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http:https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package filereceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/filereceiver"

import (
"errors"

"go.opentelemetry.io/collector/component"
)

// Config defines the configuration for the file receiver.
type Config struct {
// Path of the file to read from. Path is relative to current directory.
Path string `mapstructure:"path"`
}

func createDefaultConfig() component.Config {
return &Config{}
}

func (c Config) Validate() error {
if c.Path == "" {
return errors.New("path cannot be empty")
}
return nil
}
76 changes: 76 additions & 0 deletions receiver/filereceiver/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http:https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package filereceiver

import (
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/confmap/confmaptest"
)

func TestLoadConfig_Validate_Invalid(t *testing.T) {
cfg := Config{}
assert.Error(t, cfg.Validate())
}

func TestConfig_Validate_Valid(t *testing.T) {
cfg := Config{Path: "/foo/bar"}
assert.NoError(t, cfg.Validate())
}

func TestLoadConfig(t *testing.T) {
cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
require.NoError(t, err)

tests := []struct {
id component.ID
expected component.Config
errorMessage string
}{
{
id: component.NewIDWithName(typeStr, ""),
errorMessage: "path cannot be empty",
},
{
id: component.NewIDWithName(typeStr, "1"),
expected: &Config{
Path: "./filename.json",
},
},
}

for _, tt := range tests {
t.Run(tt.id.String(), func(t *testing.T) {
factory := NewFactory()
cfg := factory.CreateDefaultConfig()

sub, err := cm.Sub(tt.id.String())
require.NoError(t, err)
require.NoError(t, component.UnmarshalConfig(sub, cfg))

if tt.errorMessage != "" {
assert.EqualError(t, component.ValidateConfig(cfg), tt.errorMessage)
return
}

assert.NoError(t, component.ValidateConfig(cfg))
assert.Equal(t, tt.expected, cfg)
})
}
}
45 changes: 45 additions & 0 deletions receiver/filereceiver/factory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http:https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package filereceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/filereceiver"

import (
"context"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/receiver"
)

const (
typeStr = "file"
stability = component.StabilityLevelDevelopment
)

func NewFactory() receiver.Factory {
return receiver.NewFactory(
typeStr,
createDefaultConfig,
receiver.WithMetrics(createMetricsFunc, component.StabilityLevelDevelopment),
)
}

func createMetricsFunc(
ctx context.Context,
settings receiver.CreateSettings,
config component.Config,
metrics consumer.Metrics,
) (receiver.Metrics, error) {
return fileReceiver{}, nil
}
35 changes: 35 additions & 0 deletions receiver/filereceiver/factory_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http:https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package filereceiver

import (
"context"
"testing"

"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/receiver/receivertest"
)

func TestNewFactory(t *testing.T) {
f := NewFactory()
_, err := f.CreateMetricsReceiver(
context.Background(),
receivertest.NewNopCreateSettings(),
f.CreateDefaultConfig(),
consumertest.NewNop(),
)
require.NoError(t, err)
}
40 changes: 40 additions & 0 deletions receiver/filereceiver/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
module github.com/open-telemetry/opentelemetry-collector-contrib/receiver/filereceiver

go 1.18

require (
github.com/stretchr/testify v1.8.1
go.opentelemetry.io/collector v0.70.0
go.opentelemetry.io/collector/component v0.70.0
go.opentelemetry.io/collector/confmap v0.70.0
go.opentelemetry.io/collector/consumer v0.70.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/knadh/koanf v1.5.0 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.opentelemetry.io/collector/featuregate v0.70.0 // indirect
go.opentelemetry.io/collector/pdata v1.0.0-rc4 // indirect
go.opentelemetry.io/otel v1.11.2 // indirect
go.opentelemetry.io/otel/metric v0.34.0 // indirect
go.opentelemetry.io/otel/trace v1.11.2 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
go.uber.org/zap v1.24.0 // indirect
golang.org/x/net v0.5.0 // indirect
golang.org/x/sys v0.4.0 // indirect
golang.org/x/text v0.6.0 // indirect
google.golang.org/genproto v0.0.0-20221118155620-16455021b5e6 // indirect
google.golang.org/grpc v1.52.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit e2dfaa5

Please sign in to comment.