Gin Application Add newrelic monitoring

Source: Internet
Author: User
Tags key string
This is a creation in Article, where the information may have evolved or changed.

The newrelic is a very useful monitoring platform. Request analysis, data transactions, machine health, error alarm and other functions, and the strength of the segmentation is fine enough, in addition to expensive, others are very good. In fact, good service is worth the price.

Newrelic to the major language framework support is very perfect, the support of Golang is not bad. However, the official website provides examples only for net/http. For the gin currently in use, still need a hack. Fortunately gin is elegant enough. Here's a look at registering Newrelic with gin development.

Install Newrelic's official Golang package first:

go get -u -v github.com/newrelic/go-agent

Then write a middleware, for the request before the hook, that is, register to Newrelic, the code is relatively simple, as follows:

  Package Mainimport ("Net/http" "Github.com/gin-gonic/gin" "Github.com/newrelic/go-agent") func Newreli Cmiddleware (AppName string, key string) gin. Handlerfunc {if appName = = "" | | Key = = "" {return func (c *gin. Context) {}} config: = Newrelic. Newconfig (AppName, key) app, err: = Newrelic. Newapplication (config) if err! = Nil {panic (err)} return func (C *gin. Context) {txn: = App. StartTransaction (C.request.url.path, C.writer, c.request) defer txn. End () C.next ()}}func main () {router: = gin. Default () router. Use (Newrelicmiddleware ("App-name", "App-key")) router. GET ("/", Func (c *gin. Context) {c.string (http. Statusok, "It Works")}) router. Run ()}  
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.