Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
913 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
[[constraint]] | ||
name = "github.com/devopsfaith/krakend" | ||
version = "0.4.2" | ||
|
||
[[constraint]] | ||
name = "github.com/gin-gonic/gin" | ||
version = "1.2.0" | ||
|
||
[[constraint]] | ||
name = "go.opencensus.io" | ||
version = "0.8.0" | ||
|
||
[prune] | ||
non-go = true | ||
go-tests = true | ||
unused-packages = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
KrakenD opencensus | ||
==== | ||
|
||
A set of building blocks for instrumenting [KakenD](http://www.krakend.io) gateways | ||
|
||
## Available middlewares | ||
|
||
There are the avaliable middlewares to add to the KrakenD pipes. | ||
|
||
1. Backend | ||
2. Proxy | ||
3. Router | ||
|
||
## Available router flavours | ||
|
||
1. [mux](github.com/devopsfaith/krakend-opencensus/blob/master/mux) Mux based routers and handlers | ||
2. [gin](github.com/devopsfaith/krakend-opencensus/blob/master/gin) Gin based routers and handlers | ||
|
||
Check the examples and the documentation for more details |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
Example | ||
==== | ||
|
||
## Dependencies | ||
|
||
### Zipkin server | ||
|
||
$ docker run -d -p 9411:9411 openzipkin/zipkin | ||
|
||
### Prometheus server | ||
|
||
$ brew install prometheus | ||
$ prometheus | ||
|
||
## Build and Run | ||
|
||
$ go build | ||
$ ./example -l DEBUG -d -p 8080 -c krakend.json -name service_name -s 9091 | ||
|
||
|
||
|
||
Exposed traces: http://192.168.99.100:9411/zipkin/ | ||
|
||
Exposed metrics: http://127.0.0.1:9090/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"fmt" | ||
"log" | ||
"net" | ||
"net/http" | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
"time" | ||
|
||
"github.com/devopsfaith/krakend/config" | ||
"github.com/devopsfaith/krakend/logging" | ||
"github.com/devopsfaith/krakend/proxy" | ||
krakendgin "github.com/devopsfaith/krakend/router/gin" | ||
"github.com/gin-gonic/gin" | ||
"github.com/openzipkin/zipkin-go/model" | ||
httpreporter "github.com/openzipkin/zipkin-go/reporter/http" | ||
"go.opencensus.io/exporter/prometheus" | ||
"go.opencensus.io/exporter/zipkin" | ||
"go.opencensus.io/stats/view" | ||
"go.opencensus.io/trace" | ||
|
||
"github.com/devopsfaith/krakend-opencensus" | ||
opencensusgin "github.com/devopsfaith/krakend-opencensus/router/gin" | ||
) | ||
|
||
func main() { | ||
port := flag.Int("p", 0, "Port of the service") | ||
prometheusPort := flag.Int("s", 9091, "Port of the prometheus service") | ||
logLevel := flag.String("l", "ERROR", "Logging level") | ||
debug := flag.Bool("d", false, "Enable the debug") | ||
configFile := flag.String("c", "/etc/krakend/configuration.json", "Path to the configuration filename") | ||
zipkinURL := flag.String("zipkin", "http://192.168.99.100:9411/api/v2/spans", "url of the zipkin reposrting endpoint") | ||
serviceName := flag.String("name", "krakend", "name of the service") | ||
flag.Parse() | ||
|
||
sigs := make(chan os.Signal, 1) | ||
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
go func() { | ||
select { | ||
case sig := <-sigs: | ||
log.Println("Signal intercepted:", sig) | ||
cancel() | ||
case <-ctx.Done(): | ||
} | ||
}() | ||
|
||
parser := config.NewParser() | ||
serviceConfig, err := parser.Parse(*configFile) | ||
if err != nil { | ||
log.Fatal("ERROR:", err.Error()) | ||
} | ||
serviceConfig.Debug = serviceConfig.Debug || *debug | ||
if *port != 0 { | ||
serviceConfig.Port = *port | ||
} | ||
|
||
logger, _ := logging.NewLogger(*logLevel, os.Stdout, "[KRAKEND]") | ||
|
||
// Register stats and trace exporters to export the collected data. | ||
exporter, err := prometheusExporter(*prometheusPort) | ||
if err != nil { | ||
logger.Fatal(err.Error()) | ||
} | ||
|
||
zipkinExporter := zipkin.NewExporter( | ||
httpreporter.NewReporter(*zipkinURL), | ||
&model.Endpoint{ | ||
ServiceName: *serviceName, | ||
IPv4: net.ParseIP("127.0.0.1"), | ||
Port: uint16(serviceConfig.Port), | ||
}) | ||
|
||
opencensusCfg := opencensus.Config{ | ||
ViewExporters: []view.Exporter{exporter}, | ||
TraceExporters: []trace.Exporter{zipkinExporter}, | ||
ReportingPeriod: time.Second, | ||
SampleRate: 100, | ||
Views: opencensus.DefaultViews, | ||
} | ||
if err := opencensus.Register(opencensusCfg); err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
bf := func(cfg *config.Backend) proxy.Proxy { | ||
return proxy.NewHTTPProxyWithHTTPExecutor(cfg, opencensus.HTTPRequestExecutor(proxy.NewHTTPClient), cfg.Decoder) | ||
} | ||
|
||
// setup the krakend router | ||
routerFactory := krakendgin.NewFactory(krakendgin.Config{ | ||
Engine: gin.Default(), | ||
ProxyFactory: opencensus.ProxyFactory(proxy.NewDefaultFactory(opencensus.BackendFactory(bf), logger)), | ||
Middlewares: []gin.HandlerFunc{}, | ||
Logger: logger, | ||
HandlerFactory: opencensusgin.New(krakendgin.EndpointHandler), | ||
}) | ||
|
||
// start the engine | ||
routerFactory.NewWithContext(ctx).Run(serviceConfig) | ||
} | ||
|
||
func prometheusExporter(port int) (view.Exporter, error) { | ||
exporter, err := prometheus.NewExporter(prometheus.Options{}) | ||
if err != nil { | ||
return exporter, err | ||
} | ||
go func() { | ||
router := http.NewServeMux() | ||
router.Handle("/metrics", exporter) | ||
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), router)) | ||
}() | ||
return exporter, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
global: | ||
scrape_interval: 15s | ||
evaluation_interval: 15s | ||
|
||
rule_files: | ||
# - "first.rules" | ||
# - "second.rules" | ||
|
||
scrape_configs: | ||
- job_name: 'prometheus' | ||
static_configs: | ||
- targets: ['localhost:9090'] | ||
- job_name: 'one' | ||
static_configs: | ||
- targets: ['localhost:9091'] | ||
- job_name: 'two' | ||
static_configs: | ||
- targets: ['localhost:9092'] | ||
- job_name: 'three' | ||
static_configs: | ||
- targets: ['localhost:9093'] |
Oops, something went wrong.