Example #1
0
// publishError publishes an event when a handler returns an error
func publishError(req *Request, e errors.Error) {
	if !PublishErrors {
		return
	}

	stacktrace := ""
	if e.MultiStack() != nil {
		stacktrace = e.MultiStack().String()
	}

	application := ""
	if req.Auth().IsAuth() && req.Auth().AuthUser() != nil {
		application = req.Auth().AuthUser().Application()
	}
	userId := ""
	if req.Auth().IsAuth() && req.Auth().AuthUser() != nil {
		userId = req.Auth().AuthUser().Id
	}

	msg := map[string]interface{}{
		"created":     time.Now(),
		"service":     Name,
		"version":     Version,
		"azName":      az,
		"hostname":    hostname,
		"instanceId":  InstanceID,
		"error":       e.Error(),
		"type":        e.Type(),
		"code":        e.Code(),
		"description": e.Description(),
		"httpCode":    e.HttpCode(),
		"context":     e.Context(),
		"userId":      userId,
		"application": application,
		"traceId":     req.TraceID(),
		"stacktrace":  stacktrace,
	}

	payload, err := json.Marshal(msg)
	if err != nil {
		log.Errorf("[Server] Failed to JSON encode error event: %v", err)
	}
	if err = nsq.Publish(errorTopic, payload); err != nil {
		log.Errorf("[Server] Failed to publish error event: %v", err)
	}
}
func (this *defaultPublisher) Publish(event map[string]string) error {
	// validation
	if event[keyEventType] == "" {
		return errors.New("Jstats event missing eventType")
	}
	ts := event[keyTimestamp]
	if ts == "" {
		return errors.New("Jstats event missing timestamp")
	}
	if _, err := strconv.ParseInt(ts, 10, 64); err != nil {
		return errors.New("Jstats event timestamp is wrong format")
	}

	bytes, err := json.Marshal(event)
	if err != nil {
		return fmt.Errorf("Error marshalling event %s", err)
	}
	if err := nsq.Publish(jstatsTopic, bytes); err != nil {
		return fmt.Errorf("Error sending jstats event %s", err)
	}
	return nil
}