labstack / echo-contrib Goto Github PK
View Code? Open in Web Editor NEWEcho community contribution
Home Page: https://echo.labstack.com
License: MIT License
Echo community contribution
Home Page: https://echo.labstack.com
License: MIT License
How to clear or delete session key ?
This project seems to not be able to be used when importing echo as github.com/labstack/echo/v4
with Go Modules.
package main
import (
"github.com/gorilla/sessions"
"github.com/labstack/echo-contrib/session"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)
func main() {
e := echo.New()
e.Renderer = &controller.Template{}
e.Use(middleware.Logger())
// Error here
e.Use(session.Middleware(sessions.NewCookieStore([]byte("secret"))))
The error message is:
cannot use session.Middleware(sessions.NewCookieStore(([]byte)(\"secret\"))) (type \"github.com/labstack/echo\".MiddlewareFunc) as type \"github.com/labstack/echo/v4\".MiddlewareFunc in argument to e.Use
Hello, I'm just checking out the middleware (don't want to write my own)..
Typically I'm used to just using the vanilla prometheus promauto
(EG promauto.NewCounter()
to create custom metrics. However with this library and the examples it is not clear how this is done with this middleware.
I can see severally public functions but honestly I can't figure out how to just do this simple thing.
Is this even possible? If so, I'm happy to PR some docs to echo with an example if someone can explain to me how to access them. (thanks)
I tried to implement Login and Logout. When I tried to logout, its not clearing the session. Here is my code:
func Logout(c echo.Context) error {
sess, _ := session.Get("session", c)
sess.Options = &sessions.Options{
Path: "/",
MaxAge: -1,
HttpOnly: true,
}
sess.Values["username"] = ""
sess.Values["authenticated"] = ""
sess.Save(c.Request(), c.Response())
return c.Redirect(http.StatusSeeOther, "/")
}
After logout I checked the session values, its not changing or clearing anything.
Currently there is only support for jaeger tracing. While it is possible to have also support for opentracing, it is valuable to support zipkin along with it as it will do more accurate usage of the tracing library.
cc @arun0009
Today I use this package test the function of session, but I found when I firstly enter the website, in that time the request don't have session, but use Sesson.Get can't get error. But I think if the session is empty, maybe should return a error.
eg:
code: sess, err := session.Get("userInfo", c)
but in the first time the err is nil.
Can you answer my question?
Thanks.
echo-contrib/jaegertracing/jaegertracing.go
Line 240 in fd04525
POST requests are sent with a nil body? the body param is not used or I'm missing something?
Thanks
When an HTTP Handler passes down an echo.HTTPError Jaeger still reports a response code of 200, which is incorrect.
I'm having some trouble using v2 of casbin with this pacakge:
./api.go:88:28: cannot use ce (type *"github.com/casbin/casbin/v2".Enforcer) as type *"github.com/casbin/casbin".Enforcer in argument to "github.com/labstack/echo-contrib/casbin".Middleware
Would be nice to update the casbin dep to v2.
P.S. I could make a quick PR if necessary.
Hello, I've recently fell in love with echo and thought that it would be great if I can contribute a middleware that I've developed. I couldn't find any guidelines for contribution so it would be great if I can be pointed into some direction.
Thanks in advance.
Either I am missing something, or the current implementation of prometheus handler will not capture any code returned from handlers as it will trigger only at the beginning of the request.
package main
import (
"fmt"
"net/http"
"time"
"github.com/labstack/echo-contrib/prometheus"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)
const listenAddress = "localhost:1323"
func main() {
e := echo.New()
// Enable metrics middleware
p := prometheus.NewPrometheus("echo", nil)
p.Use(e)
e.Use(middleware.Logger())
e.Use(middleware.Recover())
e.GET("/", func(c echo.Context) error {
return c.String(http.StatusOK, "Hello, World!")
})
e.GET("/404", func(c echo.Context) error {
return c.String(http.StatusNotFound, "404")
})
e.GET("/500", func(c echo.Context) error {
return c.String(http.StatusInternalServerError, "oops")
})
go test()
e.Logger.Fatal(e.Start(listenAddress))
}
func test() {
time.Sleep(time.Second * 1)
urls := []string{
"/",
"/404",
"/500",
}
for _, url := range urls {
resp, err := http.Get(fmt.Sprintf("http://%s%s", listenAddress, url))
if err != nil {
fmt.Printf("Error %s\n", err)
continue
}
_ = resp.Body.Close()
}
}
When executed this program will output the following log:
{"time":"2019-12-19T16:42:28.748008Z","id":"","remote_ip":"127.0.0.1","host":"localhost:1323","method":"GET","uri":"/","user_agent":"Go-http-client/1.1","status":200,"error":"","latency":14464,"latency_human":"14.464µs","bytes_in":0,"bytes_out":13}
{"time":"2019-12-19T16:42:28.749981Z","id":"","remote_ip":"127.0.0.1","host":"localhost:1323","method":"GET","uri":"/404","user_agent":"Go-http-client/1.1","status":404,"error":"","latency":6046,"latency_human":"6.046µs","bytes_in":0,"bytes_out":3}
{"time":"2019-12-19T16:42:28.75144Z","id":"","remote_ip":"127.0.0.1","host":"localhost:1323","method":"GET","uri":"/500","user_agent":"Go-http-client/1.1","status":500,"error":"","latency":3108,"latency_human":"3.108µs","bytes_in":0,"bytes_out":4}
however, the metrics page will report
echo_requests_total{code="200",host="localhost:1323",method="GET",url="/"} 1
echo_requests_total{code="200",host="localhost:1323",method="GET",url="/404"} 1
echo_requests_total{code="200",host="localhost:1323",method="GET",url="/500"} 1
Hi
go.sum
go: github.com/belogik/[email protected]: git fetch -f origin refs/heads/*:refs/heads/* refs/tags/*:refs/tags/* in /go/pkg/mod/cache/vcs/5b0453a6aac74c86b31f0a771f02325d031eaf6ec0f0503f1cc20852de3abb0a: exit status 128:
fatal: could not read Username for 'https://github.com': terminal prompts disabled
Related gofight is updated
go get -u github.com/appleboy/gofight
Request
The issue labstack/echo#1127 made a good point about sessions.
Why not update session package using 'Before' callback:
https://sourcegraph.com/github.com/labstack/echo/-/blob/response.go#L40
so the session writes automatically?
Currently this project includes way too many dependencies as it is a single module. Making all of them a different module would avoid users to pull dependencies that aren't important for their use case e.g. pulling jaeger and zipkin when willing to use prometheus. Does that make sense?
Hi Team! Recently I discovered that the latest version tagged has been a long time ago, I would like to know when we will have a new tag.
I'm asking because has new features in the repo there are important like the Prometheus labels:
https://github.com/labstack/echo-contrib/blob/master/prometheus/prometheus.go#L55
Hello. I'm new to Go. I was wondering if there is a way to add custom labels to the standard prometheus metrics that are collected by the echo middleware.
I have tried to do add middleware using the CurryWith(Labels) API on respective MetricVec classes.
I run into this error when the middleware gets run.
2 unknown label(s) found during currying
I've attached some sample code of what I'm trying to accomplish. My end goal is adding my two custom labels to all metrics being generated.
Appreciate the time.
var errorCount = &prometheus.Metric{
ID: "err_cnt",
Name: "error_count",
Description: "Error count",
Type: "counter_vec",
Args: []string{"code", "method", "host", "url", "myLabel1", "myLabel2"},
}
type MyMetrics struct {
errorCount *prometheus.Metric // my own custom defined metric
standardMetrics []*prometheus.Metric // populated with prometheus.MetricsList
}
func (metrics *MyMetrics) addLabels() {
labels := ptheus.Labels{"myLabel1": "value1", "myLabel2": "value2"}
errorCount.MetricCollector.(*ptheus.CounterVec).With(labels)
// attempt to loop over standard metrics and add my custom labels
for i := 0; i < len(metrics.standardMetrics); i++ {
standardMetric := metrics.standardMetrics[i]
if standardMetric.MetricCollector != nil {
switch standardMetric.Type {
case "histogram_vec":
curriedMetric, err := standardMetric.MetricCollector.(*ptheus.GaugeVec).CurryWith(labels)
if curriedMetric != nil {
standardMetric.MetricCollector = curriedMetric
} else if err != nil {
fmt.Printf("Histogram Error %s with metric named %s\n", err.Error(), standardMetric.Name)
}
case "counter_vec":
standardMetric.MetricCollector.(*ptheus.CounterVec).With(labels)
curriedMetric, err := standardMetric.MetricCollector.(*ptheus.GaugeVec).CurryWith(labels)
curriedMetric, err := standardMetric.MetricCollector.(*ptheus.CounterVec).CurryWith(labels)
if curriedMetric != nil {
standardMetric.MetricCollector = curriedMetric
} else if err != nil {
fmt.Printf("Counter Error %s\n", err.Error())
}
.....
fmt.Printf("Type of %s metric is %s\n", standardMetric.Name, standardMetric.Type)
}
}
}
// middleware function that I've attached to my echo instance
func (metrics *MyMetrics) customMiddleware(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
metrics.addLabels()
return next(c)
}
}
func createMetricsServer(e *echo.Echo, port string) error {
echoPrometheus := echo.New()
echoPrometheus.HideBanner = true
echoPrometheus.HidePort = true
customMetricsList := []*prometheus.Metric{
errorCount,
}
prom := prometheus.NewPrometheus("myService", urlSkipper, customMetricsList)
myMetrics := &MyMetrics{
errorCount: errorCount,
standardMetrics: prom.MetricsList,
}
// Middleware that replaces the values of uuids in pathnames for more consistent triaging and metrics
prom.RequestCounterURLLabelMappingFunc = func(c echo.Context) string {
url := c.Request().URL.Path
for _, p := range c.ParamNames() {
if p == "uuid" {
url = strings.Replace(url, c.Param(p), ":uuid", 1)
} else if p == "attachmentUuid" {
url = strings.Replace(url, c.Param(p), ":attachmentUuid", 1)
}
}
return url
}
e.Use(myMetrics.customMiddleware)
e.Use(prom.HandlerFunc)
prom.SetMetricsPath(echoPrometheus)
// Starts a new goroutine to manage the metrics endpoint
go func() { echoPrometheus.Logger.Fatal(echoPrometheus.Start(port)) }()
log.Info().Msgf("Prometheus Metrics established started on localhost%s", port)
return nil
}
When using the prometheus middleware AND the Gzip middleware I get a compressed response for /metrics
without the Transfer-Encoding header.
Every time a user requests a unique URL that returns 404, the Prometheus middleware generates new metrics. This can easily generate millions of timeseries, overloading a Prometheus server or triggering other limits.
Minimal reproducer: https://gist.github.com/brietaylor/c5aa76df6a2f3fa3a8e14e4f8250cab1
Debugging:
I was able to trace this back through the code somewhat. The code that generates these URLs ultimately traces back to router.go:Find()
in Echo. That code initially sets Context.path
to the request path, but on all success paths, it overrides Context.path
to the route path. I couldn't find any way for middlewares to detect which was done, though, so this might need to be an upstream fix.
While I following official session example, it throws this error:
cannot use session.Middleware(sessions.NewCookieStore(([]byte)("secret"))) (type "github.com/labstack/echo/v4".MiddlewareFunc) as type "github.com/labstack/echo".MiddlewareFunc in argument to e.Use
here is the go.mod:
module github.com/pyprism/Proj
go 1.12
require (
github.com/fastly/go-utils v0.0.0-20180712184237-d95a45783239 // indirect
github.com/gorilla/sessions v1.2.0
github.com/jehiah/go-strftime v0.0.0-20171201141054-1d33003b3869 // indirect
github.com/jinzhu/gorm v1.9.10
github.com/labstack/echo v3.3.10+incompatible
github.com/labstack/echo-contrib v0.6.0
github.com/labstack/echo/v4 v4.1.11 // indirect
github.com/lestrrat-go/file-rotatelogs v2.2.0+incompatible
github.com/lestrrat-go/strftime v0.0.0-20190725011945-5c849dd2c51d // indirect
github.com/sirupsen/logrus v1.2.0
github.com/spf13/viper v1.4.0
github.com/tebeka/strftime v0.1.3 // indirect
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4
)
Hello everyone
I'm trying to integrate external services into the app but Jaeger Service not shown in Jaeger UI.
Where am I doing wrong? How can I fix it?
Help me please.
Sample app:
https://github.com/delgec/echo-opentelemetry-sample
or maybe have a cookbook recipe
Hi all
Is there a way to disable certain type of metrics at the moment? For example, disable request_size_bytes_bucket
or just completely disable histograms for any type of metric.
In my case, i am doing a proxy type of a setup with at least 20 routes, so having histogram, bytes, etc generates tons of metrics.
The current Prometheus middleware accepts only a subsystem argument:
p := prometheus.NewPrometheus("<subsystem>", nil)
p.Use(e)
Namespaces usually are used to prefix the metric name like
<namespace>_<subsystem>_<metricname>
This is helpful to avoid name conflicts (by using your application/company name as a namespace).
Every Metric type (e.g., Gaugse), inherits Opts which supports a Namespace
attribute.
#80 and #92 can go in the right direction, tho, namespace is not added to it yet.
Being able to accept a namespace AND subsystem name.
Only the subsystem is accepted as an argument: https://echo.labstack.com/middleware/prometheus/
The current method signature looks like:
func NewPrometheus(subsystem string, skipper middleware.Skipper, customMetricsList ...[]*Metric) *Prometheus {
...
A few other Prometheus implementations include the namespace as the first character, like:
func NewPrometheus(namespace, subsystem string, skipper middleware.Skipper, customMetricsList ...[]*Metric) *Prometheus {
This would be a breaking change but would follow the "order" of how namespace and subsystem is used.
A possible workaround would be to set subsystem
to namespace_subsystem
.
Imagine your namespace is tesla
and your subsystem is modelx
, you would pass tesla_modelx
.
Maybe better to support OpenTracing rather than specific to or has dependency to Jaeger
First of all, I'm new to golang so bear with me. I think that I'm missing something but I have an issue that is driving me crazy.
I'm using casbin_mw
as follows:
e.Use(casbin_mw.MiddlewareWithConfig(casbin_mw.Config{
Skipper: authorization.Skipper([]string{"/login", "/register"}),
Enforcer: enforcer,
UserGetter: authorization.UserGetter(),
}))
And I'm doing some testing to check unauthorized access to some resources of my HTTP API. The issue here is: when I run all tests with go test -v ./...
a test that should fail because unauthorized access is passing but if I run that same test in isolation it is failing as expected, but I don't know why this is happening.
I tried several things and came up with something like this:
e.Use(casbin_mw.MiddlewareWithConfig(casbin_mw.Config{
Skipper: authorization.Skipper([]string{"/login", "/register"}),
Enforcer: enforcer,
UserGetter: authorization.UserGetter(),
// TODO: This ErrorHandler is added
ErrorHandler: func(c echo.Context, internal error, proposedStatus int) error {
if proposedStatus == http.StatusForbidden {
log.Warn().Str("path", c.Path()).Str("internal", internal.Error()).Msg("Unauthorized access")
}
err := echo.NewHTTPError(proposedStatus, internal.Error())
err.Internal = internal
return err
},
}))
If I run all tests or just one test should fail always with my configuration without an ErrorHandler
.
Test fails in isolation but not when running it along with other tests.
I have the following package structure:
project
| - test
|- integration
|- example_test.go // some tests
|- example2_test.go // my tests that fails in isolation but not when running it along with other tests
|- main_test.go // initializes the database and creates an *echo.Echo for all integration tests
}
v0.13.0
Hello
I was having problems with prometheus middleware. When there is an error, prometheus middleware is calling error handler once more than needed. I think this row have to be commented
https://github.com/labstack/echo-contrib/blob/master/prometheus/prometheus.go#L374
I have a configured tracer which is used for tracing grpc requests. Now I want to use the same tracer for http requests. Unfortunately there is no config option in tracing-middleware to provide an existing tracer.
Need the SetListenAddress in prometheus to include the middleware in my project where the go project is being used as an API gateway and if the default address is set to give the metrics it will make the metrics open for the public to hit.
here is my config. it seems the jeager tracer doesnt care my env.
JAEGER_AGENT_HOST=127.0.0.1;
JAEGER_AGENT_PORT=6831;
JAEGER_SAMPLER_PARAM=0.001;
JAEGER_SAMPLER_TYPE=const
it's better to add labels like method, url, code into other metrics:
like echo_requests_total
:
# HELP echo_requests_total How many HTTP requests processed, partitioned by status code and HTTP method.
# TYPE echo_requests_total counter
echo_requests_total{code="200",host="localhost:8080",method="GET",url="/"} 1
Hi,
We're using Echo at my company for our microservices and we're about to add prometheus to all of them using this prometheus middleware.
I've been used to finding dashboards created by people when using other web frameworks like Sprint Boot og .NET, but I'm unable to find one for Echo.
Are you familiar with any dashboards created by the community? :)
Hey everyone!
I wanted to use this package today, most commonly the session part, but while running the
go get github.com/labstack/echo-contrib/session
I get the error below:
➜ go get github.com/labstack/echo-contrib/session
go: finding github.com/labstack/echo-contrib/session latest
go: finding github.com/belogik/goes v0.0.0-20151229125003-e54d722c3aff
go: github.com/belogik/[email protected]: git fetch -f https://github.com/belogik/goes refs/heads/*:refs/heads/* refs/tags/*:refs/tags/* in /Users/x/go/pkg/mod/cache/vcs/5b0453a6aac74c86b31f0a771f02325d031eaf6ec0f0503f1cc20852de3abb0a: exit status 128:
remote: Repository not found.
fatal: repository 'https://github.com/belogik/goes/' not found
go: error loading module requirements
It seems that this repo is gone for good, probably someone took over the username or something like that. What is this used for or is this something related to a 3rd party package?
Or maybe if someone has this package offline cached, we could get that up somewhere.
Thanks for your help!
Edit: I found this archived web page from 2018 https://github.com/belogik/goes/
That page says that anyone using the package, should rather use this one: https://github.com/OwnLocal/goes.
Related labstack/echo#1494
For all the standard Metrics, there are 3 labels present, However, the 4th Label host is available on one of the metrics
echo_request_size_bytes_count{code="200",method="POST",url="/v1/api"} 1200
echo_requests_total{code="200",host="localhost:9090",method="POST",url="/v1/api"} 104
Ideally we should add Host label as having host helps in differentiating the same API call across multiple service while scraping these metrics
package main
func main() {
}
instead of NewPrometheus(subsystem string, skipper middleware.Skipper, customMetricsList ...[]*Metric)
that has configuration as list of arguments we should have single argument that is configuration struct. This way it is easier to add new options.
Current and closed issues related to Prometheus:
https://github.com/labstack/echo-contrib/issues?q=is%3Aissue+prometheus+
(for quick look on titles)
Hi, This useful middleware has helped us!
There are cases where we want to overwrite some buckets values.
For example, set buckets value of "request_duration_seconds" greather than 10 seconds.
How about defining a config and allowing it to be overridden?
I would like to get your opinion this.
type Config struct {
OverwriteDefaultMetricsList []OverwriteDefaultMetrics
}
type OverwriteDefaultMetrics struct {
ID string // example "reqDur"
Buckets []float64 // example []float64{.005, .01, .025, .05, .1, .25, .5, 1, 2.5, 5, 10, 20, 40, 80}
}
none
none
none
package main
func main() {
}
I would really like to be able to track the number of requests that are currently in flight for my application.
To do this, I could of course create my own middleware that wraps each request, but with this approach it would be difficult for my custom metric to match the naming convention and label-set as those metrics provided by the prometheus package in this repo.
Would the maintainers here be open to a PR from me where I add an requests_in_flight gauge to track the number of currently running packages?
https://github.com/labstack/echo-contrib/blob/master/prometheus/prometheus.go#L379
This is where error checking and error handler is called. Then, for some reason, the function returns the same error and echo itself repeats the check and call the handler. Duplicate error responses are obtained.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.