github.com/lulzWill/go-agent@v2.1.2+incompatible/internal/error_events.go (about) 1 package internal 2 3 import ( 4 "bytes" 5 "math/rand" 6 "time" 7 ) 8 9 // MarshalJSON is used for testing. 10 func (e *ErrorEvent) MarshalJSON() ([]byte, error) { 11 buf := bytes.NewBuffer(make([]byte, 0, 256)) 12 13 e.WriteJSON(buf) 14 15 return buf.Bytes(), nil 16 } 17 18 // WriteJSON prepares JSON in the format expected by the collector. 19 // https://source.datanerd.us/agents/agent-specs/blob/master/Error-Events.md 20 func (e *ErrorEvent) WriteJSON(buf *bytes.Buffer) { 21 w := jsonFieldsWriter{buf: buf} 22 buf.WriteByte('[') 23 buf.WriteByte('{') 24 w.stringField("type", "TransactionError") 25 w.stringField("error.class", e.Klass) 26 w.stringField("error.message", e.Msg) 27 w.floatField("timestamp", timeToFloatSeconds(e.When)) 28 w.stringField("transactionName", e.FinalName) 29 w.floatField("duration", e.Duration.Seconds()) 30 if e.Queuing > 0 { 31 w.floatField("queueDuration", e.Queuing.Seconds()) 32 } 33 if e.externalCallCount > 0 { 34 w.intField("externalCallCount", int64(e.externalCallCount)) 35 w.floatField("externalDuration", e.externalDuration.Seconds()) 36 } 37 if e.datastoreCallCount > 0 { 38 // Note that "database" is used for the keys here instead of 39 // "datastore" for historical reasons. 40 w.intField("databaseCallCount", int64(e.datastoreCallCount)) 41 w.floatField("databaseDuration", e.datastoreDuration.Seconds()) 42 } 43 buf.WriteByte('}') 44 buf.WriteByte(',') 45 userAttributesJSON(e.Attrs, buf, destError, e.ErrorData.ExtraAttributes) 46 buf.WriteByte(',') 47 agentAttributesJSON(e.Attrs, buf, destError) 48 buf.WriteByte(']') 49 } 50 51 type errorEvents struct { 52 events *analyticsEvents 53 } 54 55 func newErrorEvents(max int) *errorEvents { 56 return &errorEvents{ 57 events: newAnalyticsEvents(max), 58 } 59 } 60 61 func (events *errorEvents) Add(e *ErrorEvent) { 62 stamp := eventStamp(rand.Float32()) 63 events.events.addEvent(analyticsEvent{stamp, e}) 64 } 65 66 func (events *errorEvents) MergeIntoHarvest(h *Harvest) { 67 h.ErrorEvents.events.mergeFailed(events.events) 68 } 69 70 func (events *errorEvents) Data(agentRunID string, harvestStart time.Time) ([]byte, error) { 71 return events.events.CollectorJSON(agentRunID) 72 } 73 74 func (events *errorEvents) numSeen() float64 { return events.events.NumSeen() } 75 func (events *errorEvents) numSaved() float64 { return events.events.NumSaved() }