eventbox

package
v1.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 16, 2021 License: Apache-2.0 Imports: 6 Imported by: 0

README

* 消息格式
#+BEGIN_SRC 
type LabelKey string

type Message struct {
	Sender  string                   `json:"sender"`
	Content interface{}              `json:"content"`
	Labels  map[LabelKey]interface{} `json:"labels"`
	Time    int64                    `json:"time,omitempty"` // UnixNano
}
#+END_SRC

** 发往目的地
对于发往不同目的地的消息,在 Labels 中指定
*** 钉钉 
#+BEGIN_SRC 
dest := []string{"<url-1>", "<url-2>"}
label = map[LabelKey]interface{}{"DINGDING": dest}
#+END_SRC

*** HTTP
#+BEGIN_SRC 
dest := []string{"<url-1>", "<url-2>"}
label =map[LabelKey]interface{}{"HTTP": dest}
#+END_SRC

以上 2 个 label 可以合在一起, 这样一条消息就会同时发送到 钉钉 和 http 回调



* 消息写入方式

** HTTP API
   - url 

   =http://diceeventbox.marathon.l4lb.thisdcos.directory:9528/api/dice/eventbox/message/create=
    
   - body

     type =Message= struct

   - example curl:
#+BEGIN_SRC 
curl -XPOST 'http://diceeventbox.marathon.l4lb.thisdcos.directory:9528/api/dice/eventbox/message/create' \
-H 'Accept: application/json' -H 'Content-Type: application/json' \
-d '{"sender":"curl", "content":[1,2,3], "labels":{"DINGDING":["https://oapi.dingtalk.com/robot/send?access_token=xxxxxxx"]}}'
#+END_SRC     

** 写入 ETCD
   
   
*** 使用eventbox封装的API函数
**** go Package
     =github.com/erda-project/erda/modules/eventbox/api=
**** usage
#+BEGIN_SRC 
     var notifier, err := api.New(sender, nil)
     //... error handle
     // send 
     notifier.Send(content, api.WithDest(map[string]interface{}{"HTTP": []string{"URL"}}))
     // send another one
     notifier.Send(content, api.WithDest(map[string]interface{}{"HTTP": []string{"URL"}}))
#+END_SRC

*** 直接写入 ETCD
**** etcd endpoints 
#+BEGIN_SRC 
http://diceetcd-clusteretcd0.marathon.l4lb.thisdcos.directory:2379,http://diceetcd-clusteretcd1.marathon.l4lb.thisdcos.directory:2379,http://diceetcd-clusteretcd2.marathon.l4lb.thisdcos.directory:2379
#+END_SRC
**** KEY
     =/eventbox/messages/<TIME-UNIXNANO>=
     - TIME-UNIXNANO:
       =time.Now().UnixNano()=
**** VALUE
     type =Message= struct

* 消息 Labels 列表
  - HTTP

    http 回调形式的消息
#+BEGIN_SRC 
HTTP:["http://xxx", "https://yyy"]
#+END_SRC    

  - HTTP-HEADERS

    额外自定义的http回调时候的headers
#+BEGIN_SRC
HTTP-HEADERS: {"KEY1":"V1", "K2":"V2"}
#+END_SRC

  - WEBHOOK

    其他服务触发 webhook 时候所需带上的 label

    详细看下面 =Webhook= 部分的说明
#+BEGIN_SRC 
WEBHOOK: {
 "event":"runtime", 
 "action": "create",
 "org": "1",
 "project": "2",
}

#+END_SRC

  - DINGDING

    发送 钉钉 形式的消息
#+BEGIN_SRC 
DINGDING: ["https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxx"]
#+END_SRC    

  - MARKDOWN
    钉钉消息使用markdown格式
#+BEGIN_SRC 
MARKDOWN: {"title": "xxx"}
#+END_SRC
    
  - AT

    发送 钉钉 消息时候 at 人
#+BEGIN_SRC 
AT: {
  "atMobiles": [
    "1825718XXXX"
  ],
  "isAtAll": false
}
#+END_SRC    
* Webhook
  由 dice 组件触发各类事件,发送消息至 eventbox , 然后 eventbox 依据实现注册的 webhook ,发送事件至 webhook 中记录的 URL

** 其他服务触发webhook方式
   同样使用上述 =消息写入方式= 中描述的2种方式(http 或 写etcd)

   不同的是: 在消息的 =Labels= 中增加 key 为 =WEBHOOK= , value 为
#+BEGIN_SRC 
type EventLabel struct {
	Event  string `json:"event"`
	Action string `json:"action"`
	Org    string `json:"orgID"`
	// maybe not exist, then set it to 'default'
	Project string `json:"projectID"`
}

e.g. 
{
 "event":"runtime", 
 "action": "create",
 "orgID": "1",
 "projectID": "2",
}
#+END_SRC

   最终, 发送至 webhook 中 URL 的实际json为:
#+BEGIN_SRC 
type EventMessage struct {
	Event     string `json:"event"`
	Action    string `json:"action"`
	OrgID     string `json:"orgID"`
	ProjectID string `json:"projectID"`
	// content 结构跟具体 event 相关
	Content   json.RawMessage `json:"content"`
	TimeStamp string          `json:"timestamp"`
}

e.g.
{
 "event": "runtime",
 "action": "create",
 "orgID": "1",
 "projectID": "2",
 "content": "这里的内容为其他服务所发送的消息内容",
 "timestamp": "2006-1-2 12:23:34"
}
#+END_SRC







Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Initialize

func Initialize() error

Types

This section is empty.

Directories

Path Synopsis
对外提供 API 用于注册特定 label 对 其他 label 的关系 暂定这个特定 label 的 key 为 "REGISTERED_LABEL".(constant.RegisterLabelKey) e.g.
对外提供 API 用于注册特定 label 对 其他 label 的关系 暂定这个特定 label 的 key 为 "REGISTERED_LABEL".(constant.RegisterLabelKey) e.g.
label
default register labels here
default register labels here
TODO: refactor this module when pkg/httpserver ready
TODO: refactor this module when pkg/httpserver ready
sms
vms

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL