-
-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add opentelemetry for the relay proxy (#1312)
* Add opentelemetry for the relay proxy Signed-off-by: Thomas Poignant <[email protected]> * Add doc Signed-off-by: Thomas Poignant <[email protected]> * Add configuration for openTelemetryOtlpEndpoint Signed-off-by: Thomas Poignant <[email protected]> --------- Signed-off-by: Thomas Poignant <[email protected]> Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
- Loading branch information
1 parent
4c6a9af
commit ea12ff9
Showing
12 changed files
with
244 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package opentelemetry | ||
|
||
import ( | ||
"context" | ||
"github.com/thomaspoignant/go-feature-flag/cmd/relayproxy/config" | ||
"go.opentelemetry.io/otel" | ||
"go.opentelemetry.io/otel/attribute" | ||
"go.opentelemetry.io/otel/exporters/otlp/otlptrace" | ||
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp" | ||
"go.opentelemetry.io/otel/sdk/resource" | ||
sdktrace "go.opentelemetry.io/otel/sdk/trace" | ||
"net/url" | ||
) | ||
|
||
type OtelService struct { | ||
otelTraceProvider *sdktrace.TracerProvider | ||
otelExporter *otlptrace.Exporter | ||
} | ||
|
||
func NewOtelService() OtelService { | ||
return OtelService{} | ||
} | ||
|
||
// Init the OpenTelemetry service | ||
func (s *OtelService) Init(ctx context.Context, config config.Config) error { | ||
// parsing the OpenTelemetry endpoint | ||
u, err := url.Parse(config.OpenTelemetryOtlpEndpoint) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var opts []otlptracehttp.Option | ||
if u.Scheme == "http" { | ||
opts = append(opts, otlptracehttp.WithInsecure()) | ||
} | ||
opts = append(opts, otlptracehttp.WithEndpoint(u.Host)) | ||
client := otlptracehttp.NewClient(opts...) | ||
|
||
s.otelExporter, err = otlptrace.New(ctx, client) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
s.otelTraceProvider = sdktrace.NewTracerProvider( | ||
sdktrace.WithSampler(sdktrace.AlwaysSample()), | ||
sdktrace.WithBatcher(s.otelExporter), | ||
sdktrace.WithResource(resource.NewSchemaless( | ||
attribute.String("service.name", "go-feature-flag"), | ||
attribute.String("service.version", config.Version), | ||
)), | ||
) | ||
otel.SetTracerProvider(s.otelTraceProvider) | ||
return nil | ||
} | ||
|
||
// Stop the OpenTelemetry service | ||
func (s *OtelService) Stop() error { | ||
if s.otelExporter != nil { | ||
err := s.otelExporter.Shutdown(context.Background()) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
if s.otelTraceProvider != nil { | ||
err := s.otelTraceProvider.Shutdown(context.Background()) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package config | ||
|
||
const OtelTracerName = "go-feature-flag" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# Test OpenTelemetry Tracing | ||
|
||
**GO Feature Flag** is able to generate some trace if you use the OpenTelemetry. | ||
|
||
If you want to test it locally, you can use the following command to start a local OpenTelemetry collector: | ||
|
||
```bash | ||
docker run --rm --name jaeger \ | ||
-e COLLECTOR_ZIPKIN_HOST_PORT=:9411 \ | ||
-e "COLLECTOR_OTLP_ENABLED=true" \ | ||
-p 6831:6831/udp \ | ||
-p 6832:6832/udp \ | ||
-p 5778:5778 \ | ||
-p 16686:16686 \ | ||
-p 4317:4317 \ | ||
-p 4318:4318 \ | ||
-p 14250:14250 \ | ||
-p 14268:14268 \ | ||
-p 14269:14269 \ | ||
-p 9411:9411 \ | ||
jaegertracing/all-in-one | ||
``` | ||
|
||
When your collector is up, you can configure GO Feature Flag relay-proxy by setting the OTLP endpoint in your configuration file. | ||
```yaml | ||
#... | ||
openTelemetryOtlpEndpoint: http://localhost:4318 | ||
``` | ||
You can connect to **`jaeger`** at this address: http://localhost:16686/search. | ||
|
||
After a 1st call to the API you will see a service call `go-feature-flag` and you can check the traces. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.