登录
首页 >  Golang >  Go问答

与 Telegraf 互动:使用 Go 外部插件的方法

来源:stackoverflow

时间:2024-02-23 09:15:24 287浏览 收藏

本篇文章向大家介绍《与 Telegraf 互动:使用 Go 外部插件的方法》,主要包括,具有一定的参考价值,需要的朋友可以参考一下。

问题内容

我有一个虚拟的 go 插件,使用它,我想将数据发送到 telegraf。但是,我无法找到任何方法将数据从插件发送到 telegraf。 这个外部 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 之间进行交互的方法或方向吗?如有任何提示,我们将不胜感激。


解决方案


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

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

到这里,我们也就讲完了《与 Telegraf 互动:使用 Go 外部插件的方法》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于的知识点!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>