This repository has been archived by the owner on Apr 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(GH-222) Add Honeycomb/OpenTelemetry to PCT
This commit takes the first step towards implementing telemetry for the PCT binary by adding open telemetry and the Honeycomb exporter to it; in this initial phase, the only information being captured is the OS and architecture of the machine running PCT. This implementation is designed with three compile-time flags in mind: 1. Whether or not telemetry should be turned on at all 2. The Honeycomb API key to use if telemetry is turned on 3. The Honeycomb dataset to send telemetry to if turned on These are handled in the ldflags and build tag of goreleaser and the build scripts optionally set these values to coherent defaults (do not report the telemetry, use a special identifier to make it obvious the binary is not configured to send telemetry to Honeycomb). This immediately allows us to create builds with/without telemetry via calling goreleaser. The implementation in only performs telemetry setup if: 1. The build tag for telemetry is set to true 2. The Honeycomb API Key does not equal `not_set` 3. The Honeycomb dataset does not equal `not_set` If any of those conditions are not met, the binary *can not* report any telemetry. Moreover, in local testing it turns out that if the telemetry backends are not configured the inclusion of any tracers and spans are wholly ignored. The only thing that *must* be propagated is the current context. Exactly how to do this with the commands/subcommands will need to be determined.
- Loading branch information
1 parent
609aaf4
commit 682eee4
Showing
10 changed files
with
178 additions
and
8 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
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
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,71 @@ | ||
//go:build telemetry | ||
// +build telemetry | ||
|
||
package telemetry | ||
|
||
import ( | ||
"context" | ||
"runtime" | ||
|
||
"github.com/rs/zerolog/log" | ||
|
||
"go.opentelemetry.io/otel" | ||
"go.opentelemetry.io/otel/attribute" | ||
"go.opentelemetry.io/otel/exporters/otlp" | ||
"go.opentelemetry.io/otel/exporters/otlp/otlpgrpc" | ||
"go.opentelemetry.io/otel/propagation" | ||
sdktrace "go.opentelemetry.io/otel/sdk/trace" | ||
"go.opentelemetry.io/otel/semconv" | ||
"go.opentelemetry.io/otel/trace" | ||
"google.golang.org/grpc/credentials" | ||
|
||
"go.opentelemetry.io/otel/sdk/resource" | ||
) | ||
|
||
func Start(ctx context.Context, honeycomb_api_key string, honeycomb_dataset string) { | ||
// if telemetry is turned on and honeycomb is configured, hook it up | ||
if honeycomb_api_key != "not_set" && honeycomb_dataset != "not_set" { | ||
exp, err := otlp.NewExporter( | ||
ctx, | ||
otlpgrpc.NewDriver( | ||
otlpgrpc.WithEndpoint("api.honeycomb.io:443"), | ||
otlpgrpc.WithHeaders(map[string]string{ | ||
"x-honeycomb-team": honeycomb_api_key, | ||
"x-honeycomb-dataset": honeycomb_dataset, | ||
}), | ||
otlpgrpc.WithTLSCredentials(credentials.NewClientTLSFromCert(nil, "")), | ||
), | ||
) | ||
if err != nil { | ||
log.Fatal().Msgf("failed to initialize exporter: %v", err) | ||
} | ||
|
||
// Create a new tracer provider with a batch span processor and the otlp exporter. | ||
// Add a resource attribute service.name that identifies the service in the Honeycomb UI. | ||
tp := sdktrace.NewTracerProvider( | ||
sdktrace.WithBatcher(exp), | ||
sdktrace.WithResource(resource.NewWithAttributes(semconv.ServiceNameKey.String("ExampleService"))), | ||
) | ||
|
||
// Handle this error in a sensible manner where possible | ||
defer func() { _ = tp.Shutdown(ctx) }() | ||
|
||
// Set the Tracer Provider and the W3C Trace Context propagator as globals | ||
otel.SetTracerProvider(tp) | ||
otel.SetTextMapPropagator( | ||
propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}), | ||
) | ||
} | ||
|
||
tracer := otel.Tracer("pct") | ||
|
||
osKey := attribute.Key("osinfo/os") | ||
osArch := attribute.Key("osinfo/arch") | ||
|
||
var span trace.Span | ||
_, span = tracer.Start(ctx, "execution") | ||
defer span.End() | ||
|
||
span.SetAttributes(osKey.String(runtime.GOOS)) | ||
span.SetAttributes(osArch.String(runtime.GOARCH)) | ||
} |
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,12 @@ | ||
//go:build !telemetry | ||
// +build !telemetry | ||
|
||
package telemetry | ||
|
||
import ( | ||
"context" | ||
) | ||
|
||
func Start(ctx context.Context, honeycomb_api_key string, honeycomb_dataset string) { | ||
// deliberately does nothing | ||
} |