| 1 |
# Instrumentation |
| 2 |
|
| 3 |
Code contained in this directory contains instrumentation for 3rd-party Go packages and some packages from the standard library. |
| 4 |
|
| 5 |
## Instrumentation Packages |
| 6 |
|
| 7 |
The [OpenTelemetry registry](https://opentelemetry.io/registry/) is the best place to discover instrumentation packages. |
| 8 |
It will include packages outside of this project. |
| 9 |
|
| 10 |
The following instrumentation packages are provided for popular Go packages and use-cases. |
| 11 |
|
| 12 |
| Instrumentation Package | Metrics | Traces | |
| 13 |
| :---------------------: | :-----: | :----: | |
| 14 |
| [github.com/aws/aws-sdk-go-v2](./github.com/aws/aws-sdk-go-v2/otelaws)| | ✓ | |
| 15 |
| [github.com/emicklei/go-restful](./github.com/emicklei/go-restful/otelrestful) | | ✓ | |
| 16 |
| [github.com/gin-gonic/gin](./github.com/gin-gonic/gin/otelgin) | ✓ | ✓ | |
| 17 |
| [github.com/gorilla/mux](./github.com/gorilla/mux/otelmux) | ✓ | ✓ | |
| 18 |
| [github.com/labstack/echo](./github.com/labstack/echo/otelecho) | ✓ | ✓ | |
| 19 |
| [go.mongodb.org/mongo-driver](./go.mongodb.org/mongo-driver/mongo/otelmongo) | | ✓ | |
| 20 |
| [go.mongodb.org/mongo-driver/v2](./go.mongodb.org/mongo-driver/v2/mongo/otelmongo) | | ✓ | |
| 21 |
| [google.golang.org/grpc](./google.golang.org/grpc/otelgrpc) | ✓ | ✓ | |
| 22 |
| [host](./host) | ✓ | | |
| 23 |
| [net/http](./net/http/otelhttp) | ✓ | ✓ | |
| 24 |
| [net/http/httptrace](./net/http/httptrace/otelhttptrace) | | ✓ | |
| 25 |
| [runtime](./runtime) | ✓ | | |
| 26 |
|
| 27 |
## Organization |
| 28 |
|
| 29 |
In order to ensure the maintainability and discoverability of instrumentation packages, the following guidelines MUST be followed. |
| 30 |
|
| 31 |
### Packaging |
| 32 |
|
| 33 |
All instrumentation packages SHOULD be of the form: |
| 34 |
|
| 35 |
```sh |
| 36 |
go.opentelemetry.io/contrib/instrumentation/{IMPORT_PATH}/otel{PACKAGE_NAME} |
| 37 |
``` |
| 38 |
|
| 39 |
Where the [`{IMPORT_PATH}`](https://golang.org/ref/spec#ImportPath) and [`{PACKAGE_NAME}`](https://golang.org/ref/spec#PackageName) are the standard Go identifiers for the package being instrumented. |
| 40 |
|
| 41 |
For example: |
| 42 |
|
| 43 |
- `go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux` |
| 44 |
- `go.opentelemetry.io/contrib/instrumentation/gopkg.in/macaron.v1/otelmacaron` |
| 45 |
- `go.opentelemetry.io/contrib/instrumentation/database/sql/otelsql` |
| 46 |
|
| 47 |
Exceptions to this rule exist. |
| 48 |
For example, the [runtime](./runtime) and [host](./host) instrumentation do not instrument any Go package and therefore do not fit this structure. |
| 49 |
|
| 50 |
### Contents |
| 51 |
|
| 52 |
All instrumentation packages MUST adhere to [the projects' contributing guidelines](../CONTRIBUTING.md). |
| 53 |
Additionally the following guidelines for package composition need to be followed. |
| 54 |
|
| 55 |
- All instrumentation packages MUST be a Go module. |
| 56 |
Therefore, an appropriately configured `go.mod` and `go.sum` need to exist for each package. |
| 57 |
- To help understand the instrumentation a Go package documentation SHOULD be included. |
| 58 |
This documentation SHOULD be in a dedicated `doc.go` file if the package is more than one file. |
| 59 |
It SHOULD contain useful information like what the purpose of the instrumentation is, how to use it, and any compatibility restrictions that might exist. |
| 60 |
- Examples of how to actually use the instrumentation SHOULD be included. |
| 61 |
- All instrumentation packages MUST provide an option to accept a `TracerProvider` if it uses a Tracer, a `MeterProvider` if it uses a Meter, and `Propagators` if it handles any context propagation. |
| 62 |
Also, packages MUST use the default `TracerProvider`, `MeterProvider`, and `Propagators` supplied by the `global` package if no optional one is provided. |
| 63 |
- All instrumentation packages MUST NOT provide an option to accept a `Tracer` or `Meter`. |
| 64 |
- All instrumentation packages MUST define a `ScopeName` constant with a value matching the instrumentation package and use it when creating a `Tracer` or `Meter`. |
| 65 |
- All instrumentation packages MUST define a `Version` function returning the version of the module containing the instrumentation and use it when creating a `Tracer` or `Meter`. |
| 66 |
|
| 67 |
### Recording Errors |
| 68 |
|
| 69 |
When an instrumented operation returns a non-nil `error` and the semantic conventions classify the outcome as an error, the instrumentation: |
| 70 |
|
| 71 |
1. MUST set the span status to `codes.Error` with a description of `err.Error()`, |
| 72 |
|
| 73 |
2. SHOULD also set the `error.type` attribute, for example by using the `ErrorType` function from the `semconv` package, |
| 74 |
|
| 75 |
3. SHOULD NOT use `span.RecordError(err)` for this purpose. |
| 76 |
|
| 77 |
For example: |
| 78 |
|
| 79 |
```go |
| 80 |
res, err := t.rt.RoundTrip(r) |
| 81 |
if err != nil { |
| 82 |
span.SetAttributes(semconv.ErrorType(err)) |
| 83 |
span.SetStatus(codes.Error, err.Error()) |
| 84 |
} |
| 85 |
``` |