This repository has been archived by the owner on Nov 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
223 additions
and
28 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
|
||
## Snap Plugin Go Library: Collector Plugin Example | ||
Here you will find an example plugin that cover the basics for writing a collector plugin. | ||
|
||
## Plugin Naming, Files, and Directory | ||
For your collector plugin, create a new repository and name your plugin project using the following format: | ||
|
||
>snap-plugin-[plugin-type]-[plugin-name] | ||
For example: | ||
>snap-plugin-collector-rand | ||
|
||
Example files and directory structure: | ||
``` | ||
snap-plugin-[plugin-type]-[plugin-name] | ||
|--[plugin-name] | ||
|--[plugin-name].go | ||
|--[plugin-name]_test.go | ||
|--main.go | ||
``` | ||
|
||
For example: | ||
``` | ||
snap-plugin-collector-rand | ||
|--rand | ||
|--rand.go | ||
|--rand_test.go | ||
|--main.go | ||
``` | ||
|
||
* The main file (for example `main.go`) allows each plugin to be a stand-alone binary executable. The main file will include: pluginName and pluginVersion. | ||
* The [plugin-name] folder (for example `rand`) will include all files to implement the appropriate interface methods | ||
* Your [plugin-name] folder will also include your test files. | ||
|
||
|
||
|
||
## Interface Methods | ||
|
||
In order to write a plugin for Snap, it is necessary to define a few methods to satisfy the appropriate interfaces. These interfaces must be defined for a collector plugin: | ||
|
||
|
||
```go | ||
// Plugin is an interface shared by all plugins and must implemented by each plugin to communicate with Snap. | ||
type Plugin interface { | ||
GetConfigPolicy() (ConfigPolicy, error) | ||
} | ||
|
||
// Collector is a plugin which is the source of new data in the Snap pipeline. | ||
type Collector interface { | ||
Plugin | ||
|
||
GetMetricTypes(Config) ([]Metric, error) | ||
CollectMetrics([]Metric) ([]Metric, error) | ||
} | ||
``` | ||
The interface is slightly different depending on what type (collector, processor, or publisher) of plugin is being written. Please see other plugin types for more details. | ||
|
||
|
||
|
||
## Starting a plugin | ||
|
||
After implementing a type that satisfies one of {collector, processor, publisher} interfaces, all that is left to do is a call the appropriate plugin.StartX() with your plugin specific options. That could be as simple as: | ||
|
||
```go | ||
plugin.StartCollector(rand.RandCollector{}, pluginName, pluginVersion) | ||
``` | ||
|
||
### Meta options | ||
|
||
The available options are defined in [plugin/meta.go](https://github.com/intelsdi-x/snap-plugin-lib-go/tree/master/v/1/plugin/meta.go). You can use some or none of the options. The options with definitions/explanations are below: | ||
|
||
```go | ||
// ConcurrencyCount is the max number of concurrent calls the plugin | ||
// should take. For example: | ||
// If there are 5 tasks using the plugin and its concurrency count is 2, | ||
// snapd will keep 3 plugin instances running. | ||
// ConcurrencyCount overwrites the default (5) for a Meta's ConcurrencyCount. | ||
func ConcurrencyCount(cc int) MetaOpt { | ||
} | ||
|
||
// Exclusive == true results in a single instance of the plugin running | ||
// regardless of the number of tasks using the plugin. | ||
// Exclusive overwrites the default (false) for a Meta's Exclusive key. | ||
func Exclusive(e bool) MetaOpt { | ||
} | ||
|
||
// Unsecure results in unencrypted communication with this plugin. | ||
// Unsecure overwrites the default (false) for a Meta's Unsecure key. | ||
func Unsecure(e bool) MetaOpt { | ||
} | ||
|
||
// RoutingStrategy will override the routing strategy this plugin requires. | ||
// The default routing strategy is Least Recently Used. | ||
// RoutingStrategy overwrites the default (LRU) for a Meta's RoutingStrategy. | ||
func RoutingStrategy(r router) MetaOpt { | ||
} | ||
|
||
// CacheTTL will override the default cache TTL for the this plugin. snapd | ||
// caches metrics on the daemon side for a default of 500ms. | ||
// CacheTTL overwrites the default (500ms) for a Meta's CacheTTL. | ||
func CacheTTL(t time.Duration) MetaOpt { | ||
} | ||
``` | ||
|
||
An example of what using all of them would look like: | ||
|
||
```go | ||
plugin.StartCollector(mypackage.Mytype{}, | ||
pluginName, | ||
pluginVersion, | ||
plugin.ConcurrencyCount(a), | ||
plugin.Exclusive(b), | ||
plugin.Unsecure(c), | ||
plugin.RoutingStrategy(d), | ||
plugin.CacheTTL(e)) | ||
``` | ||
|
||
## Testing | ||
You should specifiy the test size (either small, medium, or large) with the specified build tag `// +build test-size`. The default testing will run all size tests if the build tag is not specified. | ||
|
||
|
||
For example if you want to run only small tests: | ||
``` | ||
// +build small | ||
// you must include at least one line between the build tag and the package name. | ||
package rand | ||
``` | ||
|
||
For example if you want to run small and medium tests: | ||
``` | ||
// +build small medium | ||
package rand | ||
``` |