Beego 和 Newrelic 集成问题
今日不肯埋头,明日何以抬头!每日一句努力自己的话哈哈~哈喽,今天我将给大家带来一篇《Beego 和 Newrelic 集成问题》,主要内容是讲解等等,感兴趣的朋友可以收藏或者有更好的建议在评论提出,我都会认真看的!大家一起进步,一起学习!
我正在使用 beego (https://beego.me) 在 go 中创建一个 web 应用程序。
我需要捕获 newrelic 中的应用程序监控和计量指标,并能够查看 newrelic 中的所有事务。
我已遵循此文档,并使用 beego gorelic 在我的代码中初始化 newrelic 代理。
这是我的 router.go 类 -
import (
"github.com/astaxie/beego"
"github.com/yvasiyarov/beego_gorelic"
)
func init() {
beego_gorelic.initnewrelicagent()
beego.router("path", &controller{}, "functionname") // code broken intentionally
}
在我的 app.conf 中,我提供了 newrelic 许可证密钥和应用程序名称,如下所示:
newreliclicense = "key" newrelicappname = "app name"
当我在 dev 模式下运行应用程序时,我能够看到正在打印的指标,如下所示:
send data:{
"agent": {
"host": "localhost",
"version": "0.0.6",
"pid": 13942
},
"components": [
{
"name": "name",
"guid": "com.github.yvasiyarov.gorelic",
"duration": 60,
"metrics": {
"component/runtime/gc/gctime/max[nanoseconds]": 0,
"component/runtime/gc/gctime/mean[nanoseconds]": 0,
"component/runtime/gc/gctime/min[nanoseconds]": 0,
"component/runtime/gc/gctime/percentile95[nanoseconds]": 0,
"component/runtime/gc/numberofgccalls[calls]": 0,
"component/runtime/gc/pausetotaltime[nanoseconds]": 0,
"component/runtime/general/nocgocalls[calls]": 1,
"component/runtime/general/nogoroutines[goroutines]": 6,
"component/runtime/memory/inuse/heap[bytes]": 538368,
"component/runtime/memory/inuse/mcacheinuse[bytes]": 13888,
"component/runtime/memory/inuse/mspaninuse[bytes]": 13192,
"component/runtime/memory/inuse/stack[bytes]": 360448,
"component/runtime/memory/inuse/total[bytes]": 538368,
"component/runtime/memory/operations/nofrees[frees]": 128,
"component/runtime/memory/operations/nomallocs[mallocs]": 2903,
"component/runtime/memory/operations/nopointerlookups[lookups]": 0,
"component/runtime/memory/sysmem/buckhash[bytes]": 3078,
"component/runtime/memory/sysmem/heap[bytes]": 66748416,
"component/runtime/memory/sysmem/mcache[bytes]": 16384,
"component/runtime/memory/sysmem/mspan[bytes]": 16384,
"component/runtime/memory/sysmem/stack[bytes]": 360448,
"component/runtime/memory/sysmem/total[bytes]": 69928960,
"component/http/responsetime/max[ms]": 0,
"component/http/responsetime/mean[ms]": 0,
"component/http/responsetime/min[ms]": 0,
"component/http/responsetime/percentile75[ms]": 0,
"component/http/responsetime/percentile90[ms]": 0,
"component/http/responsetime/percentile95[ms]": 0,
"component/http/throughput/1minute[rps]": 0,
"component/http/throughput/ratemean[rps]": 0
}
}
],
"url": "https://platform-api.newrelic.com/platform/v1/metrics"
}
这告诉我新的遗迹代理已启动并正在捕获数据。
除此之外,我没有编写任何其他代码来将 newrelic 集成到我的代码中。
这些指标没有通过这些设置和设置报告给 newrelic,而且我无法找到更好的文档来解决这个问题。
我错过了什么?
此外,还有其他方法可以将 newrelic 集成到 beego 应用程序中吗?
附注我没有使用beego filters作为预处理器,而是在控制器中使用prepare方法来预处理请求。然而,这个库似乎在 beego 框架中添加了 2 个过滤器,如下所示:
beego.insertfilter("*", beego.beforerouter, initnewrelictimer, false)
beego.insertfilter("*", beego.finishrouter, reportmetricstonewrelic, false)
其他日志:
2020/01/09 20:13:29 Init GC metrics collection. Poll interval 10 seconds.
2020/01/09 20:13:29 Init memory allocator metrics collection. Poll interval 60 seconds.
2020/01/09 20:13:29 Init HTTP metrics collection.
2020/01/09 20:13:29 [I] [gorelic.go:71] NewRelic agent started
2020/01/09 20:13:29 Can not get metrica: Runtime/System/Threads, got error:this metrica was not implemented yet for darwin
2020/01/09 20:13:29 Can not get metrica: Runtime/System/FDSize, got error:this metrica was not implemented yet for darwin
2020/01/09 20:13:29 Can not get metrica: Runtime/System/Memory/VmPeakSize, got error:this metrica was not implemented yet for darwin
2020/01/09 20:13:29 Can not get metrica: Runtime/System/Memory/VmCurrent, got error:this metrica was not implemented yet for darwin
2020/01/09 20:13:29 Can not get metrica: Runtime/System/Memory/RssPeak, got error:this metrica was not implemented yet for darwin
2020/01/09 20:13:29 Can not get metrica: Runtime/System/Memory/RssCurrent, got error:this metrica was not implemented yet for darwin
2020/01/09 20:13:29 Send data:{
"agent": {
"host": "localhost",
"version": "0.0.6",
"pid": 14931
},
"components": [
{
"name": "App Name",
"guid": "com.github.yvasiyarov.GoRelic",
"duration": 60,
"metrics": {
"Component/Runtime/GC/GCTime/Max[nanoseconds]": 0,
"Component/Runtime/GC/GCTime/Mean[nanoseconds]": 0,
"Component/Runtime/GC/GCTime/Min[nanoseconds]": 0,
"Component/Runtime/GC/GCTime/Percentile95[nanoseconds]": 0,
"Component/Runtime/GC/NumberOfGCCalls[calls]": 0,
"Component/Runtime/GC/PauseTotalTime[nanoseconds]": 0,
"Component/Runtime/General/NOCgoCalls[calls]": 1,
"Component/Runtime/General/NOGoroutines[goroutines]": 8,
"Component/Runtime/Memory/InUse/Heap[bytes]": 591616,
"Component/Runtime/Memory/InUse/MCacheInuse[bytes]": 13888,
"Component/Runtime/Memory/InUse/MSpanInuse[bytes]": 16592,
"Component/Runtime/Memory/InUse/Stack[bytes]": 393216,
"Component/Runtime/Memory/InUse/Total[bytes]": 591616,
"Component/Runtime/Memory/Operations/NoFrees[frees]": 132,
"Component/Runtime/Memory/Operations/NoMallocs[mallocs]": 2988,
"Component/Runtime/Memory/Operations/NoPointerLookups[lookups]": 0,
"Component/Runtime/Memory/SysMem/BuckHash[bytes]": 3078,
"Component/Runtime/Memory/SysMem/Heap[bytes]": 66715648,
"Component/Runtime/Memory/SysMem/MCache[bytes]": 16384,
"Component/Runtime/Memory/SysMem/MSpan[bytes]": 32768,
"Component/Runtime/Memory/SysMem/Stack[bytes]": 393216,
"Component/Runtime/Memory/SysMem/Total[bytes]": 69928960,
"Component/http/responseTime/max[ms]": 0,
"Component/http/responseTime/mean[ms]": 0,
"Component/http/responseTime/min[ms]": 0,
"Component/http/responseTime/percentile75[ms]": 0,
"Component/http/responseTime/percentile90[ms]": 0,
"Component/http/responseTime/percentile95[ms]": 0,
"Component/http/throughput/1minute[rps]": 0,
"Component/http/throughput/rateMean[rps]": 0
}
}
],
"URL": "https://platform-api.newrelic.com/platform/v1/metrics"
}
这可能是问题所在吗?我该如何解决这个问题?
解决方案
我正在回答我自己的问题,因为我相信我已经找到了一种方法,并且它也可能对其他人有帮助 - 显然这只是更努力地寻找现有插件的问题。我发现这个插件:https://github.com/sergeylanzman/newrelic_beego 正是我所需要的。
如果我们观察代码,作者创建了 3 个方法 -
- 开始交易
- 名称交易
- 结束交易
并将它们作为过滤器插入 beego 环境中。在底层,作者使用了官方的 newrelic go agent (https://github.com/newrelic/go-agent),并使用 transactions 来报告 apm 数据。
这里是来自插件的示例代码,它注册了 3 个过滤器 -
beego.InsertFilter("*", beego.BeforeRouter, StartTransaction, false)
beego.InsertFilter("*", beego.AfterExec, NameTransaction, false)
beego.InsertFilter("*", beego.FinishRouter, EndTransaction, false)
beego.Info("NewRelic agent start")
它使用 beego 的 context 结构来跟踪跨函数的交易数据(如 context 文档中所述)
随着我们的进展,我将继续对此进行更多探索,并添加对外部调用、数据库调用和分布式跟踪的跟踪,但这解决了我的问题。
附注我已经停止使用之前使用的插件,因为这个包还会向 newrelic 报告 go 运行时统计信息,从而使其他插件过时(个人意见:))
到这里,我们也就讲完了《Beego 和 Newrelic 集成问题》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于的知识点!
《火星救援》同作者科幻小说《挽救计划》改编电影 2026 年上映,“高司令”主演
- 上一篇
- 《火星救援》同作者科幻小说《挽救计划》改编电影 2026 年上映,“高司令”主演
- 下一篇
- Java 函数的最新趋势和最佳实践有哪些?
-
- Golang · Go问答 | 1年前 |
- 在读取缓冲通道中的内容之前退出
- 139浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 戈兰岛的全球 GOPRIVATE 设置
- 204浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 如何将结构作为参数传递给 xml-rpc
- 325浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 如何用golang获得小数点以下两位长度?
- 478浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 如何通过 client-go 和 golang 检索 Kubernetes 指标
- 486浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 将多个“参数”映射到单个可变参数的习惯用法
- 439浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 将 HTTP 响应正文写入文件后出现 EOF 错误
- 357浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 结构中映射的匿名列表的“复合文字中缺少类型”
- 352浏览 收藏
-
- Golang · Go问答 | 1年前 |
- NATS Jetstream 的性能
- 101浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 如何将复杂的字符串输入转换为mapstring?
- 440浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 相当于GoLang中Java将Object作为方法参数传递
- 212浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 如何确保所有 goroutine 在没有 time.Sleep 的情况下终止?
- 143浏览 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 485次学习
-
- ChatExcel酷表
- ChatExcel酷表是由北京大学团队打造的Excel聊天机器人,用自然语言操控表格,简化数据处理,告别繁琐操作,提升工作效率!适用于学生、上班族及政府人员。
- 3163次使用
-
- Any绘本
- 探索Any绘本(anypicturebook.com/zh),一款开源免费的AI绘本创作工具,基于Google Gemini与Flux AI模型,让您轻松创作个性化绘本。适用于家庭、教育、创作等多种场景,零门槛,高自由度,技术透明,本地可控。
- 3375次使用
-
- 可赞AI
- 可赞AI,AI驱动的办公可视化智能工具,助您轻松实现文本与可视化元素高效转化。无论是智能文档生成、多格式文本解析,还是一键生成专业图表、脑图、知识卡片,可赞AI都能让信息处理更清晰高效。覆盖数据汇报、会议纪要、内容营销等全场景,大幅提升办公效率,降低专业门槛,是您提升工作效率的得力助手。
- 3403次使用
-
- 星月写作
- 星月写作是国内首款聚焦中文网络小说创作的AI辅助工具,解决网文作者从构思到变现的全流程痛点。AI扫榜、专属模板、全链路适配,助力新人快速上手,资深作者效率倍增。
- 4506次使用
-
- MagicLight
- MagicLight.ai是全球首款叙事驱动型AI动画视频创作平台,专注于解决从故事想法到完整动画的全流程痛点。它通过自研AI模型,保障角色、风格、场景高度一致性,让零动画经验者也能高效产出专业级叙事内容。广泛适用于独立创作者、动画工作室、教育机构及企业营销,助您轻松实现创意落地与商业化。
- 3784次使用
-
- GoLand调式动态执行代码
- 2023-01-13 502浏览
-
- 用Nginx反向代理部署go写的网站。
- 2023-01-17 502浏览
-
- Golang取得代码运行时间的问题
- 2023-02-24 501浏览
-
- 请问 go 代码如何实现在代码改动后不需要Ctrl+c,然后重新 go run *.go 文件?
- 2023-01-08 501浏览
-
- 如何从同一个 io.Reader 读取多次
- 2023-04-11 501浏览

