Skip to content

Commit

Permalink
Storage Extension 1/N (open-telemetry#2883)
Browse files Browse the repository at this point in the history
* WIP - Add storage extension framework

* Cleaned up initial implmenetation. Still much to do

* Remove extraneous dependency

* go tidy

* rm gopls from go.mod

* Fix typo in TODO

* Finish thought in readme

* Document storage.Extension, fix doc issues

* Run make gendependabot and make gotidy

* Use core nop pattern, add context to methods, clean up comments
  • Loading branch information
djaglowski authored and pmatyjasek-sumo committed Apr 28, 2021
1 parent 94ef22b commit 5a21a73
Show file tree
Hide file tree
Showing 8 changed files with 1,573 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ updates:
directory: "/extension/observer/k8sobserver"
schedule:
interval: "weekly"
- package-ecosystem: "gomod"
directory: "/extension/storage"
schedule:
interval: "weekly"
- package-ecosystem: "gomod"
directory: "/internal/aws"
schedule:
Expand Down
41 changes: 41 additions & 0 deletions exporter/stackdriverexporter/go.sum

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions extension/storage/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include ../../Makefile.Common
19 changes: 19 additions & 0 deletions extension/storage/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Storage

A storage extension persists state beyond the collector process. Other components can request a storage client from the storage extension and use it to manage state.

The `storage.Extension` interface extends `component.Extension` by adding the following method:
```
GetClient(component.Kind, configmodels.NamedEntity) (Client, error)
```

The `storage.Client` interface contains the following methods:
```
Get(string) ([]byte, error)
Set(string, []byte) error
Delete(string) error
```
Note: All methods should return error only if a problem occurred. (For example, if a file is no longer accessible, or if a remote service is unavailable.)

# TODO Sample code
- Document component expectations
10 changes: 10 additions & 0 deletions extension/storage/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module github.com/open-telemetry/opentelemetry-collector-contrib/extension/storage

go 1.14

require (
github.com/stretchr/testify v1.7.0
go.etcd.io/bbolt v1.3.3
go.opentelemetry.io/collector v0.23.0
go.uber.org/zap v1.16.0
)
1,408 changes: 1,408 additions & 0 deletions extension/storage/go.sum

Large diffs are not rendered by default.

41 changes: 41 additions & 0 deletions extension/storage/nop_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// 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 storage

import "context"

type nopClient struct{}

var nopClientInstance Client = &nopClient{}

// NewNopClient returns a nop client
func NewNopClient() Client {
return nopClientInstance
}

// Get does nothing, and returns nil, nil
func (c nopClient) Get(context.Context, string) ([]byte, error) {
return nil, nil // no result, but no problem
}

// Set does nothing and returns nil
func (c nopClient) Set(context.Context, string, []byte) error {
return nil // no problem
}

// Delete does nothing and returns nil
func (c nopClient) Delete(context.Context, string) error {
return nil // no problem
}
49 changes: 49 additions & 0 deletions extension/storage/storage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// 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 storage

import (
"context"

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

// Extension is the interface that storage extensions must implement
type Extension interface {
component.Extension

// GetClient will create a client for use by the specified component.
// The component can use the client to manage state
// TODO change parameters to new config.ComponentID
// https://github.com/open-telemetry/opentelemetry-collector/pull/2869
GetClient(context.Context, component.Kind, configmodels.NamedEntity) (Client, error)
}

// Client is the interface that storage clients must implement
// All methods should return error only if a problem occurred
type Client interface {

// Get will retrieve data from storage that corresponds to the
// specified key. It should return nil, nil if not found
Get(context.Context, string) ([]byte, error)

// Set will store data. The data can be retrieved by the same
// component after a process restart, using the same key
Set(context.Context, string, []byte) error

// Delete will delete data associated with the specified key
Delete(context.Context, string) error
}

0 comments on commit 5a21a73

Please sign in to comment.