Skip to content

Instantly share code, notes, and snippets.

View rafaeljesus's full-sized avatar

Rafael Jesus rafaeljesus

  • Berlin, Germany
View GitHub Profile
@rafaeljesus
rafaeljesus / tracer.go
Created August 18, 2018 15:36
Prometheus metrics jaeger
package tracer
import (
"io"
opentracing "github.com/opentracing/opentracing-go"
"github.com/prometheus/client_golang/prometheus"
jaegercfg "github.com/uber/jaeger-client-go/config"
"github.com/uber/jaeger-client-go/rpcmetrics"
jprom "github.com/uber/jaeger-lib/metrics/prometheus"
@rafaeljesus
rafaeljesus / gist:87e5fb87a3fd12377f591e31a1edd73c
Created June 12, 2018 08:31 — forked from fduran/gist:1870546
Linux check DNS cache snooping
# www.fduran.com
# Linux check DNS cache snooping
# check if somedomain (try popular ones like google.com etc) is cached in a dns_server
# with nslookup
nslookup -norecurse somedomain dns_server
# with dig
dig @dns_server somedomain A +norecurse
func main() {
breaker := newCircuitBreaker()
transport := http.NewTransport(breaker)
req := http.NewRequest(
http.WithRoundTripper(transport),
)
client := http.NewClient(req)
_ = http.NewStoreService(client)
@rafaeljesus
rafaeljesus / store.go
Created March 18, 2018 21:17
Store responsible for storing the user.
// Store responsible for storing the user.
func (s *StoreService) Store(user *User) error {
userResourceV2 := fmt.Sprintf("%s/%s/%s", usersAPIV2, user.Email, user.Country)
res, err := s.client.Get(userResourceV2)
if err != nil {
if err == srv.ErrCircuitBreakerOpen {
// fallback to apiv1
userResourceV1 := fmt.Sprintf("%s?%s&%s", usersAPIV1, user.Email, user.Country)
res, err = s.client.Get(userResourceV1)
if err != nil {
@rafaeljesus
rafaeljesus / request_do.go
Created March 18, 2018 19:35
Request Do Method
// Do is a convenient method for executing http requests.
func (r *Request) Do(method, url, contentType string, body io.Reader) (*http.Response, error) {
req, err := http.NewRequest(method, url, body)
if err != nil {
return nil, fmt.Errorf("failed to create request %v: ", err)
}
req.Header.Set("Content-Type", contentType)
ctx, cancel := context.WithTimeout(req.Context(), reqTimeout)
type (
// RequestOption is the request options.
RequestOption func(*Request)
// Request is the application http request.
Request struct {
client *http.Client
}
)
type (
// Breaker is the http circuit breaker.
Breaker interface {
// Execute runs the given request if the circuit breaker is closed or half-open states.
// An error is instantly returned when the circuit breaker is tripped.
Execute(fn func() (interface{}, error))
}
// Transport is the application http transport.
Transport struct {
@rafaeljesus
rafaeljesus / roud_tripper.go
Created March 18, 2018 19:25
RoundTripper
type RoundTripper interface {
RoundTrip(*Request) (*Response, error)
}
@rafaeljesus
rafaeljesus / store.go
Created March 18, 2018 19:24
Store Method
// Store responsible for storing the user.
func (s *StoreService) Store(user *User) error {
userResourceV2 := fmt.Sprintf("%s/%s/%s", usersAPIV2, user.Email, user.Country)
res, err := s.client.Get(userResourceV2)
if err != nil {
return fmt.Errorf("failed to fetch user: %v", err)
}
// code omitted