| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. |
| 4 |
| 5 // Package versions allows processes to report string-valued metrics with |
| 6 // versions of various libraries they link with. |
| 7 // |
| 8 // The metric is named 'luci/components/version', and it has single field |
| 9 // 'component' that defines the logical name of the component whose version is |
| 10 // reported by the metric value. |
| 11 // |
| 12 // Having such metrics allows to easily detect processes that use stale code in |
| 13 // production. |
| 14 // |
| 15 // Various go packages can register their versions during 'init' time, and all |
| 16 // registered version will be flushed to monitoring whenever 'Report' is called. |
| 17 package versions |
| 18 |
| 19 import ( |
| 20 "sync" |
| 21 |
| 22 "github.com/luci/luci-go/common/tsmon/field" |
| 23 "github.com/luci/luci-go/common/tsmon/metric" |
| 24 |
| 25 "golang.org/x/net/context" |
| 26 ) |
| 27 |
| 28 var registry struct { |
| 29 m sync.RWMutex |
| 30 r map[string]string |
| 31 } |
| 32 |
| 33 var ( |
| 34 versionMetric = metric.NewString( |
| 35 "luci/components/version", |
| 36 "Versions of LUCI components linked into the process.", |
| 37 nil, |
| 38 field.String("component"), |
| 39 ) |
| 40 ) |
| 41 |
| 42 // Register tells the library to start reporting a version for given component. |
| 43 // |
| 44 // This should usually called during 'init' time. |
| 45 // |
| 46 // The component name will be used as a value of 'component' metric field, and |
| 47 // the given version will become the actual reported metric value. It is a good |
| 48 // idea to use a fully qualified go package name as 'component'. |
| 49 func Register(component, version string) { |
| 50 registry.m.Lock() |
| 51 defer registry.m.Unlock() |
| 52 if registry.r == nil { |
| 53 registry.r = make(map[string]string, 1) |
| 54 } |
| 55 registry.r[component] = version |
| 56 } |
| 57 |
| 58 // Report populates 'luci/components/version' metric with versions of all |
| 59 // registered components. |
| 60 func Report(c context.Context) { |
| 61 registry.m.RLock() |
| 62 defer registry.m.RUnlock() |
| 63 for component := range registry.r { |
| 64 versionMetric.Set(c, registry.r[component], component) |
| 65 } |
| 66 } |
| OLD | NEW |