-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Add OpenTelemetry tracing to minikube #9723
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0f23913
Tracing compiles
c4dd42a
add --trace flag
9507e80
add doc around telemetry
63e5021
Merge branch 'master' of https://github.com/kubernetes/minikube into …
3756707
address review comments
5e13824
update docs
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 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
Large diffs are not rendered by default.
Oops, something went wrong.
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,104 @@ | ||
/* | ||
Copyright 2020 The Kubernetes Authors All rights reserved. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package trace | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
|
||
"go.opentelemetry.io/otel/api/trace" | ||
sdktrace "go.opentelemetry.io/otel/sdk/trace" | ||
"k8s.io/klog" | ||
|
||
texporter "github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/trace" | ||
"github.com/pkg/errors" | ||
"go.opentelemetry.io/otel/api/global" | ||
) | ||
|
||
const ( | ||
projectEnvVar = "MINIKUBE_GCP_PROJECT_ID" | ||
// this is the name of the parent span to help identify it | ||
// in the Cloud Trace UI. | ||
parentSpanName = "minikube start" | ||
priyawadhwa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
|
||
type gcpTracer struct { | ||
projectID string | ||
parentCtx context.Context | ||
trace.Tracer | ||
spans map[string]trace.Span | ||
cleanup func() | ||
} | ||
|
||
// StartSpan starts a span for the next step of | ||
// `minikube start` via the GCP tracer | ||
func (t *gcpTracer) StartSpan(name string) { | ||
priyawadhwa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
_, span := t.Tracer.Start(t.parentCtx, name) | ||
t.spans[name] = span | ||
} | ||
|
||
// EndSpan ends the most recent span, indicating | ||
// that one step of `minikube start` has completed | ||
func (t *gcpTracer) EndSpan(name string) { | ||
span, ok := t.spans[name] | ||
if !ok { | ||
klog.Warningf("cannot end span %s as it was never started", name) | ||
return | ||
} | ||
span.End() | ||
} | ||
|
||
func (t *gcpTracer) Cleanup() { | ||
span, ok := t.spans[parentSpanName] | ||
if ok { | ||
span.End() | ||
} | ||
t.cleanup() | ||
} | ||
|
||
func initGCPTracer() (*gcpTracer, error) { | ||
projectID := os.Getenv(projectEnvVar) | ||
if projectID == "" { | ||
return nil, fmt.Errorf("GCP tracer requires a valid GCP project id set via the %s env variable", projectEnvVar) | ||
} | ||
|
||
_, flush, err := texporter.InstallNewPipeline( | ||
[]texporter.Option{ | ||
texporter.WithProjectID(projectID), | ||
}, | ||
sdktrace.WithConfig(sdktrace.Config{ | ||
DefaultSampler: sdktrace.AlwaysSample(), | ||
}), | ||
) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "installing pipeline") | ||
} | ||
|
||
t := global.Tracer(parentSpanName) | ||
|
||
ctx, span := t.Start(context.Background(), parentSpanName) | ||
return &gcpTracer{ | ||
projectID: projectID, | ||
parentCtx: ctx, | ||
cleanup: flush, | ||
Tracer: t, | ||
spans: map[string]trace.Span{ | ||
parentSpanName: span, | ||
}, | ||
}, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
Copyright 2020 The Kubernetes Authors All rights reserved. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package trace | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
var ( | ||
tracer minikubeTracer | ||
) | ||
|
||
type minikubeTracer interface { | ||
StartSpan(string) | ||
EndSpan(string) | ||
Cleanup() | ||
} | ||
|
||
// Initialize intializes the global tracer variable | ||
func Initialize(t string) error { | ||
tr, err := getTracer(t) | ||
if err != nil { | ||
return errors.Wrap(err, "getting tracer") | ||
} | ||
tracer = tr | ||
return nil | ||
} | ||
|
||
func getTracer(t string) (minikubeTracer, error) { | ||
switch t { | ||
case "gcp": | ||
return initGCPTracer() | ||
case "": | ||
return nil, nil | ||
} | ||
return nil, fmt.Errorf("%s is not a valid tracer, valid tracers include: [gcp]", t) | ||
} | ||
|
||
// StartSpan starts a span with the given name | ||
func StartSpan(name string) { | ||
if tracer == nil { | ||
return | ||
} | ||
tracer.StartSpan(name) | ||
} | ||
|
||
// EndSpan ends a span with the given name | ||
func EndSpan(name string) { | ||
if tracer == nil { | ||
return | ||
} | ||
tracer.EndSpan(name) | ||
} | ||
|
||
// Cleanup is responsible for trace related cleanup, | ||
// such as flushing all data | ||
func Cleanup() { | ||
if tracer == nil { | ||
return | ||
} | ||
tracer.Cleanup() | ||
} |
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,24 @@ | ||
--- | ||
title: "Telemetry" | ||
linkTitle: "telemetry" | ||
weight: 1 | ||
date: 2020-11-24 | ||
--- | ||
|
||
## Overview | ||
|
||
minikube provides telemetry suppport via [OpenTelemetry tracing](https://opentelemetry.io/about/) to collect trace data for `minikube start`. | ||
|
||
Currently, minikube supports the following exporters for tracing data: | ||
- [Stackdriver](https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/master/exporter/stackdriverexporter) | ||
|
||
To collect trace data with minikube and the Stackdriver exporter, run: | ||
|
||
``` | ||
MINIKUBE_GCP_PROJECT_ID=<project ID> minikube start --output json --trace gcp | ||
``` | ||
|
||
## Contributing | ||
There are many exporters available via [OpenTelemetry community contributions](https://github.com/open-telemetry/opentelemetry-collector-contrib). | ||
|
||
If you would like to see additional exporters, please create an [issue](https://github.com/kubernetes/minikube/issues) or refer to our [contribution][https://minikube.sigs.k8s.io/docs/contrib/] guidelines and submit a pull request. Thank you! |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how about adding a check in the validate flags that if the option is not gcp, yell at them and say the valid options are ...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added the valid options to the error message that will be displayed, this is what it would look like: