|
| 1 | +/* |
| 2 | + Copyright 2022 Docker Compose CLI authors |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package metrics |
| 18 | + |
| 19 | +import ( |
| 20 | + "bytes" |
| 21 | + "encoding/json" |
| 22 | + "io" |
| 23 | + "net/http" |
| 24 | +) |
| 25 | + |
| 26 | +// Reporter reports metric events generated by the client. |
| 27 | +type Reporter interface { |
| 28 | + Heartbeat(cmd Command) |
| 29 | +} |
| 30 | + |
| 31 | +// HTTPReporter reports metric events to an HTTP endpoint. |
| 32 | +type HTTPReporter struct { |
| 33 | + client *http.Client |
| 34 | +} |
| 35 | + |
| 36 | +// NewHTTPReporter creates a new reporter that will report metric events using |
| 37 | +// the provided HTTP client. |
| 38 | +func NewHTTPReporter(client *http.Client) HTTPReporter { |
| 39 | + return HTTPReporter{client: client} |
| 40 | +} |
| 41 | + |
| 42 | +// Heartbeat reports a metric for aggregation. |
| 43 | +func (l HTTPReporter) Heartbeat(cmd Command) { |
| 44 | + entry, err := json.Marshal(cmd) |
| 45 | + if err != nil { |
| 46 | + // impossible: cannot fail on controlled input (i.e. no cycles) |
| 47 | + return |
| 48 | + } |
| 49 | + |
| 50 | + resp, _ := l.client.Post( |
| 51 | + "http://localhost/usage", |
| 52 | + "application/json", |
| 53 | + bytes.NewReader(entry), |
| 54 | + ) |
| 55 | + if resp != nil && resp.Body != nil { |
| 56 | + _ = resp.Body.Close() |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +// WriterReporter reports metrics as JSON lines to the provided writer. |
| 61 | +type WriterReporter struct { |
| 62 | + w io.Writer |
| 63 | +} |
| 64 | + |
| 65 | +// NewWriterReporter creates a new reporter that will write metrics to the |
| 66 | +// provided writer as JSON lines. |
| 67 | +func NewWriterReporter(w io.Writer) WriterReporter { |
| 68 | + return WriterReporter{w: w} |
| 69 | +} |
| 70 | + |
| 71 | +// Heartbeat reports a metric for aggregation. |
| 72 | +func (w WriterReporter) Heartbeat(cmd Command) { |
| 73 | + entry, err := json.Marshal(cmd) |
| 74 | + if err != nil { |
| 75 | + // impossible: cannot fail on controlled input (i.e. no cycles) |
| 76 | + return |
| 77 | + } |
| 78 | + entry = append(entry, '\n') |
| 79 | + _, _ = w.w.Write(entry) |
| 80 | +} |
| 81 | + |
| 82 | +// MuxReporter wraps multiple reporter instances and reports metrics to each |
| 83 | +// instance on invocation. |
| 84 | +type MuxReporter struct { |
| 85 | + reporters []Reporter |
| 86 | +} |
| 87 | + |
| 88 | +// NewMuxReporter creates a reporter that will report metrics to each of the |
| 89 | +// provided reporter instances. |
| 90 | +func NewMuxReporter(reporters ...Reporter) MuxReporter { |
| 91 | + return MuxReporter{reporters: reporters} |
| 92 | +} |
| 93 | + |
| 94 | +// Heartbeat reports a metric for aggregation. |
| 95 | +func (m MuxReporter) Heartbeat(cmd Command) { |
| 96 | + for i := range m.reporters { |
| 97 | + m.reporters[i].Heartbeat(cmd) |
| 98 | + } |
| 99 | +} |
0 commit comments