penglongli / gin-metrics

gin-gonic/gin metrics for prometheus.
MIT License
248 stars 58 forks source link

geoip metrics / how to add ? #2

Closed ghost closed 3 years ago

ghost commented 3 years ago

Hi @penglongli ,

Hope you are all well !

I would like to add some custom metrics from the following middleware:

How should I do it with gin-metrics ?

Thanks for any insights or inputs on that question :-)

Cheers, Luc Michalski

penglongli commented 3 years ago

Hi, i need to understand your needs first.

Do you want to summary CityName/StateCode/CountryCode/ContinentCode by IP-Address? And exposed these data by custom-metrics ?

ghost commented 3 years ago

Context: I am building an acme cdn (content delivery network) with gin/gin-cache and your repo.

So I would like to upgrade the grafana dashboard to use this panel: https://grafana.com/grafana/plugins/grafana-worldmap-panel/ and some others panel for summarizing the activity per city/country/state.

What would be your advice ?

penglongli commented 3 years ago

Context: I am building an acme cdn (content delivery network) with gin/gin-cache and your repo.

So I would like to upgrade the grafana dashboard to use this panel: https://grafana.com/grafana/plugins/grafana-worldmap-panel/ and some others panel for summarizing the activity per city/country/state.

What would be your advice ?

Maybe this sample can meet your needs.

// 1. Define your custom-metric, and add it to global metrics.
gaugeMetric := &ginmetrics.Metric{
    Type:        ginmetrics.Counter,
    Name:        "summary_country",
    Description: "Summary country/city... by IP Address",
    Labels:      []string{"country", "city", "state"},
}
_ = ginmetrics.GetMonitor().AddMetric(gaugeMetric)

// 2. Set geo interceptor
r.Use(func(ctx *gin.Context) {
    geoResponse, ok := ctx.Get("GeoResponse")
    if ok {
        obj := new(geo.Response)
        if err := json.Unmarshal(geoResponse, obj); err != nil {
            // log err
            // ...
        } else {
            _ = ginmetrics.GetMonitor().GetMetric("summary_country").Inc([]string{
                obj.CountryCode,
                obj.CityName,
                obj.StateCode,
            })
        }
    }
    ctx.Next()
})

If the performance of this geo library is slow, you should consider optimize it.

ghost commented 3 years ago

awesome, I ll try it now :-)

take care

ghost commented 3 years ago

Sorry to re-open it again, but I am stucked to integrate it to grafana... ^^

Do you have an idea how to formulate the query in grafana for this panel ?

penglongli commented 3 years ago

Sorry to re-open it again, but I am stucked to integrate it to grafana... ^^

Do you have an idea how to formulate the query in grafana for this panel ?

According to the above, your http_server have metrics. And you can access it with http://localhost:$port/debug/metrics, this path will show you the summary_country data.

Then, you need to install a Prometheus, configure Prometheus to grab these data. After that, you can query the data on Prometheus

Grafana is a front dashboard for backend(MySQL/Prometheus/PGSQL...).

Do you have a Prometheus server now?

ghost commented 3 years ago

yes, I have setup/dockerized prometheus with grafana. I already imported your grafana dashboard and wanted to add the world map panel.

penglongli commented 3 years ago

yes, I have setup/dockerized prometheus with grafana. I already imported your grafana dashboard and wanted to add the world map panel.

Yeah, has any summary_country data been query by Prometheus?

ghost commented 3 years ago

yes, see the screenshot below:

Screenshot 2021-04-07 at 08 26 27
penglongli commented 3 years ago

yes, see the screenshot below:

Screenshot 2021-04-07 at 08 26 27

You can install the plugin for Grafana first: https://grafana.com/grafana/plugins/grafana-worldmap-panel/?tab=installation

Download the zip and install plugin.

After Worldmap Panel plugin installed, you should create a worldmap panel. And configure it with Prometheus Query

ghost commented 3 years ago

so the question is, what would like the query because I already installed the plugin :-)

penglongli commented 3 years ago

so the question is, what would like the query because I already installed the plugin :-)

Probably like this, have an explore~ :) image

ghost commented 3 years ago

I tried the following but no luck:

summary_country{job=~"$job", name=~"$city", key=~"$country", latitude=~"$latitude", longitude=~"$longitude"}
penglongli commented 3 years ago

summary_country

You can run it in Prometheus first, see whether the grammar is ok.

ghost commented 3 years ago

I added longitude, latitude to the metrics as it is required by the panel but it does not display the points as it also needs a metric...

penglongli commented 3 years ago

I'll do a test tonight and see how to configure a worldmap panel

Luc Michalski @.***> 于2021年4月7日周三 下午5:01写道:

I added longitude, latitude to the metrics as it is required by the panel but it does not display the points as it also needs a metric...

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/penglongli/gin-metrics/issues/2#issuecomment-814738448, or unsubscribe https://github.com/notifications/unsubscribe-auth/AB4FTN3U3B7ABOVJI5ZCXULTHQNNLANCNFSM42N6CTKA .

ghost commented 3 years ago

you are just awesome ^^

penglongli commented 3 years ago

you are just awesome ^^

Try with this article: https://www.robustperception.io/using-the-worldmap-panel-with-prometheus

penglongli commented 3 years ago

you are just awesome ^^

I made some fake data. (I don't know why the worldmap not showing up)

image

ghost commented 3 years ago

Hi @penglongli ,

Hope you are all well !

That's what I managed to get for now:

Screenshot 2021-04-09 at 06 36 52

It is not displaying the total count , any ideas mate ?

penglongli commented 3 years ago

count

Don't use rate QL, just summary_country

ghost commented 3 years ago

I tried but I have the following message:

Screenshot 2021-04-09 at 12 04 48
penglongli commented 3 years ago

I tried but I have the following message:

Screenshot 2021-04-09 at 12 04 48

Try sum(summary_country) by (country)

ghost commented 3 years ago

it works nicely :-)

Screenshot 2021-04-09 at 14 17 12