【问题标题】:How to interact with Telegraf using Go external plugin?如何使用 Go 外部插件与 Telegraf 交互?
【发布时间】:2020-04-28 00:26:57
【问题描述】:

我有一个虚拟 GO 插件,使用它,我想将数据发送到电报。但是,我无法找到任何方法将数据从插件发送到电报。 这个外部 Go 插件如下所示

package main

import (
    "fmt"
    "time"
)

type greeting string
type n int

func (g greeting) Greet() {
    for i := 0; i < 10; i++ {
        timer1 := time.NewTimer(2 * time.Second)
        <-timer1.C
        fmt.Println("value ", i)

        sum := 0
        sum += i * 100
        fmt.Println(sum)
    }

}

// exported
var Greeter greeting

主文件看起来像


import (
    "fmt"
    "os"
    "plugin"
)

type Greeter interface {
    Greet()
}

func main() {

    var mod string

    mod = "./eng/eng.so"

    // load module
    // 1. open the so file to load the symbols
    plug, err := plugin.Open(mod)
    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }

    // 2. look up a symbol (an exported function or variable)
    // in this case, variable Greeter
    symGreeter, err := plug.Lookup("Greeter")
    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }

    // 3. Assert that loaded symbol is of a desired type
    // in this case interface type Greeter (defined above)
    var greeter Greeter
    greeter, ok := symGreeter.(Greeter)
    if !ok {
        fmt.Println("unexpected type from module symbol")
        os.Exit(1)
    }

    // 4. use the module
    greeter.Greet()


}

谁能帮我找到一种方法或方向,让 GO 插件和 Telegraf 之间的交互起作用。任何提醒表示赞赏。

【问题讨论】:

    标签: go plugins grafana influxdb telegraf


    【解决方案1】:

    目前,Telegraf 不支持使用外部插件。 已针对此请求的功能制作并关闭了 issue

    如果您打算用 Golang 编写代码,我建议您创建一个自定义 Telegraf 插件。 Telegraf 插件不使用外部插件包,而是使用 Telegraf 本身构建的。您的插件必须满足基于您要创建的插件类型的特定接口。根据您的问题,我假设您希望创建一个输入插件,Telegraf 有examples 用于在其源代码中创建每种类型的插件。

    【讨论】:

      猜你喜欢
      • 2014-03-13
      • 1970-01-01
      • 1970-01-01
      • 2012-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-08
      • 1970-01-01
      相关资源
      最近更新 更多