Skip to content

Commit

Permalink
Export a GetStorageClient function from pkg/stanza/adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
djaglowski committed Aug 30, 2022
1 parent 5fe4c6c commit 68a9269
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 30 deletions.
22 changes: 13 additions & 9 deletions pkg/stanza/adapter/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,34 @@ import (
"fmt"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/extension/experimental/storage"
)

func (r *receiver) setStorageClient(ctx context.Context, host component.Host) error {
if r.storageID == nil {
r.storageClient = storage.NewNopClient()
return nil
func GetStorageClient(ctx context.Context, host component.Host, storageID *config.ComponentID, componentID config.ComponentID) (storage.Client, error) {
if storageID == nil {
return storage.NewNopClient(), nil
}

extension, ok := host.GetExtensions()[*r.storageID]
extension, ok := host.GetExtensions()[*storageID]
if !ok {
return fmt.Errorf("storage extension '%s' not found", r.storageID)
return nil, fmt.Errorf("storage extension '%s' not found", storageID)
}

storageExtension, ok := extension.(storage.Extension)
if !ok {
return fmt.Errorf("non-storage extension '%s' found", r.storageID)
return nil, fmt.Errorf("non-storage extension '%s' found", storageID)
}

client, err := storageExtension.GetClient(ctx, component.KindReceiver, r.id, "")
return storageExtension.GetClient(ctx, component.KindReceiver, componentID, "")

}

func (r *receiver) setStorageClient(ctx context.Context, host component.Host) error {
client, err := GetStorageClient(ctx, host, r.storageID, r.id)
if err != nil {
return err
}

r.storageClient = client
return nil
}
23 changes: 2 additions & 21 deletions receiver/otlpjsonfilereceiver/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,16 @@ package otlpjsonfilereceiver // import "github.com/open-telemetry/opentelemetry-

import (
"context"
"fmt"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/extension/experimental/storage"
"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"
)

Expand Down Expand Up @@ -66,7 +65,7 @@ type receiver struct {
}

func (f *receiver) Start(ctx context.Context, host component.Host) error {
storageClient, err := f.storageClient(ctx, host)
storageClient, err := adapter.GetStorageClient(ctx, host, f.storageID, f.id)
if err != nil {
return err
}
Expand All @@ -77,24 +76,6 @@ func (f *receiver) Shutdown(ctx context.Context) error {
return f.input.Stop()
}

func (f *receiver) storageClient(ctx context.Context, host component.Host) (storage.Client, error) {
if f.storageID == nil {
return storage.NewNopClient(), nil
}

extension, ok := host.GetExtensions()[*f.storageID]
if !ok {
return nil, fmt.Errorf("storage extension '%s' not found", f.storageID)
}

storageExtension, ok := extension.(storage.Extension)
if !ok {
return nil, fmt.Errorf("non-storage extension '%s' found", f.storageID)
}

return storageExtension.GetClient(ctx, component.KindReceiver, f.id, "")
}

func createLogsReceiver(_ context.Context, settings component.ReceiverCreateSettings, configuration config.Receiver, logs consumer.Logs) (component.LogsReceiver, error) {
logsUnmarshaler := plog.NewJSONUnmarshaler()
obsrecv := obsreport.NewReceiver(obsreport.ReceiverSettings{
Expand Down
2 changes: 2 additions & 0 deletions receiver/otlpjsonfilereceiver/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@ require (
golang.org/x/net v0.0.0-20220225172249-27dd8689420f // indirect
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
golang.org/x/text v0.3.7 // indirect
gonum.org/v1/gonum v0.11.0 // indirect
google.golang.org/genproto v0.0.0-20211208223120-3a66f561d7aa // indirect
google.golang.org/grpc v1.48.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

Expand Down
4 changes: 4 additions & 0 deletions receiver/otlpjsonfilereceiver/go.sum

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

0 comments on commit 68a9269

Please sign in to comment.