Skip to content

Commit

Permalink
Merge pull request weibocom#7 from zengnjin/server-agent_monitor
Browse files Browse the repository at this point in the history
add a field named remoteIP for http provider headers
  • Loading branch information
zengnjin authored Dec 26, 2017
2 parents 3a0e728 + 1978568 commit 4dfba63
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 7 deletions.
1 change: 1 addition & 0 deletions core/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const (
AddressKey = "address"
GzipSizeKey = "mingzSize"
HostKey = "host"
RemoteIPKey = "remoteIP"
)

// nodeType
Expand Down
2 changes: 1 addition & 1 deletion metrics/graphite.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func genGraphiteMessages(localIP string, snap metrics.Registry) []string {
snap.Each(func(key string, i interface{}) {
var segment string
pni := strings.SplitN(key, ":", 4)

switch m := i.(type) {

case metrics.Counter:
Expand Down
9 changes: 8 additions & 1 deletion provider/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,15 @@ func (h *HTTPProvider) Call(request motan.Request) motan.Response {
k = strings.Replace(k, "M_", "MOTAN-", -1)
req.Header.Add(k, v)
}
ip := request.GetAttachment(motan.HostKey)
var ip string
remoteIP,exist := req.GetAttachments()[motan.RemoteIPKey]
if(exist){
ip = remoteIP
}else{
ip = request.GetAttachment(motan.HostKey)
}
req.Header.Add("x-forwarded-for",ip)

httpResp, err := h.httpClient.Do(req)
if err != nil {
vlog.Errorf("new HTTP Provider Do HTTP Call err: %v", err)
Expand Down
7 changes: 2 additions & 5 deletions server/motanserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,8 @@ func (m *MotanServer) processReq(request *mpro.Message, conn net.Conn) {
serialization := m.extFactory.GetSerialization("", request.Header.GetSerialize())
req, err := mpro.ConvertToRequest(request, serialization)

_,exist := req.GetAttachments()[motan.HostKey]
if (!exist){
ip := getRemoteIP(conn.RemoteAddr().String())
req.SetAttachment(motan.HostKey, ip)
}
ip := getRemoteIP(conn.RemoteAddr().String())
req.SetAttachment(motan.HostKey, ip)

req.GetRPCContext(true).ExtFactory = m.extFactory
if err != nil {
Expand Down

0 comments on commit 4dfba63

Please sign in to comment.