Skip to content

Commit 031c50e

Browse files
committed
apache#79 消息轨迹推送跟踪
1 parent 9d4a4e0 commit 031c50e

File tree

7 files changed

+43
-35
lines changed

7 files changed

+43
-35
lines changed

Diff for: deployments/db.js

+14-15
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,15 @@ db.createCollection( "polling_detail", {
8383
max: 100,
8484
validator: { $jsonSchema: {
8585
bsonType: "object",
86-
required: [ "id","session_id","domain","url_path" ],
86+
required: [ "id","session_id","revision","url_path" ],
8787
properties: {
8888
id: {
8989
bsonType: "string",
9090
},
9191
domain: {
9292
bsonType: "string",
9393
},
94-
params: {
94+
polling_data: {
9595
bsonType: "object"
9696
},
9797
ip: {
@@ -103,8 +103,8 @@ db.createCollection( "polling_detail", {
103103
response_body: {
104104
bsonType: "object"
105105
},
106-
response_header: {
107-
bsonType: "object"
106+
revision: {
107+
bsonType: "string"
108108
},
109109
response_code: {
110110
bsonType: "number"
@@ -114,15 +114,14 @@ db.createCollection( "polling_detail", {
114114
} );
115115

116116
//index
117-
db.kv.createIndex({"id": 1}, { unique: true } );
118-
db.kv.createIndex({key: 1, label_format: 1,domain:1,project:1},{ unique: true });
119-
db.kv_revision.createIndex( { "delete_time": 1 }, { expireAfterSeconds: 7 * 24 * 3600 } );
120-
db.label.createIndex({"id": 1}, { unique: true } );
121-
db.label.createIndex({format: 1,domain:1,project:1},{ unique: true });
122-
db.polling_detail.createIndex({"id": 1}, { unique: true } );
123-
db.polling_detail.createIndex({session_id:1,domain:1}, { unique: true } );
124-
db.counter.createIndex({name: 1,domain:1},{ unique: true });
125-
db.view.createIndex({"id": 1}, { unique: true } );
126-
db.view.createIndex({display:1,domain:1,project:1},{ unique: true });
117+
db.kv.createIndex({"id": 1}, {unique: true});
118+
db.kv.createIndex({key: 1, label_format: 1, domain: 1, project: 1}, {unique: true});
119+
db.kv_revision.createIndex({"delete_time": 1}, {expireAfterSeconds: 7 * 24 * 3600});
120+
db.label.createIndex({"id": 1}, {unique: true});
121+
db.label.createIndex({format: 1, domain: 1, project: 1}, {unique: true});
122+
db.polling_detail.createIndex({timestamp: 1}, {expireAfterSeconds: 7 * 24 * 3600});
123+
db.counter.createIndex({name: 1, domain: 1}, {unique: true});
124+
db.view.createIndex({"id": 1}, {unique: true});
125+
db.view.createIndex({display: 1, domain: 1, project: 1}, {unique: true});
127126
//db config
128-
db.setProfilingLevel(1, {slowms: 80, sampleRate: 1} );
127+
db.setProfilingLevel(1, {slowms: 80, sampleRate: 1});

Diff for: go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ github.com/go-chassis/foundation v0.1.1-0.20191113114104-2b05871e9ec4 h1:wx8JXvg
8787
github.com/go-chassis/foundation v0.1.1-0.20191113114104-2b05871e9ec4/go.mod h1:21/ajGtgJlWTCeM0TxGJdRhO8bJkKirWyV8Stlh6g6c=
8888
github.com/go-chassis/foundation v0.1.1-0.20200825060850-b16bf420f7b3 h1:c+bwT0qLY69jSU8TmzuNcb9UL/QFAiU96kjuX5TMiQc=
8989
github.com/go-chassis/foundation v0.1.1-0.20200825060850-b16bf420f7b3/go.mod h1:21/ajGtgJlWTCeM0TxGJdRhO8bJkKirWyV8Stlh6g6c=
90+
github.com/go-chassis/foundation v0.2.2-0.20201208060254-d5e8e5beb1f0 h1:6oazFyZKryG3r7K20UuWx+bclHMFGXjKBSMJpCefmMI=
9091
github.com/go-chassis/foundation v0.2.2-0.20201208060254-d5e8e5beb1f0/go.mod h1:2PjwqpVwYEVaAldl5A58a08viH8p27pNeYaiE3ZxOBA=
9192
github.com/go-chassis/go-archaius v1.2.1-0.20200309104817-8c3d4e87d33c h1:pimEM4Oy/Uf4xG4G7TrRUQbIRFAfHiarxDQQS2gmKaM=
9293
github.com/go-chassis/go-archaius v1.2.1-0.20200309104817-8c3d4e87d33c/go.mod h1:gVP52u/jCU0fgUjXdUW1VLp5YLLJ+Yl2zoOPrLM/WOM=
@@ -134,6 +135,7 @@ github.com/go-chassis/go-chassis/v2 v2.0.2 h1:FMiZgAa6IjDobtZN5oql+eSgMIlDq5+12o
134135
github.com/go-chassis/go-chassis/v2 v2.0.2/go.mod h1:xgXXrRUgn5sybtecTDn60ypepHKIV9QpSqR3CIEBNt0=
135136
github.com/go-chassis/go-chassis/v2 v2.1.0 h1:NnVUBj3U/pPijpZGooH+3QIRlOB2nAuZtx9b71aiaig=
136137
github.com/go-chassis/go-chassis/v2 v2.1.0/go.mod h1:DHA83QKVmuOtdkaWCP5yCDOh+GIRH+YpX3uZ2BlGMWw=
138+
github.com/go-chassis/go-chassis/v2 v2.1.1-0.20201208062518-9c2e86bd7a6c h1:k+D2w1oZ/fQjingJc1KzuwGv0cJKfaGDgaOW/4T4Q1c=
137139
github.com/go-chassis/go-chassis/v2 v2.1.1-0.20201208062518-9c2e86bd7a6c/go.mod h1:C5j2kuBAdch4DsSjKQxiYJr3Xy0PoZu2Bxx9QIJIArI=
138140
github.com/go-chassis/go-restful-swagger20 v1.0.3-0.20200310030431-17d80f34264f h1:5QmmNpVcGqIc6tuKNe5EAI4PA8Yn2EL9Oee7YdcJ4PE=
139141
github.com/go-chassis/go-restful-swagger20 v1.0.3-0.20200310030431-17d80f34264f/go.mod h1:eW62fYuzlNFDvIacB6AV8bhUDCTy4myfTCv0bT9Gbb0=

Diff for: pkg/model/db_schema.go

+14-10
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
package model
1919

20+
import "time"
21+
2022
//LabelDoc is database struct to store labels
2123
type LabelDoc struct {
2224
ID string `json:"id,omitempty" bson:"id,omitempty" yaml:"id,omitempty" swag:"string"`
@@ -57,16 +59,18 @@ type ViewDoc struct {
5759

5860
//PollingDetail is db struct, it record operation history
5961
type PollingDetail struct {
60-
ID string `json:"id,omitempty" yaml:"id,omitempty"`
61-
SessionID string `json:"session_id,omitempty" bson:"session_id," yaml:"session_id,omitempty"`
62-
Domain string `json:"domain,omitempty" yaml:"domain,omitempty"`
63-
PollingData map[string]interface{} `json:"polling_data,omitempty" yaml:"polling_data,omitempty"`
64-
IP string `json:"ip,omitempty" yaml:"ip,omitempty"`
65-
UserAgent string `json:"user_agent,omitempty" bson:"user_agent," yaml:"user_agent,omitempty"`
66-
URLPath string `json:"url_path,omitempty" bson:"url_path," yaml:"url_path,omitempty"`
67-
ResponseBody []*KVDoc `json:"kv,omitempty" bson:"kv," yaml:"kv,omitempty"`
68-
ResponseHeader map[string][]string `json:"response_header,omitempty" bson:"response_header," yaml:"response_header,omitempty"`
69-
ResponseCode int `json:"response_code,omitempty" bson:"response_code," yaml:"response_code,omitempty"`
62+
ID string `json:"id,omitempty" yaml:"id,omitempty"`
63+
SessionID string `json:"session_id,omitempty" bson:"session_id," yaml:"session_id,omitempty"`
64+
SessionGroup string `json:"session_group,omitempty" bson:"session_group," yaml:"session_group,omitempty"`
65+
Domain string `json:"domain,omitempty" yaml:"domain,omitempty"`
66+
PollingData map[string]interface{} `json:"polling_data,omitempty" yaml:"polling_data,omitempty"`
67+
Revision string `json:"revision,omitempty" yaml:"revision,omitempty"`
68+
IP string `json:"ip,omitempty" yaml:"ip,omitempty"`
69+
UserAgent string `json:"user_agent,omitempty" bson:"user_agent," yaml:"user_agent,omitempty"`
70+
URLPath string `json:"url_path,omitempty" bson:"url_path," yaml:"url_path,omitempty"`
71+
ResponseBody []*KVDoc `json:"kv,omitempty" bson:"kv," yaml:"kv,omitempty"`
72+
ResponseCode int `json:"response_code,omitempty" bson:"response_code," yaml:"response_code,omitempty"`
73+
Timestamp time.Time `json:"timestamp,omitempty" yaml:"timestamp,omitempty"`
7074
}
7175

7276
// UpdateKVRequest is db struct, it contains kv update request params

Diff for: server/handler/track_handler.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"github.com/go-chassis/openlog"
3030
"net/http"
3131
"strings"
32+
"time"
3233
)
3334

3435
//const of noop auth handler
@@ -66,13 +67,15 @@ func (h *TrackHandler) Handle(chain *handler.Chain, inv *invocation.Invocation,
6667
data := &model.PollingDetail{}
6768
data.URLPath = req.Request.Method + " " + req.Request.URL.Path
6869
data.SessionID = sessionID
70+
data.SessionGroup = req.HeaderParameter(v1.HeaderSessionGroup)
6971
data.UserAgent = req.HeaderParameter(v1.HeaderUserAgent)
7072
data.Domain = v1.ReadDomain(req.Request.Context())
7173
data.IP = iputil.ClientIP(req.Request)
7274
data.ResponseBody = req.Attribute(common.RespBodyContextKey).([]*model.KVDoc)
7375
data.ResponseCode = ir.Status
76+
data.Timestamp = time.Now()
7477
if resp != nil {
75-
data.ResponseHeader = resp.Header()
78+
data.Revision = resp.Header().Get(common.HeaderRevision)
7679
}
7780
data.PollingData = map[string]interface{}{
7881
"revision": revStr,

Diff for: server/resource/v1/common.go

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import (
4343
const (
4444
HeaderUserAgent = "User-Agent"
4545
HeaderSessionID = "X-Session-Id"
46+
HeaderSessionGroup = "X-Session-Group"
4647
AttributeDomainKey = "domain"
4748

4849
FmtReadRequestError = "decode request body failed: %v"

Diff for: server/service/mongo/session/session.go

+3-8
Original file line numberDiff line numberDiff line change
@@ -222,19 +222,14 @@ func ensureKVLongPolling(session *mgo.Session) {
222222
c := session.DB(DBName).C(CollectionPollingDetail)
223223
err := c.Create(&mgo.CollectionInfo{Validator: bson.M{
224224
"id": bson.M{"$exists": true},
225-
"params": bson.M{"$exists": true},
225+
"revision": bson.M{"$exists": true},
226226
"session_id": bson.M{"$exists": true},
227227
"url_path": bson.M{"$exists": true},
228228
}})
229229
wrapError(err, MsgDBExists)
230230
err = c.EnsureIndex(mgo.Index{
231-
Key: []string{"id"},
232-
Unique: true,
233-
})
234-
wrapError(err)
235-
err = c.EnsureIndex(mgo.Index{
236-
Key: []string{"session_id", "domain"},
237-
Unique: true,
231+
Key: []string{"timestamp"},
232+
ExpireAfter: 7 * 24 * time.Hour,
238233
})
239234
wrapError(err)
240235
}

Diff for: server/service/mongo/track/polling_detail_dao.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ import (
2929
)
3030

3131
//CreateOrUpdate create a record or update exist record
32+
//If revision and session_id is exist: update else:insert
3233
func CreateOrUpdate(ctx context.Context, detail *model.PollingDetail) (*model.PollingDetail, error) {
3334
collection := session.GetDB().Collection(session.CollectionPollingDetail)
34-
queryFilter := bson.M{"domain": detail.Domain, "session_id": detail.SessionID}
35+
queryFilter := bson.M{"revision": detail.Domain, "session_id": detail.SessionID}
3536
res := collection.FindOne(ctx, queryFilter)
3637
if res.Err() != nil {
3738
if res.Err() == mongo.ErrNoDocuments {
@@ -67,6 +68,9 @@ func Get(ctx context.Context, detail *model.PollingDetail) ([]*model.PollingDeta
6768
if detail.URLPath != "" {
6869
queryFilter["url_path"] = detail.URLPath
6970
}
71+
if detail.Revision != "" {
72+
queryFilter["revision"] = detail.Revision
73+
}
7074
cur, err := collection.Find(ctx, queryFilter)
7175
if err != nil {
7276
return nil, err

0 commit comments

Comments
 (0)