Skip to content

Commit

Permalink
Fix total not sum up in http check
Browse files Browse the repository at this point in the history
  • Loading branch information
v9n committed Dec 14, 2019
1 parent 67b6be8 commit 07ccfde
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 64 deletions.
2 changes: 1 addition & 1 deletion config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Application < Rails::Application
scope: 'bot',
client_id: '51439348069.157091434678',
redirect_uri: 'http://127.0.0.1:3000/bot/slack',
client_secret: ENV.fetch('SLACK_CLIENT_SECRET', Rails.application.credentials.fetch(:SLACK_CLIENT_SECRET))
client_secret: Rails.application.credentials.fetch(:SLACK_CLIENT_SECRET, ENV['SLACK_CLIENT_SECRET'])
}
# End custom configuration

Expand Down
2 changes: 1 addition & 1 deletion config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
config.assets.compile = false

# Enable serving of images, stylesheets, and JavaScripts from an asset server.
config.action_controller.asset_host = 'https://assets.noty.im'
config.action_controller.asset_host = ENV['ASSETS_HOST'] || 'https://next.noty.im'

# Specifies the header that your server uses for sending files.
# config.action_dispatch.x_sendfile_header = 'X-Sendfile' # for Apache
Expand Down
2 changes: 1 addition & 1 deletion config/initializers/bugsnag.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

Bugsnag.configure do |config|
config.api_key = Rails.application.credentials[:BUGSNAG_KEY]
config.api_key = ENV['BUGSNAG_KEY'] || Rails.application.credentials[:BUGSNAG_KEY]
config.app_version = ENV['GIT_COMMIT'] || 'master'
end
56 changes: 56 additions & 0 deletions extras/gaia/scanner/example/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package main

import (
"fmt"
"log"
"net"
"net/http"
"time"

"github.com/notyim/gaia/scanner/httpscanner"
"net/http/httptrace"
)

func main() {
req, _ := http.NewRequest("GET", "https://axcoto.com", nil)

trace := &httptrace.ClientTrace{
DNSStart: func(i httptrace.DNSStartInfo) {
fmt.Printf("DNS Start O")
},

DNSDone: func(dnsInfo httptrace.DNSDoneInfo) {
fmt.Printf("DNS Info: %+v\n", dnsInfo)
},
GotConn: func(connInfo httptrace.GotConnInfo) {
fmt.Printf("Got Conn: %+v\n", connInfo)
},
}
req = req.WithContext(httptrace.WithClientTrace(req.Context(), trace))

client := &http.Client{
Timeout: 30 * time.Second,
Transport: &http.Transport{
Dial: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).Dial,
TLSHandshakeTimeout: 10 * time.Second,
ResponseHeaderTimeout: 10 * time.Second,
ExpectContinueTimeout: 10 * time.Second,
},

CheckRedirect: func(req *http.Request, via []*http.Request) error {
// always refuse to formatllow redirects,
return http.ErrUseLastResponse
},
}

if _, err := client.Do(req); err != nil {
log.Fatal(err)
}

log.Println("\n\n\n===================\n\n\n\n\n\n\n\n")
m := httpscanner.Check(req)
fmt.Println(m.Timing)
}
77 changes: 76 additions & 1 deletion extras/gaia/scanner/httpscanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import (
"crypto/tls"
"fmt"
"io"
"io/ioutil"
"log"
"net"
"net/http"
"net/http/httptrace"
"strings"
Expand Down Expand Up @@ -181,6 +184,7 @@ func (r *Result) End(t time.Time) {
}

r.contentTransfer = r.transferDone.Sub(r.transferStart)
fmt.Println("END")
r.total = r.transferDone.Sub(r.dnsStart)
}

Expand All @@ -199,7 +203,7 @@ func (r *Result) Total(t time.Time) time.Duration {
}

