Skip to content

Instantly share code, notes, and snippets.

@jcooklin
Last active August 4, 2016 01:17
Show Gist options
  • Save jcooklin/038534310ca38bbcc727ee8cb29021f1 to your computer and use it in GitHub Desktop.
Save jcooklin/038534310ca38bbcc727ee8cb29021f1 to your computer and use it in GitHub Desktop.
package nginxdbg
import (
"bufio"
"fmt"
"os"
"strings"
"time"
"github.com/intelsdi-x/snap/control/plugin"
"github.com/intelsdi-x/snap/control/plugin/cpolicy"
"github.com/intelsdi-x/snap/core"
)
const (
// Name of plugin
Name = "nginxdbg"
// Version of plugin
Version = 1
// Type of plugin
Type = plugin.CollectorPluginType
)
type Nginxdbg struct{}
func (n *Nginxdbg) CollectMetrics(inmts []plugin.MetricType) (mts []plugin.MetricType, err error) {
return nil, nil
}
func (n *Nginxdbg) GetMetricTypes(cfg plugin.ConfigType) ([]plugin.MetricType, error) {
mts := []plugin.MetricType{}
for i := 0; i < 900; i++ {
tmp := plugin.MetricType{}
ss := strings.Split("/staples/nginx/stream/upstreams/dns_udp_backends/peers/host_id_745cd_eurxserver_com__12kc135_americanid_int/health_checks/checks", "/")
tmp.Namespace_ = core.NewNamespace(ss...)
mts = append(mts, tmp)
// fmt.Println("Obj =", i, " mts =", mts) // I don't think you really want to print mts here.
// if you do it will eventually exceed the scanners buffer that's reading stdout and will break
// the plugin.
fmt.Printf("i: %v metric: %+v \n", i, tmp)
}
f := bufio.NewWriter(os.Stdout)
defer f.Flush()
// fmt.Println("len =", len(mts), " Capcity =", cap(mts))
fmt.Fprintf(f, "len=%v cap=%v\n", len(mts), cap(mts))
//The Below println is not print the data when run along with the agent. But works with stand alone
// nb, err := fmt.Fprintf(f, "GetMetricTypes = %+v \n", mts)
// I'm leaving the above line commented out since it won't work. Snapd uses standard out to communicate
// information about the plugin with control. After this handshake we then capture stdout (and stderr)
// and in the v0.14 which you downloaded wrote <plugin>.stdout and <plugin>.stderr files using a
// routine that scans the stdout. The problem that this line runs into is the fact that we are scaning
// using new lines as the token and the line above is huge and exceeds the buffer.
// From the bufio.Scanner docs: Scanning stops unrecoverably at EOF, the first I/O error, or a token too
// large to fit in the buffer. When a scan stops, the reader may have advanced arbitrarily far past the
// last token.
// At this point the best advice I can give is to not print "large" items to stdout and instead open a
// file that the plugin can write to. In the meantime we'll open an issue and see if there isn't a
// clever solution we can put into place or provide more effective guidance.
// P.S. Note all of the newlines added to the print statements. These are critical since we are scanning
// stdout looking for them before printing.
return mts, nil
}
func (n *Nginxdbg) GetConfigPolicy() (*cpolicy.ConfigPolicy, error) {
cfg := cpolicy.New()
policy := cpolicy.NewPolicyNode()
cfg.Add([]string{"staples", "nginxdbg"}, policy)
return cfg, nil
}
func Meta() *plugin.PluginMeta {
return plugin.NewPluginMeta(
Name,
Version,
Type,
[]string{plugin.SnapGOBContentType},
[]string{plugin.SnapGOBContentType},
plugin.Unsecure(true),
plugin.RoutingStrategy(plugin.DefaultRouting),
plugin.CacheTTL(1100*time.Millisecond),
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment