-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add a file receiver component to receive pipeline data written to file
- Loading branch information
Showing
16 changed files
with
854 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
include ../../Makefile.Common |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.