Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package monitor | 5 package monitor |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "github.com/golang/protobuf/proto" | 8 "github.com/golang/protobuf/proto" |
| 9 "github.com/luci/luci-go/common/auth" | 9 "github.com/luci/luci-go/common/auth" |
| 10 "github.com/luci/luci-go/common/gcloud/gcps" | 10 "github.com/luci/luci-go/common/gcloud/gcps" |
| 11 "github.com/luci/luci-go/common/tsmon/types" | 11 "github.com/luci/luci-go/common/tsmon/types" |
| 12 » "google.golang.org/cloud" | 12 » "golang.org/x/net/context" |
| 13 "google.golang.org/cloud/pubsub" | 13 "google.golang.org/cloud/pubsub" |
| 14 ) | 14 ) |
| 15 | 15 |
| 16 type pubSubMonitor struct { | 16 type pubSubMonitor struct { |
| 17 context.Context | |
| 17 ps gcps.PubSub | 18 ps gcps.PubSub |
| 18 topic gcps.Topic | 19 topic gcps.Topic |
| 19 } | 20 } |
| 20 | 21 |
| 21 // NewPubsubMonitor returns a Monitor that sends metrics to the Cloud Pub/Sub | 22 // NewPubsubMonitor returns a Monitor that sends metrics to the Cloud Pub/Sub |
| 22 // API. | 23 // API. |
| 23 func NewPubsubMonitor(credentialPath string, project string, topic string) (Moni tor, error) { | 24 func NewPubsubMonitor(credentialPath string, project string, topic string) (Moni tor, error) { |
| 24 authOpts := auth.Options{ | 25 authOpts := auth.Options{ |
| 25 Scopes: gcps.PublisherScopes, | 26 Scopes: gcps.PublisherScopes, |
| 26 ServiceAccountJSONPath: credentialPath, | 27 ServiceAccountJSONPath: credentialPath, |
| 27 } | 28 } |
| 28 | 29 |
| 29 httpClient, err := auth.NewAuthenticator(auth.SilentLogin, authOpts).Cli ent() | 30 httpClient, err := auth.NewAuthenticator(auth.SilentLogin, authOpts).Cli ent() |
| 30 if err != nil { | 31 if err != nil { |
| 31 return nil, err | 32 return nil, err |
| 32 } | 33 } |
| 33 | 34 |
| 34 return &pubSubMonitor{ | 35 return &pubSubMonitor{ |
|
dnj (Google)
2016/01/21 04:36:24
Change in response to Pub/Sub changes. "context.Ba
| |
| 35 » » ps: gcps.New(cloud.NewContext(project, httpClient)), | 36 » » Context: context.Background(), |
| 36 » » topic: gcps.Topic(topic), | 37 » » ps: gcps.New(httpClient, project), |
| 38 » » topic: gcps.Topic(topic), | |
| 37 }, nil | 39 }, nil |
| 38 } | 40 } |
| 39 | 41 |
| 40 func (m *pubSubMonitor) ChunkSize() int { | 42 func (m *pubSubMonitor) ChunkSize() int { |
| 41 return 1000 | 43 return 1000 |
| 42 } | 44 } |
| 43 | 45 |
| 44 func (m *pubSubMonitor) Send(cells []types.Cell, defaultTarget types.Target) err or { | 46 func (m *pubSubMonitor) Send(cells []types.Cell, defaultTarget types.Target) err or { |
| 45 collection := serializeCells(cells, defaultTarget) | 47 collection := serializeCells(cells, defaultTarget) |
| 46 | 48 |
| 47 data, err := proto.Marshal(collection) | 49 data, err := proto.Marshal(collection) |
| 48 if err != nil { | 50 if err != nil { |
| 49 return err | 51 return err |
| 50 } | 52 } |
| 51 | 53 |
| 52 » _, err = m.ps.Publish(m.topic, &pubsub.Message{Data: data}) | 54 » _, err = m.ps.Publish(m, m.topic, &pubsub.Message{Data: data}) |
| 53 return err | 55 return err |
| 54 } | 56 } |
| OLD | NEW |