func (cc CloudController) GetBillingManagersByOrgGuid(guid, token string) ([]CloudControllerUser, error) {
	var ccUsers []CloudControllerUser
	then := time.Now()

	list, err := cc.client.Organizations.ListBillingManagers(guid, token)
	if err != nil {
		return ccUsers, NewFailure(0, err.Error())
	}

	users, err := list.AllUsers(token)
	if err != nil {
		return ccUsers, NewFailure(0, err.Error())
	}

	duration := time.Now().Sub(then)

	metrics.NewMetric("histogram", map[string]interface{}{
		"name":  "notifications.external-requests.cc.billing-managers-by-org-guid",
		"value": duration.Seconds(),
	}).Log()

	for _, user := range users {
		ccUsers = append(ccUsers, CloudControllerUser{
			GUID: user.GUID,
		})
	}

	return ccUsers, nil
}
コード例 #2
0
func (cc CloudController) GetUsersBySpaceGuid(guid, token string) ([]CloudControllerUser, error) {
	then := time.Now()

	list, err := cc.client.Spaces.ListUsers(guid, token)
	if err != nil {
		return []CloudControllerUser{}, NewFailure(0, err.Error())
	}

	users, err := list.AllUsers(token)
	if err != nil {
		return []CloudControllerUser{}, NewFailure(0, err.Error())
	}

	duration := time.Now().Sub(then)

	metrics.NewMetric("histogram", map[string]interface{}{
		"name":  "notifications.external-requests.cc.users-by-space-guid",
		"value": duration.Seconds(),
	}).Log()

	ccUsers := []CloudControllerUser{}
	for _, user := range users {
		ccUsers = append(ccUsers, CloudControllerUser{
			GUID: user.GUID,
		})
	}

	return ccUsers, nil
}
コード例 #3
0
ファイル: load_space.go プロジェクト: dieucao/notifications
func (cc CloudController) LoadSpace(spaceGuid, token string) (CloudControllerSpace, error) {
	then := time.Now()

	space, err := cc.client.Spaces.Get(spaceGuid, token)
	if err != nil {
		_, ok := err.(rainmaker.NotFoundError)
		if ok {
			return CloudControllerSpace{}, NewFailure(404, fmt.Sprintf("Space %q could not be found", spaceGuid))
		} else {
			return CloudControllerSpace{}, NewFailure(0, err.Error())
		}
	}

	duration := time.Now().Sub(then)

	metrics.NewMetric("histogram", map[string]interface{}{
		"name":  "notifications.external-requests.cc.space",
		"value": duration.Seconds(),
	}).Log()

	return CloudControllerSpace{
		GUID:             space.GUID,
		Name:             space.Name,
		OrganizationGUID: space.OrganizationGUID,
	}, nil
}
コード例 #4
0
ファイル: token_loader.go プロジェクト: dieucao/notifications
func (t *TokenLoader) Load(uaaHost string) (string, error) {
	then := time.Now()

	token, err := t.uaaClient.GetClientToken(uaaHost)

	duration := time.Now().Sub(then)

	metrics.NewMetric("histogram", map[string]interface{}{
		"name":  "notifications.external-requests.uaa.client-token",
		"value": duration.Seconds(),
	}).Log()
	return token, err
}
コード例 #5
0
ファイル: user_loader.go プロジェクト: dieucao/notifications
func (loader UserLoader) fetchUsersByIDs(token string, guids []string) ([]uaa.User, error) {
	then := time.Now()

	usersByIDs, err := loader.uaaClient.UsersEmailsByIDs(token, guids...)

	duration := time.Now().Sub(then)

	metrics.NewMetric("histogram", map[string]interface{}{
		"name":  "notifications.external-requests.uaa.users-email",
		"value": duration.Seconds(),
	}).Log()

	return usersByIDs, err
}
コード例 #6
0
func (ware RequestCounter) ServeHTTP(w http.ResponseWriter, req *http.Request, context stack.Context) bool {
	path := "UNKNOWN"
	var match mux.RouteMatch
	if ok := ware.matcher.Match(req, &match); ok {
		name := match.Route.GetName()
		path = convertNameToMetricPath(name)
	}

	m := metrics.NewMetric("counter", map[string]interface{}{
		"name": "notifications.web",
		"tags": map[string]string{
			"endpoint": req.Method + path,
		},
	})
	m.LogWith(ware.logger)

	return true
}
コード例 #7
0
func (h DeliveryFailureHandler) Handle(job Retryable, logger lager.Logger) {
	retryCount, _ := job.State()
	if retryCount > 9 {
		return
	}

	duration := time.Duration(int64(math.Pow(2, float64(retryCount)))) * time.Minute
	job.Retry(duration)

	retryCount, activeAt := job.State()
	logger.Info("delivery-failed-retrying", lager.Data{
		"retry_count": retryCount,
		"active_at":   activeAt.Format(time.RFC3339),
	})

	// TODO: (rm) find way to test this without having to mock out globals
	metrics.NewMetric("counter", map[string]interface{}{
		"name": "notifications.worker.retry",
	}).Log()
}
コード例 #8
0
func (cc CloudController) LoadOrganization(guid, token string) (CloudControllerOrganization, error) {
	then := time.Now()

	org, err := cc.client.Organizations.Get(guid, token)
	if err != nil {
		return CloudControllerOrganization{}, NewFailure(0, err.Error())
	}

	duration := time.Now().Sub(then)

	metrics.NewMetric("histogram", map[string]interface{}{
		"name":  "notifications.external-requests.cc.organization",
		"value": duration.Seconds(),
	}).Log()

	return CloudControllerOrganization{
		GUID: org.GUID,
		Name: org.Name,
	}, nil
}
コード例 #9
0
func (worker DeliveryWorker) Deliver(job *gobble.Job) {
	var typedJob struct {
		JobType string
	}

	err := job.Unmarshal(&typedJob)
	if err != nil {
		metrics.NewMetric("counter", map[string]interface{}{
			"name": "notifications.worker.panic.json",
		}).Log()

		worker.deliveryFailureHandler.Handle(job, worker.logger)
		return
	}

	switch typedJob.JobType {
	case "campaign":
		err := worker.strategyDeterminer.Determine(worker.database.Connection(), worker.uaaHost, *job)
		if err != nil {
			worker.deliveryFailureHandler.Handle(job, worker.logger)
		}
	case "v2":
		var delivery Delivery
		job.Unmarshal(&delivery)

		err = worker.V2Workflow.Deliver(delivery, worker.logger)
		if err != nil {
			worker.deliveryFailureHandler.Handle(job, worker.logger)
			status := StatusFailed
			if job.ShouldRetry {
				status = StatusRetry
			}

			worker.messageStatusUpdater.Update(worker.database.Connection(), delivery.MessageID, status, delivery.CampaignID, worker.logger)
		}
	default:
		worker.V1Process.Deliver(job, worker.logger)
	}
}
コード例 #10
0
ファイル: v1_process.go プロジェクト: dieucao/notifications
func (p V1Process) Deliver(job *gobble.Job, logger lager.Logger) error {
	var delivery Delivery
	err := job.Unmarshal(&delivery)
	if err != nil {
		metrics.NewMetric("counter", map[string]interface{}{
			"name": "notifications.worker.panic.json",
		}).Log()

		p.deliveryFailureHandler.Handle(job, logger)
		return nil
	}

	logger = logger.WithData(lager.Data{
		"message_id":      delivery.MessageID,
		"vcap_request_id": delivery.VCAPRequestID,
	})

	if p.dbTrace {
		p.database.TraceOn("", gorpCompatibleLogger{logger})
	}

	err = p.receiptsRepo.CreateReceipts(p.database.Connection(), []string{delivery.UserGUID}, delivery.ClientID, delivery.Options.KindID)
	if err != nil {
		p.deliveryFailureHandler.Handle(job, logger)
		return nil
	}

	if delivery.Email == "" {
		var token string

		token, err = p.tokenLoader.Load(p.uaaHost)
		if err != nil {
			p.deliveryFailureHandler.Handle(job, logger)
			return nil
		}

		users, err := p.userLoader.Load([]string{delivery.UserGUID}, token)
		if err != nil || len(users) < 1 {
			p.deliveryFailureHandler.Handle(job, logger)
			return nil
		}

		emails := users[delivery.UserGUID].Emails
		if len(emails) > 0 {
			delivery.Email = emails[0]
		}
	}

	logger = logger.WithData(lager.Data{
		"recipient": delivery.Email,
	})

	if p.shouldDeliver(delivery, logger) {
		status := p.deliver(delivery, logger)

		if status != StatusDelivered {
			p.deliveryFailureHandler.Handle(job, logger)
			return nil
		} else {
			metrics.NewMetric("counter", map[string]interface{}{
				"name": "notifications.worker.delivered",
			}).Log()
		}
	} else {
		metrics.NewMetric("counter", map[string]interface{}{
			"name": "notifications.worker.unsubscribed",
		}).Log()
	}

	return nil
}
コード例 #11
0
ファイル: metric_test.go プロジェクト: dieucao/notifications
)

var _ = Describe("Metric", func() {
	var packageLogger *log.Logger
	var buffer *bytes.Buffer

	BeforeEach(func() {
		buffer = bytes.NewBuffer([]byte{})
		packageLogger = metrics.DefaultLogger
		metrics.DefaultLogger = metrics.NewLogger(buffer)
	})

	AfterEach(func() {
		metrics.DefaultLogger = packageLogger
	})

	It("can log itself", func() {
		metric := metrics.NewMetric("counter", map[string]interface{}{
			"name": "test",
		})
		metric.Log()

		message, err := buffer.ReadString('\n')
		if err != nil {
			panic(err)
		}

		Expect(message).To(Equal(`[METRIC] {"kind":"counter","payload":{"name":"test"}}` + "\n"))
	})
})