本文整理匯總了Golang中github.com/mozilla-services/heka/message.Message類的典型用法代碼示例。如果您正苦於以下問題:Golang Message類的具體用法?Golang Message怎麽用?Golang Message使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Message類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: convertMessageToValues
// convertMessageToValue reads a Heka Message and returns a slice of field values
func (po *PostgresOutput) convertMessageToValues(m *message.Message, insertFields []string) (fieldValues []interface{}, err error) {
fieldValues = []interface{}{}
missingFields := []string{}
for _, field := range insertFields {
// Special case: get "Timestamp" from Heka message
if field == "Timestamp" {
// Convert Heka time (Unix timestamp in nanoseconds) to Golang time
v := time.Unix(0, m.GetTimestamp())
fieldValues = append(fieldValues, v)
} else {
v, ok := m.GetFieldValue(field)
if !ok {
// If configured to do so, write NULL when a FieldValue isn't found in the Heka message
if po.allowMissingMessageFields {
v = nil
} else {
missingFields = append(missingFields, field)
continue
}
}
fieldValues = append(fieldValues, v)
}
}
if len(missingFields) > 0 {
return []interface{}{}, fmt.Errorf("message is missing expected fields: %s", strings.Join(missingFields, ", "))
}
return fieldValues, nil
}
示例2: newInt64Field
// Convenience function for creating a new int64 field on a message object.
func newInt64Field(msg *message.Message, name string, val int64, representation string) {
if f, err := message.NewField(name, val, representation); err == nil {
msg.AddField(f)
} else {
fmt.Println("Report error adding int64 field: ", err)
}
}
示例3: PopulateReportMsg
// Given a PluginRunner and a Message struct, this function will populate the
// Message struct's field values with the plugin's input channel length and
// capacity, plus any additional data that the plugin might provide through
// implementation of the `ReportingPlugin` interface defined above.
func PopulateReportMsg(pr PluginRunner, msg *message.Message) (err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("'%s' `populateReportMsg` panic: %s", pr.Name(), r)
}
}()
if reporter, ok := pr.Plugin().(ReportingPlugin); ok {
if err = reporter.ReportMsg(msg); err != nil {
return
}
}
if fRunner, ok := pr.(FilterRunner); ok {
newIntField(msg, "InChanCapacity", cap(fRunner.InChan()), "count")
newIntField(msg, "InChanLength", len(fRunner.InChan()), "count")
newIntField(msg, "MatchChanCapacity", cap(fRunner.MatchRunner().inChan), "count")
newIntField(msg, "MatchChanLength", len(fRunner.MatchRunner().inChan), "count")
var tmp int64 = 0
fRunner.MatchRunner().reportLock.Lock()
if fRunner.MatchRunner().matchSamples > 0 {
tmp = fRunner.MatchRunner().matchDuration / fRunner.MatchRunner().matchSamples
}
fRunner.MatchRunner().reportLock.Unlock()
newInt64Field(msg, "MatchAvgDuration", tmp, "ns")
} else if dRunner, ok := pr.(DecoderRunner); ok {
newIntField(msg, "InChanCapacity", cap(dRunner.InChan()), "count")
newIntField(msg, "InChanLength", len(dRunner.InChan()), "count")
}
msg.SetType("heka.plugin-report")
return
}
示例4: newStringField
// Convenience function for creating and setting a string field called "name"
// on a message object.
func newStringField(msg *message.Message, name string, val string) {
if f, err := message.NewField(name, val, ""); err == nil {
msg.AddField(f)
} else {
fmt.Println("Report error adding string field: ", err)
}
}
示例5: PopulateReportMsg
// Given a PluginRunner and a Message struct, this function will populate the
// Message struct's field values with the plugin's input channel length and
// capacity, plus any additional data that the plugin might provide through
// implementation of the `ReportingPlugin` interface defined above.
func PopulateReportMsg(pr PluginRunner, msg *message.Message) (err error) {
if reporter, ok := pr.Plugin().(ReportingPlugin); ok {
if err = reporter.ReportMsg(msg); err != nil {
return
}
}
if fRunner, ok := pr.(FilterRunner); ok {
message.NewIntField(msg, "InChanCapacity", cap(fRunner.InChan()), "count")
message.NewIntField(msg, "InChanLength", len(fRunner.InChan()), "count")
message.NewIntField(msg, "MatchChanCapacity", cap(fRunner.MatchRunner().inChan), "count")
message.NewIntField(msg, "MatchChanLength", len(fRunner.MatchRunner().inChan), "count")
message.NewIntField(msg, "LeakCount", fRunner.LeakCount(), "count")
var tmp int64 = 0
fRunner.MatchRunner().reportLock.Lock()
if fRunner.MatchRunner().matchSamples > 0 {
tmp = fRunner.MatchRunner().matchDuration / fRunner.MatchRunner().matchSamples
}
fRunner.MatchRunner().reportLock.Unlock()
message.NewInt64Field(msg, "MatchAvgDuration", tmp, "ns")
} else if dRunner, ok := pr.(DecoderRunner); ok {
message.NewIntField(msg, "InChanCapacity", cap(dRunner.InChan()), "count")
message.NewIntField(msg, "InChanLength", len(dRunner.InChan()), "count")
}
msg.SetType("heka.plugin-report")
return
}
示例6: PopulateReportMsg
// Given a PluginRunner and a Message struct, this function will populate the
// Message struct's field values with the plugin's input channel length and
// capacity, plus any additional data that the plugin might provide through
// implementation of the `ReportingPlugin` interface defined above.
func PopulateReportMsg(pr PluginRunner, msg *message.Message) (err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("'%s' `populateReportMsg` panic: %s", pr.Name(), r)
}
}()
if reporter, ok := pr.Plugin().(ReportingPlugin); ok {
if err = reporter.ReportMsg(msg); err != nil {
return
}
}
if fRunner, ok := pr.(FilterRunner); ok {
newIntField(msg, "InChanCapacity", cap(fRunner.InChan()))
newIntField(msg, "InChanLength", len(fRunner.InChan()))
newIntField(msg, "MatchChanCapacity", cap(fRunner.MatchRunner().inChan))
newIntField(msg, "MatchChanLength", len(fRunner.MatchRunner().inChan))
} else if dRunner, ok := pr.(DecoderRunner); ok {
newIntField(msg, "InChanCapacity", cap(dRunner.InChan()))
newIntField(msg, "InChanLength", len(dRunner.InChan()))
}
msg.SetType("heka.plugin-report")
return
}
示例7: getField
func getField(msg *message.Message, name string) interface{} {
switch name {
case "Uuid":
if msg.Uuid == nil {
return nil
}
return msg.GetUuidString()
case "Timestamp":
return msg.Timestamp
case "Type":
return msg.Type
case "Logger":
return msg.Logger
case "Severity":
return msg.Severity
case "Payload":
return msg.Payload
case "EnvVersion":
return msg.EnvVersion
case "Pid":
return msg.Pid
case "Hostname":
return msg.Hostname
case "_hekaTimestampMicro":
if msg.Timestamp != nil {
return *msg.Timestamp / 1000 // nano -> micro
}
return nil
default:
val, _ := msg.GetFieldValue(name)
return val
}
}
示例8: WriteToBuffer
func (so *S3Output) WriteToBuffer(buffer *bytes.Buffer, msg *message.Message, or OutputRunner) (err error) {
_, err = buffer.Write([]byte(msg.GetPayload()))
if err != nil {
return
}
if buffer.Len() > so.config.BufferChunkLimit {
err = so.SaveToDisk(buffer, or)
}
return
}
示例9: Run
func (cwo *CloudwatchOutput) Run(or pipeline.OutputRunner, h pipeline.PluginHelper) (err error) {
inChan := or.InChan()
payloads := make(chan CloudwatchDatapoints, cwo.backlog)
go cwo.Submitter(payloads, or)
var (
pack *pipeline.PipelinePack
msg *message.Message
rawDataPoints *CloudwatchDatapointPayload
dataPoints *CloudwatchDatapoints
)
dataPoints = new(CloudwatchDatapoints)
dataPoints.Datapoints = make([]cloudwatch.MetricDatum, 0, 0)
for pack = range inChan {
rawDataPoints = new(CloudwatchDatapointPayload)
msg = pack.Message
err = json.Unmarshal([]byte(msg.GetPayload()), rawDataPoints)
if err != nil {
or.LogMessage(fmt.Sprintf("warning, unable to parse payload: %s", err))
err = nil
continue
}
// Run through the list and convert them to CloudwatchDatapoints
for _, rawDatum := range rawDataPoints.Datapoints {
datum := cloudwatch.MetricDatum{
Dimensions: rawDatum.Dimensions,
MetricName: rawDatum.MetricName,
Unit: rawDatum.Unit,
Value: rawDatum.Value,
StatisticValues: rawDatum.StatisticValues,
}
if rawDatum.Timestamp != "" {
parsedTime, err := message.ForgivingTimeParse("", rawDatum.Timestamp, cwo.tzLocation)
if err != nil {
or.LogMessage(fmt.Sprintf("unable to parse timestamp for datum: %s", rawDatum))
continue
}
datum.Timestamp = parsedTime
}
dataPoints.Datapoints = append(dataPoints.Datapoints, datum)
}
payloads <- *dataPoints
dataPoints.Datapoints = dataPoints.Datapoints[:0]
rawDataPoints.Datapoints = rawDataPoints.Datapoints[:0]
pack.Recycle()
}
or.LogMessage("shutting down AWS Cloudwatch submitter")
cwo.stopChan <- true
<-cwo.stopChan
return
}
示例10: loadSandbox
// Parses a Heka message and extracts the information necessary to start a new
// SandboxFilter
func (this *SandboxManagerFilter) loadSandbox(fr pipeline.FilterRunner,
h pipeline.PluginHelper, dir string, msg *message.Message) (err error) {
fv, _ := msg.GetFieldValue("config")
if config, ok := fv.(string); ok {
var configFile pipeline.ConfigFile
if _, err = toml.Decode(config, &configFile); err != nil {
return fmt.Errorf("loadSandbox failed: %s\n", err)
} else {
for name, conf := range configFile {
name = getSandboxName(fr.Name(), name)
if _, ok := h.Filter(name); ok {
// todo support reload
return fmt.Errorf("loadSandbox failed: %s is already running", name)
}
fr.LogMessage(fmt.Sprintf("Loading: %s", name))
confFile := filepath.Join(dir, fmt.Sprintf("%s.toml", name))
err = ioutil.WriteFile(confFile, []byte(config), 0600)
if err != nil {
return
}
var sbc SandboxConfig
if err = toml.PrimitiveDecode(conf, &sbc); err != nil {
return fmt.Errorf("loadSandbox failed: %s\n", err)
}
scriptFile := filepath.Join(dir, fmt.Sprintf("%s.%s", name, sbc.ScriptType))
err = ioutil.WriteFile(scriptFile, []byte(msg.GetPayload()), 0600)
if err != nil {
removeAll(dir, fmt.Sprintf("%s.*", name))
return
}
// check/clear the old state preservation file
// this avoids issues with changes to the data model since the last load
// and prevents holes in the graph from looking like anomalies
os.Remove(filepath.Join(pipeline.PrependBaseDir(DATA_DIR), name+DATA_EXT))
var runner pipeline.FilterRunner
runner, err = this.createRunner(dir, name, conf)
if err != nil {
removeAll(dir, fmt.Sprintf("%s.*", name))
return
}
err = h.PipelineConfig().AddFilterRunner(runner)
if err == nil {
this.currentFilters++
}
break // only interested in the first item
}
}
}
return
}
示例11: Run
func (s *SmtpOutput) Run(or OutputRunner, h PluginHelper) (err error) {
inChan := or.InChan()
var (
pack *PipelinePack
msg *message.Message
contents []byte
subject string
message string
headerText string
)
if s.conf.Subject == "" {
subject = "Heka [" + or.Name() + "]"
} else {
subject = s.conf.Subject
}
header := make(map[string]string)
header["From"] = s.conf.SendFrom
header["Subject"] = subject
header["MIME-Version"] = "1.0"
header["Content-Type"] = "text/plain; charset=\"utf-8\""
header["Content-Transfer-Encoding"] = "base64"
for k, v := range header {
headerText += fmt.Sprintf("%s: %s\r\n", k, v)
}
for pack = range inChan {
msg = pack.Message
message = headerText
if s.conf.PayloadOnly {
message += "\r\n" + base64.StdEncoding.EncodeToString([]byte(msg.GetPayload()))
err = s.sendFunction(s.conf.Host, s.auth, s.conf.SendFrom, s.conf.SendTo, []byte(message))
} else {
if contents, err = json.Marshal(msg); err == nil {
message += "\r\n" + base64.StdEncoding.EncodeToString(contents)
err = s.sendFunction(s.conf.Host, s.auth, s.conf.SendFrom, s.conf.SendTo, []byte(message))
} else {
or.LogError(err)
}
}
if err != nil {
or.LogError(err)
}
pack.Recycle()
}
return
}
示例12: loadSandbox
// Parses a Heka message and extracts the information necessary to start a new
// SandboxFilter
func (this *SandboxManagerFilter) loadSandbox(fr pipeline.FilterRunner,
h pipeline.PluginHelper, dir string, msg *message.Message) (err error) {
fv, _ := msg.GetFieldValue("config")
if config, ok := fv.(string); ok {
var configFile pipeline.ConfigFile
if _, err = toml.Decode(config, &configFile); err != nil {
return fmt.Errorf("loadSandbox failed: %s\n", err)
}
for name, conf := range configFile {
name = getSandboxName(fr.Name(), name)
if _, ok := h.Filter(name); ok {
// todo support reload
return fmt.Errorf("loadSandbox failed: %s is already running", name)
}
fr.LogMessage(fmt.Sprintf("Loading: %s", name))
confFile := filepath.Join(dir, fmt.Sprintf("%s.toml", name))
err = ioutil.WriteFile(confFile, []byte(config), 0600)
if err != nil {
return
}
var sbc SandboxConfig
// Default, will get overwritten if necessary
sbc.ScriptType = "lua"
if err = toml.PrimitiveDecode(conf, &sbc); err != nil {
return fmt.Errorf("loadSandbox failed: %s\n", err)
}
scriptFile := filepath.Join(dir, fmt.Sprintf("%s.%s", name, sbc.ScriptType))
err = ioutil.WriteFile(scriptFile, []byte(msg.GetPayload()), 0600)
if err != nil {
removeAll(dir, fmt.Sprintf("%s.*", name))
return
}
var runner pipeline.FilterRunner
runner, err = this.createRunner(dir, name, conf)
if err != nil {
removeAll(dir, fmt.Sprintf("%s.*", name))
return
}
err = this.pConfig.AddFilterRunner(runner)
if err == nil {
atomic.AddInt32(&this.currentFilters, 1)
}
break // only interested in the first item
}
}
return
}
示例13: lookup_field
func lookup_field(msg *message.Message, fn string, fi int, ai int) (int,
unsafe.Pointer, int) {
var field *message.Field
if fi != 0 {
fields := msg.FindAllFields(fn)
if fi >= len(fields) {
return 0, unsafe.Pointer(nil), 0
}
field = fields[fi]
} else {
if field = msg.FindFirstField(fn); field == nil {
return 0, unsafe.Pointer(nil), 0
}
}
fieldType := int(field.GetValueType())
switch field.GetValueType() {
case message.Field_STRING:
if ai >= len(field.ValueString) {
return fieldType, unsafe.Pointer(nil), 0
}
value := field.ValueString[ai]
cs := C.CString(value) // freed by the caller
return fieldType, unsafe.Pointer(cs), len(value)
case message.Field_BYTES:
if ai >= len(field.ValueBytes) {
return fieldType, unsafe.Pointer(nil), 0
}
value := field.ValueBytes[ai]
return fieldType, unsafe.Pointer(&field.ValueBytes[ai][0]), len(value)
case message.Field_INTEGER:
if ai >= len(field.ValueInteger) {
return fieldType, unsafe.Pointer(nil), 0
}
return fieldType, unsafe.Pointer(&field.ValueInteger[ai]), 0
case message.Field_DOUBLE:
if ai >= len(field.ValueDouble) {
return fieldType, unsafe.Pointer(nil), 0
}
return fieldType, unsafe.Pointer(&field.ValueDouble[ai]), 0
case message.Field_BOOL:
if ai >= len(field.ValueBool) {
return fieldType, unsafe.Pointer(nil), 0
}
return fieldType, unsafe.Pointer(&field.ValueBool[ai]), 0
}
return 0, unsafe.Pointer(nil), 0
}
示例14: addFields
// Fields are additional logging data passed to Heka. They are technically
// undefined, but searchable and actionable.
func addFields(msg *message.Message, fields Fields) (err error) {
for key, ival := range fields {
var field *message.Field
if ival == "" {
ival = "*empty*"
}
if key == "" {
continue
}
field, err = message.NewField(key, ival, ival)
if err != nil {
return err
}
msg.AddField(field)
}
return err
}
示例15: loadSandbox
func (this *SandboxManagerFilter) loadSandbox(fr FilterRunner,
h PluginHelper, dir string, msg *message.Message) (err error) {
fv, _ := msg.GetFieldValue("config")
if config, ok := fv.(string); ok {
var configFile ConfigFile
if _, err = toml.Decode(config, &configFile); err != nil {
return fmt.Errorf("loadSandbox failed: %s\n", err)
} else {
for name, conf := range configFile {
name = getSandboxName(fr.Name(), name)
if _, ok := h.Filter(name); ok {
// todo support reload
return fmt.Errorf("loadSandbox failed: %s is already running", name)
}
fr.LogMessage(fmt.Sprintf("Loading: %s", name))
confFile := path.Join(dir, fmt.Sprintf("%s.toml", name))
err = ioutil.WriteFile(confFile, []byte(config), 0600)
if err != nil {
return
}
var sbfc SandboxFilterConfig
if err = toml.PrimitiveDecode(conf, &sbfc); err != nil {
return fmt.Errorf("loadSandbox failed: %s\n", err)
}
scriptFile := path.Join(dir, fmt.Sprintf("%s.%s", name, sbfc.Sbc.ScriptType))
err = ioutil.WriteFile(scriptFile, []byte(msg.GetPayload()), 0600)
if err != nil {
removeAll(dir, fmt.Sprintf("%s.*", name))
return
}
var runner FilterRunner
runner, err = createRunner(dir, name, conf)
if err != nil {
removeAll(dir, fmt.Sprintf("%s.*", name))
return
}
err = h.PipelineConfig().AddFilterRunner(runner)
if err == nil {
this.currentFilters++
}
break // only interested in the first item
}
}
}
return
}