Skip to content

Commit

Permalink
Add managed by metric (#1831)
Browse files Browse the repository at this point in the history
Signed-off-by: Ruben Vargas <[email protected]>
  • Loading branch information
rubenvp8510 authored Mar 30, 2022
1 parent f904949 commit cb9bf02
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
17 changes: 17 additions & 0 deletions pkg/metrics/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ const agentStrategiesMetric = "agent_strategies"
const storageMetric = "storage_types"
const strategiesMetric = "strategies"
const autoprovisioningMetric = "autoprovisioning"
const managedMetric = "managed"
const managedByLabel = "app.kubernetes.io/managed-by"

// This structure contains the labels associated with the instances and a counter of the number of instances
type instancesView struct {
Expand Down Expand Up @@ -134,6 +136,21 @@ func (i *instancesMetric) Setup(ctx context.Context) error {
}
i.observations = append(i.observations, obs)

obs, err = newObservation(batch, managedMetric,
"Instances managed by other tool",
"tool",
func(jaeger v1.Jaeger) string {
managed, hasManagement := jaeger.Labels[managedByLabel]
if !hasManagement {
return "none"
}
return managed
})
if err != nil {
return err
}
i.observations = append(i.observations, obs)

return nil
}

Expand Down
65 changes: 65 additions & 0 deletions pkg/metrics/instances_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,68 @@ func TestAutoProvisioningESObservedMetric(t *testing.T) {
assertLabelAndValues(t, expectedMetric.name, meter.MeasurementBatches, expectedMetric.labels, expectedMetric.value)

}

func TestManagerByMetric(t *testing.T) {
s := scheme.Scheme
s.AddKnownTypes(v1.GroupVersion, &v1.Jaeger{}, &v1.JaegerList{})

managed := v1.Jaeger{
ObjectMeta: metav1.ObjectMeta{
Name: "jaeger-managed",
Namespace: "ns",
Labels: map[string]string{
managedByLabel: "maistra-istio-operator",
},
},
Spec: v1.JaegerSpec{
Strategy: "allInOne",
Storage: v1.JaegerStorageSpec{
Type: v1.JaegerMemoryStorage,
},
},
}

nonManaged := v1.Jaeger{
ObjectMeta: metav1.ObjectMeta{
Name: "jaeger-no-managed",
Namespace: "ns",
},
Spec: v1.JaegerSpec{
Strategy: "allInOne",
Storage: v1.JaegerStorageSpec{
Type: v1.JaegerMemoryStorage,
},
},
}

objs := []runtime.Object{
&managed,
&nonManaged,
}

cl := fake.NewFakeClientWithScheme(s, objs...)
meter, provider := oteltest.NewMeterProvider()
global.SetMeterProvider(provider)

instancesObservedValue := newInstancesMetric(cl)
err := instancesObservedValue.Setup(context.Background())
require.NoError(t, err)
meter.RunAsyncInstruments()

expectedMetric := newExpectedMetric(managedMetric, attribute.String("tool", "maistra-istio-operator"), 1)
assertLabelAndValues(t, expectedMetric.name, meter.MeasurementBatches, expectedMetric.labels, expectedMetric.value)

expectedMetric = newExpectedMetric(managedMetric, attribute.String("tool", "none"), 1)
assertLabelAndValues(t, expectedMetric.name, meter.MeasurementBatches, expectedMetric.labels, expectedMetric.value)

// Test deleting managed
err = cl.Delete(context.Background(), &managed)
require.NoError(t, err)

// Reset measurement batches
meter.MeasurementBatches = []oteltest.Batch{}
meter.RunAsyncInstruments()

expectedMetric = newExpectedMetric(managedMetric, attribute.String("tool", "maistra-istio-operator"), 0)
assertLabelAndValues(t, expectedMetric.name, meter.MeasurementBatches, expectedMetric.labels, expectedMetric.value)
}

0 comments on commit cb9bf02

Please sign in to comment.