英文:
How to capture and send error with Go APM to Elastic Kibana?
问题
我了解到你对Elastic和Kibana(版本8.4)APM的基础知识有所了解,但是当你访问Kibana>Observability>APM>Services>MyGoApp>Errors时,似乎无法填充错误页面。它总是显示为空白,就像这样:
这是我的Golang代码,你有什么想法,我做错了什么?为什么错误不显示在我的Kibana APM错误页面上?
package main
import (
        "errors"
        "fmt"
        "net/http"
        "github.com/gorilla/mux"
        "github.com/joho/godotenv"
        "go.elastic.co/apm"
        "go.elastic.co/apm/module/apmhttp/v2"
)
func main() {
        godotenv.Load()
        router := mux.NewRouter()
        router.HandleFunc("/", homePage)
        router.HandleFunc("/error", errorPage)
        http.ListenAndServe(":8080", apmhttp.Wrap(router))
}
func homePage(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "Welcome to the HomePage!")
        fmt.Println("Endpoint Hit: homePage")
}
func errorPage(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "Sending Error!")
        apm.CaptureError(r.Context(), errors.New("test error")).Send()
}
作为实验,我尝试在Node.js中使用CaptureError()方法。实际上,它填充了错误。这是我的代码:
var apm = require('elastic-apm-node').start({
  // 允许的字符:a-z、A-Z、0-9、-、_和空格
  serviceName: 'app2',
  // 如果APM服务器需要秘密令牌,请使用
  secretToken: 'testtoken',
  serverUrl: 'http url',
  verifyServerCert: false,
  environment: 'production'
})
const express = require('express')
const app = express()
const port = 3030
app.get('/', (req, res) => {
  res.send('Hello World!')
})
app.get('/error', (req, res) => {
  res.send('Send Err!')
  const err = new Error('Trigger Error!')
  apm.setTransactionName('/error')
  apm.captureError(err)
})
app.listen(port, () => {
  console.log(`Example app listening on port ${port}`)
})
因此,似乎CaptureError()方法适用于Node.js包,但不适用于Golang包。
英文:
I got the basics of Elastic and Kibana (version 8.4) APM for a simple go app, but I can't seem to populate the errors page when I go to Kibana>Observability>APM>Services>MyGoApp>Errors. It always looks empty like this:
The Overview page properly shows things though like this:

This is my golang code, any idea what I'm doing wrong? Why don't errors show up in my Kibana APM Errors?
package main
import (
        "errors"
        "fmt"
        "net/http"
        "github.com/gorilla/mux"
        "github.com/joho/godotenv"
        "go.elastic.co/apm"
        "go.elastic.co/apm/module/apmhttp/v2"
)
func main() {
        godotenv.Load()
        router := mux.NewRouter()
        router.HandleFunc("/", homePage)
        router.HandleFunc("/error", errorPage)
        http.ListenAndServe(":8080", apmhttp.Wrap(router))
}
func homePage(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "Welcome to the HomePage!")
        fmt.Println("Endpoint Hit: homePage")
}
func errorPage(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "Sending Error!")
        apm.CaptureError(r.Context(), errors.New("test error")).Send()
}
As experiment, I tried using CaptureError() method in nodejs. And it actually populated the errors.  This was my code:
var apm = require('elastic-apm-node').start({
  // Allowed characters: a-z, A-Z, 0-9, -, _, and space
  serviceName: 'app2',
  // Use if APM Server requires a secret token
  secretToken: 'testtoken',
  serverUrl: 'http url',
  verifyServerCert: false,
  environment: 'production'
})
const express = require('express')
const app = express()
const port = 3030
app.get('/', (req, res) => {
  res.send('Hello World!')
})
app.get('/error', (req, res) => {
  res.send('Send Err!')
  const err = new Error('Trigger Error!')
  apm.setTransactionName('/error')
  apm.captureError(err)
})
app.listen(port, () => {
  console.log(`Example app listening on port ${port}`)
})
So it seems that CaptureError() works for nodejs package, but not for golang package.
答案1
得分: 1
我刚遇到了一个类似的问题,我不得不设置ELASTIC_APM_CAPTURE_BODY=all环境变量。否则,你作为第一个参数传递的上下文将为空,没有任何追踪信息。如果任一参数为nil,则不会返回错误。
英文:
I just had a similar problem, I had to set the ELASTIC_APM_CAPTURE_BODY=all environment variable.
Otherwise the context you pass as the first argument is empty of any trace,
If either argument is nil then the error will not be returned.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。



评论