Skip to content

Commit

Permalink
New file receiver (#7840)
Browse files Browse the repository at this point in the history
* Add a file receiver component to receive pipeline data written to file
  • Loading branch information
atoulme authored Jul 20, 2022
1 parent 960b841 commit 55831bc
Show file tree
Hide file tree
Showing 16 changed files with 854 additions and 3 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ receiver/nginxreceiver/ @open-telemetry/collector-c
receiver/nsxtreceiver/ @open-telemetry/collector-contrib-approvers @dashpole @schmikei
receiver/opencensusreceiver/ @open-telemetry/collector-contrib-approvers @open-telemetry/collector-approvers
receiver/podmanreceiver/ @open-telemetry/collector-contrib-approvers @rogercoll
receiver/otlpjsonfilereceiver/ @open-telemetry/collector-contrib-approvers @djaglowski @atoulme
receiver/postgresqlreceiver/ @open-telemetry/collector-contrib-approvers @djaglowski
receiver/prometheusexecreceiver/ @open-telemetry/collector-contrib-approvers @dmitryax
receiver/prometheusreceiver/ @open-telemetry/collector-contrib-approvers @Aneurysm9 @dashpole
Expand Down
3 changes: 3 additions & 0 deletions cmd/configschema/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,7 @@ require (
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/nginxreceiver v0.55.0 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/nsxtreceiver v0.55.0 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/opencensusreceiver v0.55.0 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/otlpjsonfilereceiver v0.55.0 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/podmanreceiver v0.55.0 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/postgresqlreceiver v0.55.0 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/prometheusexecreceiver v0.55.0 // indirect
Expand Down Expand Up @@ -835,6 +836,8 @@ replace github.com/open-telemetry/opentelemetry-collector-contrib/receiver/expva

replace github.com/open-telemetry/opentelemetry-collector-contrib/receiver/filelogreceiver => ../../receiver/filelogreceiver

replace github.com/open-telemetry/opentelemetry-collector-contrib/receiver/otlpjsonfilereceiver => ../../receiver/otlpjsonfilereceiver

replace github.com/open-telemetry/opentelemetry-collector-contrib/receiver/flinkmetricsreceiver => ../../receiver/flinkmetricsreceiver

replace github.com/open-telemetry/opentelemetry-collector-contrib/receiver/fluentforwardreceiver => ../../receiver/fluentforwardreceiver
Expand Down
7 changes: 5 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ require (
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/nginxreceiver v0.55.0
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/nsxtreceiver v0.55.0
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/opencensusreceiver v0.55.0
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/otlpjsonfilereceiver v0.55.0
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/podmanreceiver v0.55.0
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/postgresqlreceiver v0.55.0
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/prometheusexecreceiver v0.55.0
Expand Down Expand Up @@ -843,8 +844,6 @@ replace github.com/open-telemetry/opentelemetry-collector-contrib/receiver/expva

replace github.com/open-telemetry/opentelemetry-collector-contrib/receiver/filelogreceiver => ./receiver/filelogreceiver

replace github.com/open-telemetry/opentelemetry-collector-contrib/receiver/fluentforwardreceiver => ./receiver/fluentforwardreceiver

replace github.com/open-telemetry/opentelemetry-collector-contrib/receiver/googlecloudspannerreceiver => ./receiver/googlecloudspannerreceiver

replace github.com/open-telemetry/opentelemetry-collector-contrib/receiver/googlecloudpubsubreceiver => ./receiver/googlecloudpubsubreceiver
Expand All @@ -865,6 +864,8 @@ replace github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8scl

replace github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8seventsreceiver => ./receiver/k8seventsreceiver

replace github.com/open-telemetry/opentelemetry-collector-contrib/receiver/fluentforwardreceiver => ./receiver/fluentforwardreceiver

replace github.com/open-telemetry/opentelemetry-collector-contrib/receiver/kafkametricsreceiver => ./receiver/kafkametricsreceiver

replace github.com/open-telemetry/opentelemetry-collector-contrib/receiver/kafkareceiver => ./receiver/kafkareceiver
Expand All @@ -885,6 +886,8 @@ replace github.com/open-telemetry/opentelemetry-collector-contrib/receiver/nsxtr

replace github.com/open-telemetry/opentelemetry-collector-contrib/receiver/opencensusreceiver => ./receiver/opencensusreceiver

replace github.com/open-telemetry/opentelemetry-collector-contrib/receiver/otlpjsonfilereceiver => ./receiver/otlpjsonfilereceiver

replace github.com/open-telemetry/opentelemetry-collector-contrib/receiver/podmanreceiver => ./receiver/podmanreceiver

replace github.com/open-telemetry/opentelemetry-collector-contrib/receiver/postgresqlreceiver => ./receiver/postgresqlreceiver
Expand Down
2 changes: 2 additions & 0 deletions internal/components/components.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ import (
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/nginxreceiver"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/nsxtreceiver"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/opencensusreceiver"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/otlpjsonfilereceiver"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/podmanreceiver"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/postgresqlreceiver"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/prometheusexecreceiver"
Expand Down Expand Up @@ -237,6 +238,7 @@ func Components() (component.Factories, error) {
nsxtreceiver.NewFactory(),
nginxreceiver.NewFactory(),
opencensusreceiver.NewFactory(),
otlpjsonfilereceiver.NewFactory(),
otlpreceiver.NewFactory(),
podmanreceiver.NewFactory(),
postgresqlreceiver.NewFactory(),
Expand Down
9 changes: 9 additions & 0 deletions internal/components/receivers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/adapter"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/carbonreceiver"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/filelogreceiver"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/otlpjsonfilereceiver"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/prometheusreceiver"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/syslogreceiver"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/tcplogreceiver"
Expand Down Expand Up @@ -205,6 +206,14 @@ func TestDefaultReceivers(t *testing.T) {
{
receiver: "otlp",
},
{
receiver: "otlpjsonfile",
getConfigFn: func() config.Receiver {
cfg := rcvrFactories["otlpjsonfile"].CreateDefaultConfig().(*otlpjsonfilereceiver.Config)
cfg.Include = []string{"/tmp/*.log"}
return cfg
},
},
{
receiver: "podman_stats",
skipLifecyle: true, // Requires a running podman daemon
Expand Down
2 changes: 1 addition & 1 deletion pkg/stanza/operator/helper/duration.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (

// Duration is the representation of a length of time
type Duration struct {
time.Duration
time.Duration `mapstructure:"duration"`
}

// NewDuration creates a new duration from a time
Expand Down
1 change: 1 addition & 0 deletions receiver/otlpjsonfilereceiver/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include ../../Makefile.Common
38 changes: 38 additions & 0 deletions receiver/otlpjsonfilereceiver/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# OTLP JSON File Receiver

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

This receiver will read pipeline data from JSON files. The data is written in
[Protobuf JSON
encoding](https://developers.google.com/protocol-buffers/docs/proto3#json)
using [OpenTelemetry
protocol](https://github.com/open-telemetry/opentelemetry-proto).

The receiver will watch the directory and read files. If a file is updated or added,
the receiver will read it in its entirety again.

Please note that there is no guarantee that exact field names will remain stable.
This intended for primarily for debugging Collector without setting up backends.

Supported pipeline types: traces, metrics, logs

## Getting Started

The following settings are required:

- `include`: set a glob path of files to include in data collection

Example:

```yaml
receivers:
otlpjsonfile:
include:
- "/var/log/*.log"
exclude:
- "/var/log/example.log"
```
18 changes: 18 additions & 0 deletions receiver/otlpjsonfilereceiver/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// 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://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 otlpjsonfilereceiver implements a receiver that can be used by the
// Opentelemetry collector to receive logs, traces and metrics from files
// See https://github.com/open-telemetry/opentelemetry-specification/blob/main/experimental/serialization/json.md
package otlpjsonfilereceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/otlpjsonfilereceiver"
147 changes: 147 additions & 0 deletions receiver/otlpjsonfilereceiver/file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
// 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://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 otlpjsonfilereceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/otlpjsonfilereceiver"

import (
"context"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/obsreport"
"go.opentelemetry.io/collector/pdata/plog"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/pdata/ptrace"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/adapter"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/fileconsumer"
)

const (
typeStr = "otlpjsonfile"
stability = component.StabilityLevelAlpha
transport = "file"
)

// NewFactory creates a factory for file receiver
func NewFactory() component.ReceiverFactory {
return component.NewReceiverFactory(
typeStr,
createDefaultConfig,
component.WithMetricsReceiverAndStabilityLevel(createMetricsReceiver, stability),
component.WithLogsReceiverAndStabilityLevel(createLogsReceiver, stability),
component.WithTracesReceiverAndStabilityLevel(createTracesReceiver, stability))
}

type Config struct {
config.ReceiverSettings `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct
fileconsumer.Config `mapstructure:",squash"`
}

func createDefaultConfig() config.Receiver {
return &Config{
Config: *fileconsumer.NewConfig(),
ReceiverSettings: config.NewReceiverSettings(config.NewComponentID(typeStr)),
}
}

type receiver struct {
input *fileconsumer.Input
id config.ComponentID
}

func (f *receiver) Start(ctx context.Context, host component.Host) error {
storageClient, err := adapter.GetStorageClient(ctx, f.id, component.KindReceiver, host)
if err != nil {
return err
}
return f.input.Start(storageClient)
}

func (f *receiver) Shutdown(ctx context.Context) error {
return f.input.Stop()
}

func createLogsReceiver(_ context.Context, settings component.ReceiverCreateSettings, configuration config.Receiver, logs consumer.Logs) (component.LogsReceiver, error) {
logsUnmarshaler := plog.NewJSONUnmarshaler()
obsrecv := obsreport.NewReceiver(obsreport.ReceiverSettings{
ReceiverID: configuration.ID(),
Transport: transport,
ReceiverCreateSettings: settings,
})
input, err := configuration.(*Config).Config.Build(settings.Logger.Sugar(), func(ctx context.Context, attrs *fileconsumer.FileAttributes, token []byte) {
ctx = obsrecv.StartMetricsOp(ctx)
l, err := logsUnmarshaler.UnmarshalLogs(token)
if err != nil {
obsrecv.EndLogsOp(ctx, typeStr, 0, err)
} else {
err = logs.ConsumeLogs(ctx, l)
obsrecv.EndLogsOp(ctx, typeStr, l.LogRecordCount(), err)
}
})
if err != nil {
return nil, err
}

return &receiver{input: input, id: configuration.ID()}, nil
}

func createMetricsReceiver(_ context.Context, settings component.ReceiverCreateSettings, configuration config.Receiver, metrics consumer.Metrics) (component.MetricsReceiver, error) {
metricsUnmarshaler := pmetric.NewJSONUnmarshaler()
obsrecv := obsreport.NewReceiver(obsreport.ReceiverSettings{
ReceiverID: configuration.ID(),
Transport: transport,
ReceiverCreateSettings: settings,
})
input, err := configuration.(*Config).Config.Build(settings.Logger.Sugar(), func(ctx context.Context, attrs *fileconsumer.FileAttributes, token []byte) {
ctx = obsrecv.StartMetricsOp(ctx)
m, err := metricsUnmarshaler.UnmarshalMetrics(token)
if err != nil {
obsrecv.EndMetricsOp(ctx, typeStr, 0, err)
} else {
err = metrics.ConsumeMetrics(ctx, m)
obsrecv.EndMetricsOp(ctx, typeStr, m.MetricCount(), err)
}
})
if err != nil {
return nil, err
}

return &receiver{input: input, id: configuration.ID()}, nil
}

func createTracesReceiver(ctx context.Context, settings component.ReceiverCreateSettings, configuration config.Receiver, traces consumer.Traces) (component.TracesReceiver, error) {
tracesUnmarshaler := ptrace.NewJSONUnmarshaler()
obsrecv := obsreport.NewReceiver(obsreport.ReceiverSettings{
ReceiverID: configuration.ID(),
Transport: transport,
ReceiverCreateSettings: settings,
})
input, err := configuration.(*Config).Config.Build(settings.Logger.Sugar(), func(ctx context.Context, attrs *fileconsumer.FileAttributes, token []byte) {
ctx = obsrecv.StartTracesOp(ctx)
t, err := tracesUnmarshaler.UnmarshalTraces(token)
if err != nil {
obsrecv.EndTracesOp(ctx, typeStr, 0, err)
} else {
err = traces.ConsumeTraces(ctx, t)
obsrecv.EndTracesOp(ctx, typeStr, t.SpanCount(), err)
}
})
if err != nil {
return nil, err
}

return &receiver{input: input, id: configuration.ID()}, nil
}
Loading

0 comments on commit 55831bc

Please sign in to comment.