NewInt64Metric create a Int64Metric metric, returns nil when viewName is empty.
(metricID MetricID, viewName string, description string, unit string, aggregation Aggregation, tagNames []string)
| 43 | |
| 44 | // NewInt64Metric create a Int64Metric metric, returns nil when viewName is empty. |
| 45 | func NewInt64Metric(metricID MetricID, viewName string, description string, unit string, aggregation Aggregation, tagNames []string) (*Int64Metric, error) { |
| 46 | if viewName == "" { |
| 47 | return nil, nil |
| 48 | } |
| 49 | |
| 50 | MetricMap.AddMapping(metricID, viewName) |
| 51 | |
| 52 | tagKeys, err := getTagKeysFromNames(tagNames) |
| 53 | if err != nil { |
| 54 | return nil, fmt.Errorf("failed to create metric %q because of tag creation failure: %v", viewName, err) |
| 55 | } |
| 56 | |
| 57 | var aggregationMethod *view.Aggregation |
| 58 | switch aggregation { |
| 59 | case LastValue: |
| 60 | aggregationMethod = view.LastValue() |
| 61 | case Sum: |
| 62 | aggregationMethod = view.Sum() |
| 63 | default: |
| 64 | return nil, fmt.Errorf("unknown aggregation option %q", aggregation) |
| 65 | } |
| 66 | |
| 67 | measure := stats.Int64(viewName, description, unit) |
| 68 | newView := &view.View{ |
| 69 | Name: viewName, |
| 70 | Measure: measure, |
| 71 | Description: description, |
| 72 | Aggregation: aggregationMethod, |
| 73 | TagKeys: tagKeys, |
| 74 | } |
| 75 | if err := view.Register(newView); err != nil { |
| 76 | return nil, fmt.Errorf("failed to register view for metric %q: %v", viewName, err) |
| 77 | } |
| 78 | |
| 79 | metric := Int64Metric{viewName, measure} |
| 80 | return &metric, nil |
| 81 | } |
| 82 | |
| 83 | // Record records a measurement for the metric, with provided tags as metric labels. |
| 84 | func (metric *Int64Metric) Record(tags map[string]string, measurement int64) error { |
no test coverage detected