|
| 1 | +# Fx Trace Module |
| 2 | + |
| 3 | +[](https://github.com/ankorstore/yokai/actions/workflows/fxtrace-ci.yml) |
| 4 | +[](https://goreportcard.com/report/github.com/ankorstore/yokai/fxtrace) |
| 5 | +[](https://app.codecov.io/gh/ankorstore/yokai/tree/main/fxtrace) |
| 6 | +[](https://deps.dev/go/github.com%2Fankorstore%2Fyokai%2Ffxtrace) |
| 7 | +[](https://pkg.go.dev/github.com/ankorstore/yokai/fxtrace) |
| 8 | + |
| 9 | +> [Fx](https://uber-go.github.io/fx/) module for [trace](https://github.com/ankorstore/yokai/tree/main/trace). |
| 10 | +
|
| 11 | +<!-- TOC --> |
| 12 | +* [Installation](#installation) |
| 13 | +* [Documentation](#documentation) |
| 14 | + * [Dependencies](#dependencies) |
| 15 | + * [Loading](#loading) |
| 16 | + * [Configuration](#configuration) |
| 17 | + * [Override](#override) |
| 18 | + * [Testing](#testing) |
| 19 | +<!-- TOC --> |
| 20 | + |
| 21 | +## Installation |
| 22 | + |
| 23 | +```shell |
| 24 | +go get github.com/ankorstore/yokai/fxtrace |
| 25 | +``` |
| 26 | + |
| 27 | +## Documentation |
| 28 | + |
| 29 | +### Dependencies |
| 30 | + |
| 31 | +This module is intended to be used alongside the [fxconfig](https://github.com/ankorstore/yokai/tree/main/fxconfig) |
| 32 | +module. |
| 33 | + |
| 34 | +### Loading |
| 35 | + |
| 36 | +To load the module in your Fx application: |
| 37 | + |
| 38 | +```go |
| 39 | +package main |
| 40 | + |
| 41 | +import ( |
| 42 | + "context" |
| 43 | + |
| 44 | + "github.com/ankorstore/yokai/config" |
| 45 | + "github.com/ankorstore/yokai/fxconfig" |
| 46 | + "github.com/ankorstore/yokai/fxtrace" |
| 47 | + oteltrace "go.opentelemetry.io/otel/trace" |
| 48 | + "go.uber.org/fx" |
| 49 | +) |
| 50 | + |
| 51 | +func main() { |
| 52 | + fx.New( |
| 53 | + fxconfig.FxConfigModule, // load the module dependency |
| 54 | + fxtrace.FxTraceModule, // load the module |
| 55 | + fx.Invoke(func(tracerProvider oteltrace.TracerProvider) { |
| 56 | + // invoke the tracer provider to create a span |
| 57 | + _, span := tracerProvider.Tracer("some tracer").Start(context.Background(), "some span") |
| 58 | + defer span.End() |
| 59 | + }), |
| 60 | + ).Run() |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +### Configuration |
| 65 | + |
| 66 | +This module provides the possibility to configure the `processor`: |
| 67 | + |
| 68 | +- `noop`: to async void traces (default and fallback) |
| 69 | +- `stdout`: to async print traces to stdout |
| 70 | +- `otlp-grpc`: to async send traces to [OTLP/gRPC](https://opentelemetry.io/docs/specs/otlp/#otlpgrpc) collectors (ex: [Jaeger](https://www.jaegertracing.io/), [Grafana](https://grafana.com/docs/tempo/latest/configuration/grafana-agent/#grafana-agent), etc.) |
| 71 | +- `test`: to sync store traces in memory (for testing assertions) |
| 72 | + |
| 73 | +If an error occurs while creating the processor (for example failing OTLP/gRPC connection), the `noop` processor will be |
| 74 | +used as safety fallback (to prevent outages). |
| 75 | + |
| 76 | +This module also provides possibility to configure the `sampler`: |
| 77 | + |
| 78 | +- `parent-based-always-on`: always on depending on parent (default) |
| 79 | +- `parent-based-always-off`: always off depending on parent |
| 80 | +- `parent-based-trace-id-ratio`: trace id ratio based depending on parent |
| 81 | +- `always-on`: always on |
| 82 | +- `always-off`: always off |
| 83 | +- `trace-id-ratio`: trace id ratio based |
| 84 | + |
| 85 | +Example with `stdout` processor (with pretty print) and `parent-based-trace-id-ratio` sampler (ratio=0.5): |
| 86 | + |
| 87 | +```yaml |
| 88 | +# ./configs/config.yaml |
| 89 | +app: |
| 90 | + name: app |
| 91 | + env: dev |
| 92 | + version: 0.1.0 |
| 93 | + debug: false |
| 94 | +modules: |
| 95 | + trace: |
| 96 | + processor: |
| 97 | + type: stdout |
| 98 | + options: |
| 99 | + pretty: true |
| 100 | + sampler: |
| 101 | + type: parent-based-trace-id-ratio |
| 102 | + options: |
| 103 | + ratio: 0.5 |
| 104 | +``` |
| 105 | +
|
| 106 | +Another example with `otlp-grpc` processor (on jaeger:4317 host) and `always-on` sampler: |
| 107 | + |
| 108 | +```yaml |
| 109 | +# ./configs/config.yaml |
| 110 | +app: |
| 111 | + name: app |
| 112 | + env: dev |
| 113 | + version: 0.1.0 |
| 114 | + debug: false |
| 115 | +modules: |
| 116 | + trace: |
| 117 | + processor: |
| 118 | + type: otlp-grpc |
| 119 | + options: |
| 120 | + host: jaeger:4317 |
| 121 | + sampler: |
| 122 | + type: always-on |
| 123 | +``` |
| 124 | + |
| 125 | +### Override |
| 126 | + |
| 127 | +By default, the `oteltrace.TracerProvider` is created by the [DefaultTracerProviderFactory](https://github.com/ankorstore/yokai/blob/main/trace/factory.go). |
| 128 | + |
| 129 | +If needed, you can provide your own factory and override the module: |
| 130 | + |
| 131 | +```go |
| 132 | +package main |
| 133 | +
|
| 134 | +import ( |
| 135 | + "context" |
| 136 | +
|
| 137 | + "github.com/ankorstore/yokai/fxconfig" |
| 138 | + "github.com/ankorstore/yokai/fxtrace" |
| 139 | + "github.com/ankorstore/yokai/trace" |
| 140 | + otelsdktrace "go.opentelemetry.io/otel/sdk/trace" |
| 141 | + oteltrace "go.opentelemetry.io/otel/trace" |
| 142 | + "go.uber.org/fx" |
| 143 | +) |
| 144 | +
|
| 145 | +type CustomTracerProviderFactory struct{} |
| 146 | +
|
| 147 | +func NewCustomTracerProviderFactory() trace.TracerProviderFactory { |
| 148 | + return &CustomTracerProviderFactory{} |
| 149 | +} |
| 150 | +
|
| 151 | +func (f *CustomTracerProviderFactory) Create(options ...trace.TracerProviderOption) (*otelsdktrace.TracerProvider, error) { |
| 152 | + return &otelsdktrace.TracerProvider{...}, nil |
| 153 | +} |
| 154 | +
|
| 155 | +func main() { |
| 156 | + fx.New( |
| 157 | + fxconfig.FxConfigModule, // load the module dependency |
| 158 | + fxtrace.FxTraceModule, // load the module |
| 159 | + fx.Decorate(NewCustomTracerProviderFactory), // override the module with a custom factory |
| 160 | + fx.Invoke(func(tracerProvider oteltrace.TracerProvider) { // invoke the custom tracer provider |
| 161 | + _, span := tracerProvider.Tracer("custom tracer").Start(context.Background(), "custom span") |
| 162 | + defer span.End() |
| 163 | + }), |
| 164 | + ).Run() |
| 165 | +} |
| 166 | +``` |
| 167 | + |
| 168 | +### Testing |
| 169 | + |
| 170 | +This module provides the possibility to easily test your trace spans, using the [TestTraceExporter](https://github.com/ankorstore/yokai/blob/main/trace/tracetest/exporter.go) with `modules.trace.processor.type=test`. |
| 171 | + |
| 172 | +```yaml |
| 173 | +# ./configs/config.test.yaml |
| 174 | +modules: |
| 175 | + trace: |
| 176 | + processor: |
| 177 | + type: test # to send traces to test buffer |
| 178 | +``` |
| 179 | + |
| 180 | +You can then test: |
| 181 | + |
| 182 | +```go |
| 183 | +package main_test |
| 184 | +
|
| 185 | +import ( |
| 186 | + "context" |
| 187 | + "testing" |
| 188 | +
|
| 189 | + "github.com/ankorstore/yokai/fxconfig" |
| 190 | + "github.com/ankorstore/yokai/fxtrace" |
| 191 | + "github.com/ankorstore/yokai/trace/tracetest" |
| 192 | + "go.opentelemetry.io/otel/attribute" |
| 193 | + oteltrace "go.opentelemetry.io/otel/trace" |
| 194 | + "go.uber.org/fx" |
| 195 | + "go.uber.org/fx/fxtest" |
| 196 | +) |
| 197 | +
|
| 198 | +func TestTracerProvider(t *testing.T) { |
| 199 | + t.Setenv("APP_NAME", "test") |
| 200 | + t.Setenv("APP_ENV", "test") |
| 201 | +
|
| 202 | + var exporter tracetest.TestTraceExporter |
| 203 | +
|
| 204 | + fxtest.New( |
| 205 | + t, |
| 206 | + fx.NopLogger, |
| 207 | + fxconfig.FxConfigModule, |
| 208 | + fxtrace.FxTraceModule, |
| 209 | + fx.Invoke(func(tracerProvider oteltrace.TracerProvider) { |
| 210 | + _, span := tracerProvider.Tracer("some tracer").Start( |
| 211 | + context.Background(), |
| 212 | + "some span", |
| 213 | + oteltrace.WithAttributes(attribute.String("some attribute name", "some attribute value")), |
| 214 | + ) |
| 215 | + defer span.End() |
| 216 | + }), |
| 217 | + fx.Populate(&exporter), // extracts the TestTraceExporter from the Fx container |
| 218 | + ).RequireStart().RequireStop() |
| 219 | +
|
| 220 | + // assertion success |
| 221 | + tracetest.AssertHasTraceSpan(t, exporter, "some span", attribute.String("some attribute name", "some attribute value")) |
| 222 | +} |
| 223 | +``` |
| 224 | + |
| 225 | +See the `trace` module testing [documentation](https://github.com/ankorstore/yokai/tree/main/trace#test-span-processor) for more details. |
0 commit comments