-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathperformance_client.go
141 lines (126 loc) · 4.05 KB
/
performance_client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package main
import (
"context"
"crypto/tls"
"encoding/xml"
"fmt"
"net/http"
"net/http/cookiejar"
"time"
log "github.com/sirupsen/logrus"
)
// ApiMonitorClient API client
type ApiMonitorClient struct {
client *http.Client // client reference to exist HTTP Client
session string // session actual id
requests uint64 // requests success created request
responses uint64 // responses success obtains response
responseErrors uint64 // responseErrors error obtain response
}
// NewApiMonitorClient create new API client with prepared http.Client
func NewApiMonitorClient() *ApiMonitorClient {
var cp ApiMonitorClient
jar, _ := cookiejar.New(nil)
if config.IgnoreCertificate {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
cp = ApiMonitorClient{
client: &http.Client{Transport: tr, Jar: jar},
requests: 0,
responses: 0,
responseErrors: 0,
session: "",
}
} else {
cp = ApiMonitorClient{
client: &http.Client{Jar: jar},
requests: 0,
responses: 0,
responseErrors: 0,
session: "",
}
}
return &cp
}
// processRequest process one request to API with predefined timeout
// program returns collected body or error if here any problem
func (p *ApiMonitorClient) processRequest(name string, inner string) (body string, err error) {
if LogRequestDuration {
defer duration(track(log.Fields{FieldRoutine: "processRequest"}, "procedure ends"))
}
var req *http.Request
var resp *http.Response
s := fmt.Sprintf(Envelope, inner)
requestId := RandomString()
req, err = perfRequestCreate(requestId, s)
p.requests++
if err != nil {
log.WithFields(p.logFields(name)).Errorf("problem prepare %s request. Error: %s", name, err)
return "", err
}
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(config.ApiTimeout)*time.Second)
defer cancel()
req = req.WithContext(ctx)
body, resp, err = perfRequestResponse(requestId, p.client, req)
if resp != nil && resp.StatusCode > 299 {
log.WithFields(p.logFields(name)).Errorf("problem read %s response. Status code: %s", name, resp.Status)
var f FaultResponse
err = xml.Unmarshal([]byte(body), &f)
p.responseErrors++
if resp.StatusCode == 401 || err != nil {
return fmt.Sprintf("%d", resp.StatusCode), fmt.Errorf("response status is %s - %s %s", resp.Status, f.FaultCode, f.FaultString)
}
return f.FaultCode, fmt.Errorf("response status is %s - %s %s", resp.Status, f.FaultCode, f.FaultString)
}
if err != nil {
log.WithFields(p.logFields(name)).Errorf("problem read %s response. Error: %s", name, err)
p.responseErrors++
return "", err
}
for _, cookie := range resp.Cookies() {
if cookie.Name == "JSESSIONIDSSO" {
p.session = cookie.Value
log.WithFields(p.logFields(name)).Debugf("JSESSIONIDSSO cookie received and stored: %s", p.session)
break
}
log.WithFields(p.logFields(name)).Debugf("no cookie JSESSIONIDSSO")
}
p.responses++
body, err = perfRequestBodyRelevant(body)
if err != nil {
log.WithFields(p.logFields(name)).Errorf("problem analyze %s response. Error: %s", name, err)
return "", err
}
return body, nil
}
// isSessionOpen Define if connection is UP
func (p *ApiMonitorClient) isSessionOpen() bool {
return len(p.session) > 0
}
// logFields create valid list of log fields depend on server
func (p *ApiMonitorClient) logFields(operation ...string) log.Fields {
var f log.Fields
if len(operation) == 1 {
f = log.Fields{
FieldSession: p.session,
FieldIsUp: p.isSessionOpen(),
FieldRoutine: operation,
}
} else {
f = log.Fields{
FieldIsUp: p.isSessionOpen(),
FieldSession: p.session,
}
}
return f
}
// print List actual error status of client
func (p *ApiMonitorClient) print() string {
msg := "Client status"
msg = fmt.Sprintf("%s\r\nRequests %d", msg, p.requests)
msg = fmt.Sprintf("%s\r\nResponses %d", msg, p.responses)
msg = fmt.Sprintf("%s\r\nError %d", msg, p.responseErrors)
msg = fmt.Sprintf("%s\r\nConnected %t", msg, p.isSessionOpen())
return msg
}