func withClientTrace(ctx context.Context, r *Result) context.Context {
return httptrace.WithClientTrace(ctx, &httptrace.ClientTrace{
trace := &httptrace.ClientTrace{
DNSStart: func(i httptrace.DNSStartInfo) {
r.dnsStart = time.Now()
},
Expand Down Expand Up @@ -290,5 +294,76 @@ func withClientTrace(ctx context.Context, r *Result) context.Context {

r.transferStart = r.serverDone
},
}

return httptrace.WithClientTrace(ctx, trace)
}

func Check(req *http.Request) *CheckResponse {
req.Header.Set("User-Agent", "noty/2.0 (https://noty.im)")
var result Result

ctx := WithHTTPStat(req.Context(), &result)

var cancel context.CancelFunc
ctx, cancel = context.WithCancel(ctx)
time.AfterFunc(30*time.Second, func() {
cancel()
})

req = req.WithContext(ctx)

httpClient := &http.Client{
Timeout: 30 * time.Second,
Transport: &http.Transport{
//Dial: (&net.Dialer{
// Timeout: 30 * time.Second,
// KeepAlive: 30 * time.Second,
//}).Dial,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
}).DialContext,
TLSHandshakeTimeout: 10 * time.Second,
ResponseHeaderTimeout: 10 * time.Second,
ExpectContinueTimeout: 10 * time.Second,
},

CheckRedirect: func(req *http.Request, via []*http.Request) error {
// always refuse to formatllow redirects,
return http.ErrUseLastResponse
},
}

res, err := httpClient.Do(req)
if res != nil {
defer res.Body.Close()
}

if err != nil {
log.Println("Error when perform http check request")
return nil
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Println("Cannot read body", err)
return nil
}
result.End(time.Now())

metric := &CheckResponse{
RunAt: time.Now(),
StatusCode: res.StatusCode,
Status: res.Status,
ContentLength: res.ContentLength,
Header: res.Header,
Timing: result.ToCheckTiming(),
Body: string(body),
}

//log.Printf("Response metric %v\n", metric)
log.Printf("Scanner Timing %v", result.Durations())

return metric
}
62 changes: 2 additions & 60 deletions extras/gaia/scanner/scanner.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package scanner

import (
"context"
"encoding/json"
"io/ioutil"
"log"
"net"
"net/http"
"time"

Expand Down Expand Up @@ -63,71 +60,16 @@ func checkTCP(check *dao.Check, agent MetricWriter) {
func checkHTTP(check *dao.Check, agent MetricWriter) {
t0 := time.Now()
defer func() {
// Telegram hook to report abnormal thing
log.Printf("Check %s finish in %v", check.URI, time.Now().Sub(t0))

}()
req, err := http.NewRequest("GET", check.URI, nil)
if err != nil {
log.Println("Error creating http request for")
return
}

req.Header.Set("User-Agent", "noty/2.0 (https://noty.im)")
var result httpscanner.Result
var cancel context.CancelFunc
ctx := httpscanner.WithHTTPStat(req.Context(), &result)
ctx, cancel = context.WithCancel(ctx)
time.AfterFunc(30*time.Second, func() {
cancel()
})

req = req.WithContext(ctx)

httpClient := &http.Client{
Timeout: 30 * time.Second,
Transport: &http.Transport{
Dial: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).Dial,
TLSHandshakeTimeout: 10 * time.Second,
ResponseHeaderTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
},

CheckRedirect: func(req *http.Request, via []*http.Request) error {
// always refuse to formatllow redirects,
return http.ErrUseLastResponse
},
}

res, err := httpClient.Do(req)
if res != nil {
defer res.Body.Close()
}
if err != nil {
log.Println("Error when perform http check request")
return
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Println("Cannot read body", err)
return
}
result.End(time.Now())

metric := &httpscanner.CheckResponse{
RunAt: time.Now(),
StatusCode: res.StatusCode,
Status: res.Status,
ContentLength: res.ContentLength,
Header: res.Header,
Timing: result.ToCheckTiming(),
Body: string(body),
}

//log.Printf("Response metric %v\n", metric)
log.Printf("Scanner Timing %v", result.Durations())
metric := httpscanner.Check(req)

runResult := gaia.EventCheckHTTPResult{
EventType: gaia.EventTypeCheckHTTPResult,
Expand Down

0 comments on commit 07ccfde

Please sign in to comment.