-
Notifications
You must be signed in to change notification settings - Fork 344
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Configure sampling strategies (#139)
* Configure sampling strategies Signed-off-by: Gary Brown <[email protected]> * Add example sampling config Signed-off-by: Gary Brown <[email protected]> * Move sampling example to separate file, and add text to README.adoc Signed-off-by: Gary Brown <[email protected]> * Fix production test Signed-off-by: Gary Brown <[email protected]> * Update following review comments Signed-off-by: Gary Brown <[email protected]> * Further test fix Signed-off-by: Gary Brown <[email protected]>
- Loading branch information
1 parent
c82a7c8
commit 9c9f809
Showing
15 changed files
with
298 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
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,11 @@ | ||
apiVersion: io.jaegertracing/v1alpha1 | ||
kind: Jaeger | ||
metadata: | ||
name: with-sampling | ||
spec: | ||
strategy: allInOne | ||
sampling: | ||
options: | ||
default_strategy: | ||
type: probabilistic | ||
param: 50 |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package sampling | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/sirupsen/logrus" | ||
"k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
||
"github.com/jaegertracing/jaeger-operator/pkg/apis/io/v1alpha1" | ||
) | ||
|
||
const ( | ||
defaultSamplingStrategy = "{\"default_strategy\":{\"param\":1,\"type\":\"probabilistic\"}}" | ||
) | ||
|
||
// Config represents a sampling configmap | ||
type Config struct { | ||
jaeger *v1alpha1.Jaeger | ||
} | ||
|
||
// NewConfig builds a new Config struct based on the given spec | ||
func NewConfig(jaeger *v1alpha1.Jaeger) *Config { | ||
return &Config{jaeger: jaeger} | ||
} | ||
|
||
// Get returns a configmap specification for the current instance | ||
func (u *Config) Get() *v1.ConfigMap { | ||
var jsonObject []byte | ||
var err error | ||
|
||
// Check for empty map | ||
if u.jaeger.Spec.Sampling.Options.IsEmpty() { | ||
jsonObject = []byte(defaultSamplingStrategy) | ||
} else { | ||
jsonObject, err = u.jaeger.Spec.Sampling.Options.MarshalJSON() | ||
} | ||
|
||
if err != nil { | ||
return nil | ||
} | ||
|
||
logrus.WithField("instance", u.jaeger.Name).Debug("Assembling the Sampling configmap") | ||
trueVar := true | ||
|
||
data := map[string]string{ | ||
"sampling": string(jsonObject), | ||
} | ||
|
||
return &v1.ConfigMap{ | ||
TypeMeta: metav1.TypeMeta{ | ||
APIVersion: "v1", | ||
Kind: "ConfigMap", | ||
}, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: fmt.Sprintf("%s-sampling-configuration", u.jaeger.Name), | ||
Namespace: u.jaeger.Namespace, | ||
OwnerReferences: []metav1.OwnerReference{ | ||
metav1.OwnerReference{ | ||
APIVersion: u.jaeger.APIVersion, | ||
Kind: u.jaeger.Kind, | ||
Name: u.jaeger.Name, | ||
UID: u.jaeger.UID, | ||
Controller: &trueVar, | ||
}, | ||
}, | ||
}, | ||
Data: data, | ||
} | ||
} | ||
|
||
// Update will modify the supplied common spec and options to include | ||
// support for the Sampling configmap. | ||
func Update(jaeger *v1alpha1.Jaeger, commonSpec *v1alpha1.JaegerCommonSpec, options *[]string) { | ||
|
||
volume := v1.Volume{ | ||
Name: fmt.Sprintf("%s-sampling-configuration-volume", jaeger.Name), | ||
VolumeSource: v1.VolumeSource{ | ||
ConfigMap: &v1.ConfigMapVolumeSource{ | ||
LocalObjectReference: v1.LocalObjectReference{ | ||
Name: fmt.Sprintf("%s-sampling-configuration", jaeger.Name), | ||
}, | ||
Items: []v1.KeyToPath{ | ||
v1.KeyToPath{ | ||
Key: "sampling", | ||
Path: "sampling.json", | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
volumeMount := v1.VolumeMount{ | ||
Name: fmt.Sprintf("%s-sampling-configuration-volume", jaeger.Name), | ||
MountPath: "/etc/jaeger/sampling", | ||
ReadOnly: true, | ||
} | ||
commonSpec.Volumes = append(commonSpec.Volumes, volume) | ||
commonSpec.VolumeMounts = append(commonSpec.VolumeMounts, volumeMount) | ||
*options = append(*options, "--sampling.strategies-file=/etc/jaeger/sampling/sampling.json") | ||
} |
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,81 @@ | ||
package sampling | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/jaegertracing/jaeger-operator/pkg/apis/io/v1alpha1" | ||
) | ||
|
||
func TestNoSamplingConfig(t *testing.T) { | ||
jaeger := v1alpha1.NewJaeger("TestNoSamplingConfig") | ||
|
||
config := NewConfig(jaeger) | ||
cm := config.Get() | ||
assert.NotNil(t, cm) | ||
assert.Equal(t, defaultSamplingStrategy, cm.Data["sampling"]) | ||
} | ||
|
||
func TestWithEmptySamplingConfig(t *testing.T) { | ||
uiconfig := v1alpha1.NewFreeForm(map[string]interface{}{}) | ||
jaeger := v1alpha1.NewJaeger("TestWithEmptySamplingConfig") | ||
jaeger.Spec.UI.Options = uiconfig | ||
|
||
config := NewConfig(jaeger) | ||
cm := config.Get() | ||
assert.NotNil(t, cm) | ||
assert.Equal(t, defaultSamplingStrategy, cm.Data["sampling"]) | ||
} | ||
|
||
func TestWithSamplingConfig(t *testing.T) { | ||
samplingconfig := v1alpha1.NewFreeForm(map[string]interface{}{ | ||
"default_strategy": map[string]interface{}{ | ||
"type": "probabilistic", | ||
"param": "20", | ||
}, | ||
}) | ||
json := `{"default_strategy":{"param":"20","type":"probabilistic"}}` | ||
jaeger := v1alpha1.NewJaeger("TestWithSamplingConfig") | ||
jaeger.Spec.Sampling.Options = samplingconfig | ||
|
||
config := NewConfig(jaeger) | ||
cm := config.Get() | ||
assert.Equal(t, json, cm.Data["sampling"]) | ||
} | ||
|
||
func TestUpdateNoSamplingConfig(t *testing.T) { | ||
jaeger := v1alpha1.NewJaeger("TestUpdateNoSamplingConfig") | ||
|
||
commonSpec := v1alpha1.JaegerCommonSpec{} | ||
options := []string{} | ||
|
||
Update(jaeger, &commonSpec, &options) | ||
assert.Len(t, commonSpec.Volumes, 1) | ||
assert.Equal(t, "TestUpdateNoSamplingConfig-sampling-configuration-volume", commonSpec.Volumes[0].Name) | ||
assert.Len(t, commonSpec.VolumeMounts, 1) | ||
assert.Equal(t, "TestUpdateNoSamplingConfig-sampling-configuration-volume", commonSpec.VolumeMounts[0].Name) | ||
assert.Len(t, options, 1) | ||
assert.Equal(t, "--sampling.strategies-file=/etc/jaeger/sampling/sampling.json", options[0]) | ||
} | ||
|
||
func TestUpdateWithSamplingConfig(t *testing.T) { | ||
uiconfig := v1alpha1.NewFreeForm(map[string]interface{}{ | ||
"tracking": map[string]interface{}{ | ||
"gaID": "UA-000000-2", | ||
}, | ||
}) | ||
jaeger := v1alpha1.NewJaeger("TestUpdateWithSamplingConfig") | ||
jaeger.Spec.UI.Options = uiconfig | ||
|
||
commonSpec := v1alpha1.JaegerCommonSpec{} | ||
options := []string{} | ||
|
||
Update(jaeger, &commonSpec, &options) | ||
assert.Len(t, commonSpec.Volumes, 1) | ||
assert.Equal(t, "TestUpdateWithSamplingConfig-sampling-configuration-volume", commonSpec.Volumes[0].Name) | ||
assert.Len(t, commonSpec.VolumeMounts, 1) | ||
assert.Equal(t, "TestUpdateWithSamplingConfig-sampling-configuration-volume", commonSpec.VolumeMounts[0].Name) | ||
assert.Len(t, options, 1) | ||
assert.Equal(t, "--sampling.strategies-file=/etc/jaeger/sampling/sampling.json", options[0]) | ||
} |
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
Oops, something went wrong.