-
Notifications
You must be signed in to change notification settings - Fork 118
Prototype sending product telemetry #1246
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,129 @@ | ||
package static | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/go-logr/logr" | ||
"go.opentelemetry.io/otel" | ||
"go.opentelemetry.io/otel/attribute" | ||
"go.opentelemetry.io/otel/exporters/otlp/otlptrace" | ||
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc" | ||
"go.opentelemetry.io/otel/exporters/stdout/stdouttrace" | ||
"go.opentelemetry.io/otel/sdk/resource" | ||
sdktrace "go.opentelemetry.io/otel/sdk/trace" | ||
semconv "go.opentelemetry.io/otel/semconv/v1.17.0" | ||
v1 "k8s.io/api/core/v1" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
type errorHandler struct { | ||
logger logr.Logger | ||
} | ||
|
||
func (h errorHandler) Handle(err error) { | ||
h.logger.Error(err, "error in telemetry") | ||
} | ||
|
||
type telemetryReporter struct { | ||
k8sClient client.Client | ||
logger logr.Logger | ||
} | ||
|
||
func (r *telemetryReporter) Start(ctx context.Context) error { | ||
// runs once in a POC. | ||
|
||
// telemetry endpoint configuration | ||
var ( | ||
// /otel-collector/README.md deploys a collector with the endpoint below | ||
// no TLS or auth headers are needed for that collector | ||
endpoint = "my-opentelemetry-collector.default.svc.cluster.local:4317" | ||
secure = false // use TLS or not. | ||
headers = map[string]string{} // allows t add headers to a GRPC connection. (e.g. authentication) | ||
) | ||
|
||
// gather telemetry data | ||
var ns v1.Namespace | ||
err := r.k8sClient.Get(ctx, client.ObjectKey{Name: "kube-system"}, &ns) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// cluster ID (UUID of kube-system namespace) | ||
clusterID := string(ns.UID) | ||
// NGF version | ||
ngfVersion := "product-tel-prototype-0.0.1" | ||
|
||
// configure OTel | ||
otel.SetLogger(r.logger) | ||
otel.SetErrorHandler(errorHandler{ | ||
logger: r.logger, | ||
}) | ||
|
||
// create resource | ||
res, err := resource.Merge( | ||
resource.Default(), | ||
resource.NewWithAttributes( | ||
"", | ||
semconv.ServiceName("NGF"), | ||
semconv.ServiceVersion("1.0.0"), | ||
), | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
stdoutExporter, err := newStdoutExporter() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
otelExporter, err := newOTLPExporter(ctx, endpoint, secure, headers) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
provider := sdktrace.NewTracerProvider( | ||
sdktrace.WithBatcher(stdoutExporter), // prints to stdout | ||
sdktrace.WithBatcher(otelExporter), // sends to a collector | ||
sdktrace.WithResource(res), | ||
) | ||
|
||
tracer := provider.Tracer("product-telemetry") | ||
|
||
// create span | ||
_, span := tracer.Start(ctx, "report") | ||
|
||
span.SetAttributes( | ||
attribute.String("clusterId", clusterID), | ||
attribute.String("ngfVersion", ngfVersion), | ||
) | ||
|
||
// send | ||
span.End() // exits immediately and sends asynchronously | ||
|
||
return nil | ||
} | ||
|
||
func newStdoutExporter() (*stdouttrace.Exporter, error) { | ||
return stdouttrace.New(stdouttrace.WithPrettyPrint()) | ||
} | ||
|
||
func newOTLPExporter( | ||
ctx context.Context, | ||
endpoint string, | ||
secure bool, | ||
headers map[string]string, | ||
) (*otlptrace.Exporter, error) { | ||
options := []otlptracegrpc.Option{ | ||
otlptracegrpc.WithEndpoint(endpoint), | ||
otlptracegrpc.WithHeaders(headers), | ||
} | ||
|
||
if !secure { | ||
options = append(options, otlptracegrpc.WithInsecure()) | ||
} | ||
|
||
traceClient := otlptracegrpc.NewClient(options...) | ||
|
||
return otlptrace.New(ctx, traceClient) | ||
} |
This file contains hidden or 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,19 @@ | ||
# How to Install | ||
|
||
Add repo: | ||
|
||
```text | ||
helm repo add open-telemetry https://open-telemetry.github.io/opentelemetry-helm-charts | ||
``` | ||
|
||
Install: | ||
|
||
```text | ||
helm install my-opentelemetry-collector open-telemetry/opentelemetry-collector -f values.yaml | ||
``` | ||
|
||
Uninstall: | ||
|
||
```text | ||
helm uninstall my-opentelemetry-collector | ||
``` |
This file contains hidden or 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,27 @@ | ||
mode: deployment | ||
replicaCount: 1 | ||
config: | ||
exporters: | ||
debug: | ||
verbosity: detailed | ||
receivers: | ||
jaeger: | ||
prometheus: | ||
zipkin: | ||
otlp: | ||
protocols: | ||
grpc: | ||
endpoint: 0.0.0.0:4317 | ||
service: | ||
telemetry: | ||
extensions: | ||
- health_check | ||
pipelines: | ||
logs: | ||
metrics: | ||
traces: | ||
exporters: | ||
- debug | ||
processors: | ||
receivers: | ||
- otlp |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.