serkant.uluderya | e5afeff | 2021-02-23 18:00:23 +0300 | [diff] [blame] | 1 | // Copyright The OpenTelemetry Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package metric |
| 16 | |
| 17 | import ( |
| 18 | "context" |
| 19 | |
| 20 | "go.opentelemetry.io/otel/label" |
| 21 | ) |
| 22 | |
| 23 | // MeterImpl is the interface an SDK must implement to supply a Meter |
| 24 | // implementation. |
| 25 | type MeterImpl interface { |
| 26 | // RecordBatch atomically records a batch of measurements. |
| 27 | RecordBatch(ctx context.Context, labels []label.KeyValue, measurement ...Measurement) |
| 28 | |
| 29 | // NewSyncInstrument returns a newly constructed |
| 30 | // synchronous instrument implementation or an error, should |
| 31 | // one occur. |
| 32 | NewSyncInstrument(descriptor Descriptor) (SyncImpl, error) |
| 33 | |
| 34 | // NewAsyncInstrument returns a newly constructed |
| 35 | // asynchronous instrument implementation or an error, should |
| 36 | // one occur. |
| 37 | NewAsyncInstrument( |
| 38 | descriptor Descriptor, |
| 39 | runner AsyncRunner, |
| 40 | ) (AsyncImpl, error) |
| 41 | } |
| 42 | |
| 43 | // InstrumentImpl is a common interface for synchronous and |
| 44 | // asynchronous instruments. |
| 45 | type InstrumentImpl interface { |
| 46 | // Implementation returns the underlying implementation of the |
| 47 | // instrument, which allows the implementation to gain access |
| 48 | // to its own representation especially from a `Measurement`. |
| 49 | Implementation() interface{} |
| 50 | |
| 51 | // Descriptor returns a copy of the instrument's Descriptor. |
| 52 | Descriptor() Descriptor |
| 53 | } |
| 54 | |
| 55 | // SyncImpl is the implementation-level interface to a generic |
| 56 | // synchronous instrument (e.g., ValueRecorder and Counter instruments). |
| 57 | type SyncImpl interface { |
| 58 | InstrumentImpl |
| 59 | |
| 60 | // Bind creates an implementation-level bound instrument, |
| 61 | // binding a label set with this instrument implementation. |
| 62 | Bind(labels []label.KeyValue) BoundSyncImpl |
| 63 | |
| 64 | // RecordOne captures a single synchronous metric event. |
| 65 | RecordOne(ctx context.Context, number Number, labels []label.KeyValue) |
| 66 | } |
| 67 | |
| 68 | // BoundSyncImpl is the implementation-level interface to a |
| 69 | // generic bound synchronous instrument |
| 70 | type BoundSyncImpl interface { |
| 71 | |
| 72 | // RecordOne captures a single synchronous metric event. |
| 73 | RecordOne(ctx context.Context, number Number) |
| 74 | |
| 75 | // Unbind frees the resources associated with this bound instrument. It |
| 76 | // does not affect the metric this bound instrument was created through. |
| 77 | Unbind() |
| 78 | } |
| 79 | |
| 80 | // AsyncImpl is an implementation-level interface to an |
| 81 | // asynchronous instrument (e.g., Observer instruments). |
| 82 | type AsyncImpl interface { |
| 83 | InstrumentImpl |
| 84 | } |
| 85 | |
| 86 | // WrapMeterImpl constructs a `Meter` implementation from a |
| 87 | // `MeterImpl` implementation. |
| 88 | func WrapMeterImpl(impl MeterImpl, instrumentationName string, opts ...MeterOption) Meter { |
| 89 | return Meter{ |
| 90 | impl: impl, |
| 91 | name: instrumentationName, |
| 92 | version: NewMeterConfig(opts...).InstrumentationVersion, |
| 93 | } |
| 94 | } |