Skip to content

Commit

Permalink
apache#79 消息轨迹推送跟踪
Browse files Browse the repository at this point in the history
  • Loading branch information
develpoerX committed Dec 26, 2020
1 parent 9d4a4e0 commit 031c50e
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 35 deletions.
29 changes: 14 additions & 15 deletions deployments/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,15 @@ db.createCollection( "polling_detail", {
max: 100,
validator: { $jsonSchema: {
bsonType: "object",
required: [ "id","session_id","domain","url_path" ],
required: [ "id","session_id","revision","url_path" ],
properties: {
id: {
bsonType: "string",
},
domain: {
bsonType: "string",
},
params: {
polling_data: {
bsonType: "object"
},
ip: {
Expand All @@ -103,8 +103,8 @@ db.createCollection( "polling_detail", {
response_body: {
bsonType: "object"
},
response_header: {
bsonType: "object"
revision: {
bsonType: "string"
},
response_code: {
bsonType: "number"
Expand All @@ -114,15 +114,14 @@ db.createCollection( "polling_detail", {
} );

//index
db.kv.createIndex({"id": 1}, { unique: true } );
db.kv.createIndex({key: 1, label_format: 1,domain:1,project:1},{ unique: true });
db.kv_revision.createIndex( { "delete_time": 1 }, { expireAfterSeconds: 7 * 24 * 3600 } );
db.label.createIndex({"id": 1}, { unique: true } );
db.label.createIndex({format: 1,domain:1,project:1},{ unique: true });
db.polling_detail.createIndex({"id": 1}, { unique: true } );
db.polling_detail.createIndex({session_id:1,domain:1}, { unique: true } );
db.counter.createIndex({name: 1,domain:1},{ unique: true });
db.view.createIndex({"id": 1}, { unique: true } );
db.view.createIndex({display:1,domain:1,project:1},{ unique: true });
db.kv.createIndex({"id": 1}, {unique: true});
db.kv.createIndex({key: 1, label_format: 1, domain: 1, project: 1}, {unique: true});
db.kv_revision.createIndex({"delete_time": 1}, {expireAfterSeconds: 7 * 24 * 3600});
db.label.createIndex({"id": 1}, {unique: true});
db.label.createIndex({format: 1, domain: 1, project: 1}, {unique: true});
db.polling_detail.createIndex({timestamp: 1}, {expireAfterSeconds: 7 * 24 * 3600});
db.counter.createIndex({name: 1, domain: 1}, {unique: true});
db.view.createIndex({"id": 1}, {unique: true});
db.view.createIndex({display: 1, domain: 1, project: 1}, {unique: true});
//db config
db.setProfilingLevel(1, {slowms: 80, sampleRate: 1} );
db.setProfilingLevel(1, {slowms: 80, sampleRate: 1});
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ github.com/go-chassis/foundation v0.1.1-0.20191113114104-2b05871e9ec4 h1:wx8JXvg
github.com/go-chassis/foundation v0.1.1-0.20191113114104-2b05871e9ec4/go.mod h1:21/ajGtgJlWTCeM0TxGJdRhO8bJkKirWyV8Stlh6g6c=
github.com/go-chassis/foundation v0.1.1-0.20200825060850-b16bf420f7b3 h1:c+bwT0qLY69jSU8TmzuNcb9UL/QFAiU96kjuX5TMiQc=
github.com/go-chassis/foundation v0.1.1-0.20200825060850-b16bf420f7b3/go.mod h1:21/ajGtgJlWTCeM0TxGJdRhO8bJkKirWyV8Stlh6g6c=
github.com/go-chassis/foundation v0.2.2-0.20201208060254-d5e8e5beb1f0 h1:6oazFyZKryG3r7K20UuWx+bclHMFGXjKBSMJpCefmMI=
github.com/go-chassis/foundation v0.2.2-0.20201208060254-d5e8e5beb1f0/go.mod h1:2PjwqpVwYEVaAldl5A58a08viH8p27pNeYaiE3ZxOBA=
github.com/go-chassis/go-archaius v1.2.1-0.20200309104817-8c3d4e87d33c h1:pimEM4Oy/Uf4xG4G7TrRUQbIRFAfHiarxDQQS2gmKaM=
github.com/go-chassis/go-archaius v1.2.1-0.20200309104817-8c3d4e87d33c/go.mod h1:gVP52u/jCU0fgUjXdUW1VLp5YLLJ+Yl2zoOPrLM/WOM=
Expand Down Expand Up @@ -134,6 +135,7 @@ github.com/go-chassis/go-chassis/v2 v2.0.2 h1:FMiZgAa6IjDobtZN5oql+eSgMIlDq5+12o
github.com/go-chassis/go-chassis/v2 v2.0.2/go.mod h1:xgXXrRUgn5sybtecTDn60ypepHKIV9QpSqR3CIEBNt0=
github.com/go-chassis/go-chassis/v2 v2.1.0 h1:NnVUBj3U/pPijpZGooH+3QIRlOB2nAuZtx9b71aiaig=
github.com/go-chassis/go-chassis/v2 v2.1.0/go.mod h1:DHA83QKVmuOtdkaWCP5yCDOh+GIRH+YpX3uZ2BlGMWw=
github.com/go-chassis/go-chassis/v2 v2.1.1-0.20201208062518-9c2e86bd7a6c h1:k+D2w1oZ/fQjingJc1KzuwGv0cJKfaGDgaOW/4T4Q1c=
github.com/go-chassis/go-chassis/v2 v2.1.1-0.20201208062518-9c2e86bd7a6c/go.mod h1:C5j2kuBAdch4DsSjKQxiYJr3Xy0PoZu2Bxx9QIJIArI=
github.com/go-chassis/go-restful-swagger20 v1.0.3-0.20200310030431-17d80f34264f h1:5QmmNpVcGqIc6tuKNe5EAI4PA8Yn2EL9Oee7YdcJ4PE=
github.com/go-chassis/go-restful-swagger20 v1.0.3-0.20200310030431-17d80f34264f/go.mod h1:eW62fYuzlNFDvIacB6AV8bhUDCTy4myfTCv0bT9Gbb0=
Expand Down
24 changes: 14 additions & 10 deletions pkg/model/db_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package model

import "time"

//LabelDoc is database struct to store labels
type LabelDoc struct {
ID string `json:"id,omitempty" bson:"id,omitempty" yaml:"id,omitempty" swag:"string"`
Expand Down Expand Up @@ -57,16 +59,18 @@ type ViewDoc struct {

//PollingDetail is db struct, it record operation history
type PollingDetail struct {
ID string `json:"id,omitempty" yaml:"id,omitempty"`
SessionID string `json:"session_id,omitempty" bson:"session_id," yaml:"session_id,omitempty"`
Domain string `json:"domain,omitempty" yaml:"domain,omitempty"`
PollingData map[string]interface{} `json:"polling_data,omitempty" yaml:"polling_data,omitempty"`
IP string `json:"ip,omitempty" yaml:"ip,omitempty"`
UserAgent string `json:"user_agent,omitempty" bson:"user_agent," yaml:"user_agent,omitempty"`
URLPath string `json:"url_path,omitempty" bson:"url_path," yaml:"url_path,omitempty"`
ResponseBody []*KVDoc `json:"kv,omitempty" bson:"kv," yaml:"kv,omitempty"`
ResponseHeader map[string][]string `json:"response_header,omitempty" bson:"response_header," yaml:"response_header,omitempty"`
ResponseCode int `json:"response_code,omitempty" bson:"response_code," yaml:"response_code,omitempty"`
ID string `json:"id,omitempty" yaml:"id,omitempty"`
SessionID string `json:"session_id,omitempty" bson:"session_id," yaml:"session_id,omitempty"`
SessionGroup string `json:"session_group,omitempty" bson:"session_group," yaml:"session_group,omitempty"`
Domain string `json:"domain,omitempty" yaml:"domain,omitempty"`
PollingData map[string]interface{} `json:"polling_data,omitempty" yaml:"polling_data,omitempty"`
Revision string `json:"revision,omitempty" yaml:"revision,omitempty"`
IP string `json:"ip,omitempty" yaml:"ip,omitempty"`
UserAgent string `json:"user_agent,omitempty" bson:"user_agent," yaml:"user_agent,omitempty"`
URLPath string `json:"url_path,omitempty" bson:"url_path," yaml:"url_path,omitempty"`
ResponseBody []*KVDoc `json:"kv,omitempty" bson:"kv," yaml:"kv,omitempty"`
ResponseCode int `json:"response_code,omitempty" bson:"response_code," yaml:"response_code,omitempty"`
Timestamp time.Time `json:"timestamp,omitempty" yaml:"timestamp,omitempty"`
}

// UpdateKVRequest is db struct, it contains kv update request params
Expand Down
5 changes: 4 additions & 1 deletion server/handler/track_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/go-chassis/openlog"
"net/http"
"strings"
"time"
)

//const of noop auth handler
Expand Down Expand Up @@ -66,13 +67,15 @@ func (h *TrackHandler) Handle(chain *handler.Chain, inv *invocation.Invocation,
data := &model.PollingDetail{}
data.URLPath = req.Request.Method + " " + req.Request.URL.Path
data.SessionID = sessionID
data.SessionGroup = req.HeaderParameter(v1.HeaderSessionGroup)
data.UserAgent = req.HeaderParameter(v1.HeaderUserAgent)
data.Domain = v1.ReadDomain(req.Request.Context())
data.IP = iputil.ClientIP(req.Request)
data.ResponseBody = req.Attribute(common.RespBodyContextKey).([]*model.KVDoc)
data.ResponseCode = ir.Status
data.Timestamp = time.Now()
if resp != nil {
data.ResponseHeader = resp.Header()
data.Revision = resp.Header().Get(common.HeaderRevision)
}
data.PollingData = map[string]interface{}{
"revision": revStr,
Expand Down
1 change: 1 addition & 0 deletions server/resource/v1/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import (
const (
HeaderUserAgent = "User-Agent"
HeaderSessionID = "X-Session-Id"
HeaderSessionGroup = "X-Session-Group"
AttributeDomainKey = "domain"

FmtReadRequestError = "decode request body failed: %v"
Expand Down
11 changes: 3 additions & 8 deletions server/service/mongo/session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,19 +222,14 @@ func ensureKVLongPolling(session *mgo.Session) {
c := session.DB(DBName).C(CollectionPollingDetail)
err := c.Create(&mgo.CollectionInfo{Validator: bson.M{
"id": bson.M{"$exists": true},
"params": bson.M{"$exists": true},
"revision": bson.M{"$exists": true},
"session_id": bson.M{"$exists": true},
"url_path": bson.M{"$exists": true},
}})
wrapError(err, MsgDBExists)
err = c.EnsureIndex(mgo.Index{
Key: []string{"id"},
Unique: true,
})
wrapError(err)
err = c.EnsureIndex(mgo.Index{
Key: []string{"session_id", "domain"},
Unique: true,
Key: []string{"timestamp"},
ExpireAfter: 7 * 24 * time.Hour,
})
wrapError(err)
}
Expand Down
6 changes: 5 additions & 1 deletion server/service/mongo/track/polling_detail_dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ import (
)

//CreateOrUpdate create a record or update exist record
//If revision and session_id is exist: update else:insert
func CreateOrUpdate(ctx context.Context, detail *model.PollingDetail) (*model.PollingDetail, error) {
collection := session.GetDB().Collection(session.CollectionPollingDetail)
queryFilter := bson.M{"domain": detail.Domain, "session_id": detail.SessionID}
queryFilter := bson.M{"revision": detail.Domain, "session_id": detail.SessionID}
res := collection.FindOne(ctx, queryFilter)
if res.Err() != nil {
if res.Err() == mongo.ErrNoDocuments {
Expand Down Expand Up @@ -67,6 +68,9 @@ func Get(ctx context.Context, detail *model.PollingDetail) ([]*model.PollingDeta
if detail.URLPath != "" {
queryFilter["url_path"] = detail.URLPath
}
if detail.Revision != "" {
queryFilter["revision"] = detail.Revision
}
cur, err := collection.Find(ctx, queryFilter)
if err != nil {
return nil, err
Expand Down

0 comments on commit 031c50e

Please sign in to comment.