Skip to content
This repository has been archived by the owner on Nov 8, 2022. It is now read-only.

Commit

Permalink
Separated Go RPC code from other code:
Browse files Browse the repository at this point in the history
- Identified code used by GoRPC plugins
- Created files with deprecated flags to hold code that is used by GoRPC plugins
- Added deprecation warning when loading legacy plugin
  • Loading branch information
kjlyon committed Oct 20, 2016
1 parent e72f01e commit 0a30d04
Show file tree
Hide file tree
Showing 16 changed files with 1,018 additions and 848 deletions.
5 changes: 5 additions & 0 deletions control/available_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ func newAvailablePlugin(resp plugin.Response, emitter gomit.Emitter, ep executab
}
ap.client = c
case plugin.NativeRPC:
log.WithFields(log.Fields{
"_module": "control-aplugin",
"_block": "newAvailablePlugin",
"plugin_name": ap.name,
}).Warning("This plugin is using a deprecated RPC protocol. Find more information here: https://github.com/intelsdi-x/snap/issues/1289 ")
c, e := client.NewCollectorNativeClient(resp.ListenAddress, DefaultClientTimeout, resp.PublicKey, !resp.Meta.Unsecure)
if e != nil {
return nil, errors.New("error while creating client connection: " + e.Error())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/* ** DEPRECATED **
For more information, see our deprecation notice
on Github: https://github.com/intelsdi-x/snap/issues/1289
*/

/*
http://www.apache.org/licenses/LICENSE-2.0.txt
Expand Down
58 changes: 0 additions & 58 deletions control/plugin/collector_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,6 @@ limitations under the License.

package plugin

import (
"errors"
"fmt"

"github.com/intelsdi-x/snap/core/cdata"
)

// Arguments passed to CollectMetrics() for a Collector implementation
type CollectMetricsArgs struct {
MetricTypes []MetricType
Expand All @@ -45,54 +38,3 @@ type GetMetricTypesArgs struct {
type GetMetricTypesReply struct {
MetricTypes []MetricType
}

type collectorPluginProxy struct {
Plugin CollectorPlugin
Session Session
}

func (c *collectorPluginProxy) GetMetricTypes(args []byte, reply *[]byte) error {
defer catchPluginPanic(c.Session.Logger())

c.Session.Logger().Debugln("GetMetricTypes called")
// Reset heartbeat
c.Session.ResetHeartbeat()

dargs := &GetMetricTypesArgs{PluginConfig: ConfigType{ConfigDataNode: cdata.NewNode()}}
c.Session.Decode(args, dargs)

mts, err := c.Plugin.GetMetricTypes(dargs.PluginConfig)
if err != nil {
return errors.New(fmt.Sprintf("GetMetricTypes call error : %s", err.Error()))
}

r := GetMetricTypesReply{MetricTypes: mts}
*reply, err = c.Session.Encode(r)
if err != nil {
return err
}

return nil
}

func (c *collectorPluginProxy) CollectMetrics(args []byte, reply *[]byte) error {
defer catchPluginPanic(c.Session.Logger())
c.Session.Logger().Debugln("CollectMetrics called")
// Reset heartbeat
c.Session.ResetHeartbeat()

dargs := &CollectMetricsArgs{}
c.Session.Decode(args, dargs)

ms, err := c.Plugin.CollectMetrics(dargs.MetricTypes)
if err != nil {
return errors.New(fmt.Sprintf("CollectMetrics call error : %s", err.Error()))
}

r := CollectMetricsReply{PluginMetrics: ms}
*reply, err = c.Session.Encode(r)
if err != nil {
return err
}
return nil
}
83 changes: 83 additions & 0 deletions control/plugin/collector_proxy_deprecated.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/* ** DEPRECATED **
For more information, see our deprecation notice
on Github: https://github.com/intelsdi-x/snap/issues/1289
*/

/*
http://www.apache.org/licenses/LICENSE-2.0.txt
Copyright 2015 Intel Corporation
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 plugin

import (
"errors"
"fmt"

"github.com/intelsdi-x/snap/core/cdata"
)

type collectorPluginProxy struct {
Plugin CollectorPlugin
Session Session
}

func (c *collectorPluginProxy) GetMetricTypes(args []byte, reply *[]byte) error {
defer catchPluginPanic(c.Session.Logger())

c.Session.Logger().Debugln("GetMetricTypes called")
// Reset heartbeat
c.Session.ResetHeartbeat()

dargs := &GetMetricTypesArgs{PluginConfig: ConfigType{ConfigDataNode: cdata.NewNode()}}
c.Session.Decode(args, dargs)

mts, err := c.Plugin.GetMetricTypes(dargs.PluginConfig)
if err != nil {
return errors.New(fmt.Sprintf("GetMetricTypes call error : %s", err.Error()))
}

r := GetMetricTypesReply{MetricTypes: mts}
*reply, err = c.Session.Encode(r)
if err != nil {
return err
}

return nil
}

func (c *collectorPluginProxy) CollectMetrics(args []byte, reply *[]byte) error {
defer catchPluginPanic(c.Session.Logger())
c.Session.Logger().Debugln("CollectMetrics called")
// Reset heartbeat
c.Session.ResetHeartbeat()

dargs := &CollectMetricsArgs{}
c.Session.Decode(args, dargs)

ms, err := c.Plugin.CollectMetrics(dargs.MetricTypes)
if err != nil {
return errors.New(fmt.Sprintf("CollectMetrics call error : %s", err.Error()))
}

r := CollectMetricsReply{PluginMetrics: ms}
*reply, err = c.Session.Encode(r)
if err != nil {
return err
}
return nil
}
140 changes: 0 additions & 140 deletions control/plugin/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,8 @@ import (
"bytes"
"encoding/gob"
"encoding/json"
"errors"
"fmt"
"time"

log "github.com/Sirupsen/logrus"

"github.com/intelsdi-x/snap/core"
"github.com/intelsdi-x/snap/core/cdata"
)
Expand All @@ -42,8 +38,6 @@ const (
SnapGOBContentType = "snap.gob"
// SnapJSON snap metrics serialized into json
SnapJSONContentType = "snap.json"
// SnapProtoBuff snap metrics serialized into protocol buffers
// SnapProtoBuff = "snap.pb" // TO BE IMPLEMENTED
)

type ConfigType struct {
Expand Down Expand Up @@ -81,12 +75,6 @@ func (p *ConfigType) GobDecode(data []byte) error {
return nil
}

func NewPluginConfigType() ConfigType {
return ConfigType{
ConfigDataNode: cdata.NewNode(),
}
}

// Represents a metric type. Only used within plugins and across plugin calls.
// Converted to core.MetricType before being used within modules.
type MetricType struct {
Expand Down Expand Up @@ -122,18 +110,6 @@ type MetricType struct {
Timestamp_ time.Time `json:"timestamp"`
}

// NewMetricType returns a Constructor
func NewMetricType(namespace core.Namespace, timestamp time.Time, tags map[string]string, unit string, data interface{}) *MetricType {
return &MetricType{
Namespace_: namespace,
Tags_: tags,
Data_: data,
Timestamp_: timestamp,
LastAdvertisedTime_: timestamp,
Unit_: unit,
}
}

// Returns the namespace.
func (p MetricType) Namespace() core.Namespace {
return p.Namespace_
Expand Down Expand Up @@ -182,119 +158,3 @@ func (p MetricType) Unit() string {
func (p *MetricType) AddData(data interface{}) {
p.Data_ = data
}

// MarshalMetricTypes returns a []byte containing a serialized version of []MetricType using the content type provided.
func MarshalMetricTypes(contentType string, metrics []MetricType) ([]byte, string, error) {
// If we have an empty slice we return an error
if len(metrics) == 0 {
es := fmt.Sprintf("attempt to marshall empty slice of metrics: %s", contentType)
log.WithFields(log.Fields{
"_module": "control-plugin",
"block": "marshal-content-type",
"error": es,
}).Error("error while marshalling")
return nil, "", errors.New(es)
}
// Switch on content type
switch contentType {
case SnapAllContentType, SnapGOBContentType:
// NOTE: A snap All wildcard will result in GOB
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
err := enc.Encode(metrics)
if err != nil {
log.WithFields(log.Fields{
"_module": "control-plugin",
"block": "marshal-content-type",
"error": err.Error(),
}).Error("error while marshalling")
return nil, "", err
}
// contentType := SnapGOBContentType
return buf.Bytes(), SnapGOBContentType, nil
case SnapJSONContentType:
// Serialize into JSON
b, err := json.Marshal(metrics)
if err != nil {
log.WithFields(log.Fields{
"_module": "control-plugin",
"block": "marshal-content-type",
"error": err.Error(),
}).Error("error while marshalling")
return nil, "", err
}
return b, SnapJSONContentType, nil
default:
// We don't recognize this content type. Log and return error.
es := fmt.Sprintf("invalid snap content type: %s", contentType)
log.WithFields(log.Fields{
"_module": "control-plugin",
"block": "marshal-content-type",
"error": es,
}).Error("error while marshalling")
return nil, "", errors.New(es)
}
}

// UnmarshallMetricTypes takes a content type and []byte payload and returns a []MetricType
func UnmarshallMetricTypes(contentType string, payload []byte) ([]MetricType, error) {
switch contentType {
case SnapGOBContentType:
var metrics []MetricType
r := bytes.NewBuffer(payload)
err := gob.NewDecoder(r).Decode(&metrics)
if err != nil {
log.WithFields(log.Fields{
"_module": "control-plugin",
"block": "unmarshal-content-type",
"error": err.Error(),
}).Error("error while unmarshalling")
return nil, err
}
return metrics, nil
case SnapJSONContentType:
var metrics []MetricType
err := json.Unmarshal(payload, &metrics)
if err != nil {
log.WithFields(log.Fields{
"_module": "control-plugin",
"block": "unmarshal-content-type",
"error": err.Error(),
}).Error("error while unmarshalling")
return nil, err
}
return metrics, nil
default:
// We don't recognize this content type as one we can unmarshal. Log and return error.
es := fmt.Sprintf("invalid snap content type for unmarshalling: %s", contentType)
log.WithFields(log.Fields{
"_module": "control-plugin",
"block": "unmarshal-content-type",
"error": es,
}).Error("error while unmarshalling")
return nil, errors.New(es)
}
}

// SwapMetricContentType swaps a payload with one content type to another one.
func SwapMetricContentType(contentType, requestedContentType string, payload []byte) ([]byte, string, error) {
metrics, err1 := UnmarshallMetricTypes(contentType, payload)
if err1 != nil {
log.WithFields(log.Fields{
"_module": "control-plugin",
"block": "swap-content-type",
"error": err1.Error(),
}).Error("error while swaping")
return nil, "", err1
}
newPayload, newContentType, err2 := MarshalMetricTypes(requestedContentType, metrics)
if err2 != nil {
log.WithFields(log.Fields{
"_module": "control-plugin",
"block": "swap-content-type",
"error": err2.Error(),
}).Error("error while swaping")
return nil, "", err2
}
return newPayload, newContentType, nil
}
Loading

0 comments on commit 0a30d04

Please sign in to comment.