0

我正在尝试使用 sarama 获取 kafka 生产者/消费者指标。但我找不到任何关于如何做同样事情的例子。有人可以提供一个示例实现示例吗?

我正在使用以下代码来获取经纪人的指标。但是获取生产者/消费者指标的配置应该是什么。我假设它不会一样。如我错了请纠正我

    saramaConfig := sarama.NewConfig()
    saramaConfig.Version = <BrokerVersion>
    client, err := sarama.NewClient(<brokerAddresses>, saramaConfig)
    if err != nil {
        log.Println("Unable to create sarama client")
        panic(err)
    }
    config := client.Config()

    MeanIncomingByteRate := metrics.GetOrRegisterMeter(getMetricNameForTopic("incoming-byte-rate", topic), config.MetricRegistry).RateMean(),
4

1 回答 1

0

我从来没有合作过。但我会尽力回答。

您应该将go-metrics注册表注册到配置中。

这是如何创建和使用注册表的示例(https://pkg.go.dev/gopkg.in/Shopify/sarama.v2#example-Config-Metrics):

// Our application registry
appMetricRegistry := metrics.NewRegistry()
appGauge := metrics.GetOrRegisterGauge("m1", appMetricRegistry)
appGauge.Update(1)

config := NewConfig()
// Use a prefix registry instead of the default local one
config.MetricRegistry = metrics.NewPrefixedChildRegistry(appMetricRegistry, "sarama.")

// Simulate a metric created by sarama without starting a broker
saramaGauge := metrics.GetOrRegisterGauge("m2", config.MetricRegistry)
saramaGauge.Update(2)

metrics.WriteOnce(appMetricRegistry, os.Stdout)

所以你应该创建注册表并将其设置为配置

appMetricRegistry := metrics.NewRegistry()
appGauge := metrics.GetOrRegisterGauge("m1", appMetricRegistry)
appGauge.Update(1)

    saramaConfig := sarama.NewConfig()
    saramaConfig.Version = <BrokerVersion>

    // set it here
    saramaConfig.MetricRegistry = metrics.NewPrefixedChildRegistry(appMetricRegistry, "sarama.")


    client, err := sarama.NewClient(<brokerAddresses>, saramaConfig)
    if err != nil {
        log.Println("Unable to create sarama client")
        panic(err)
    }
    config := client.Config()

    MeanIncomingByteRate := metrics.GetOrRegisterMeter(getMetricNameForTopic("incoming-byte-rate", topic), config.MetricRegistry).RateMean(),


于 2021-06-28T14:49:11.300 回答