func (so *StackdriverCustomMetricOutput) Run(or OutputRunner, h PluginHelper) (err error) {
	inChan := or.InChan()
	ticker := or.Ticker()

	var (
		pack      *PipelinePack
		values    = make(map[string]string)
		tmpMetric sd.Metric
		ok        = true
	)

	so.or = or

	for ok {
		select {
		case pack, ok = <-inChan:
			if !ok {
				break
			}

			for _, field := range pack.Message.Fields {
				if field.GetValueType() == message.Field_STRING && len(field.ValueString) > 0 {
					values[field.GetName()] = field.ValueString[0]
				}
				if field.GetValueType() == message.Field_INTEGER {
					values[field.GetName()] = strconv.FormatInt(field.ValueInteger[0], 10)
				}
				if field.GetValueType() == message.Field_DOUBLE {
					values[field.GetName()] = strconv.FormatFloat(field.ValueDouble[0], 'f', -1, 64)
				}
			}

			for _, met := range so.metrics {
				tmpMetric.Name = InterpolateString(met.Name, values)
				if met.InstanceId != "" {
					tmpMetric.InstanceId = InterpolateString(met.InstanceId, values)
				}
				tmpMetric.Value = InterpolateString(met.Value, values)
				if strings.Contains(tmpMetric.Value.(string), ".") {
					val, _ := strconv.ParseFloat(tmpMetric.Value.(string), 64)
					so.gwm.CustomMetric(tmpMetric.Name, tmpMetric.InstanceId, FormatUnixNano(pack.Message.GetTimestamp()), val)
				} else {
					val, _ := strconv.ParseInt(tmpMetric.Value.(string), 10, 64)
					so.gwm.CustomMetric(tmpMetric.Name, tmpMetric.InstanceId, FormatUnixNano(pack.Message.GetTimestamp()), val)
				}
			}
			pack.Recycle()
		case <-ticker:
			client := sd.NewStackdriverClient(so.conf.ApiKey)
			err = client.Send(so.gwm)
			if err != nil {
				so.or.LogMessage(fmt.Sprintf("[StackdriverCustomMetricOutput] API submission fail: %s\n", err))
			}
			so.gwm = sd.NewGatewayMessage()
		}
	}

	return
}
func (so *StackdriverCustomMetricOutput) Init(config interface{}) (err error) {
	so.conf = config.(*StackdriverCustomMetricOutputConfig)

	// Ensure Stackdriver API key value is set from TOML configuration.
	if so.conf.ApiKey == "" {
		return fmt.Errorf("api_key must contain a Stackdriver API key.")
	}

	// Populate StackdriverCustomMetricOutput config with values from TOML configuration.
	so.metrics = so.conf.Metric

	so.gwm = sd.NewGatewayMessage()

	return
}