Skip to content

Commit

Permalink
[receiver/k8scluster] Change k8s.cronjob.active_jobs to use mdatagen (#…
Browse files Browse the repository at this point in the history
…24057)

**Description:** Change k8s.cronjob.active_jobs to use mdatagen

**Link to tracking Issue:**
#4367

---------

Co-authored-by: Dmitrii Anoshin <[email protected]>
  • Loading branch information
povilasv and dmitryax authored Jul 17, 2023
1 parent 6ba05da commit 3f548bb
Show file tree
Hide file tree
Showing 13 changed files with 717 additions and 82 deletions.
20 changes: 20 additions & 0 deletions .chloggen/k8s-cluster-receiver-cronjob.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Use this changelog template to create an entry for release notes.
# If your change doesn't affect end users, such as a test fix or a tooling change,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: k8sclusterreceiver

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: "Change k8s.cronjob.active_jobs to use mdatagen"

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [10553]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:
4 changes: 2 additions & 2 deletions receiver/k8sclusterreceiver/internal/collection/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,9 @@ func (dc *DataCollector) SyncMetrics(obj interface{}) {
case *batchv1.Job:
md = jobs.GetMetrics(dc.settings, o)
case *batchv1.CronJob:
md = ocsToMetrics(cronjob.GetMetrics(o))
md = cronjob.GetMetrics(dc.settings, o)
case *batchv1beta1.CronJob:
md = ocsToMetrics(cronjob.GetMetricsBeta(o))
md = cronjob.GetMetricsBeta(dc.settings, o)
case *autoscalingv2.HorizontalPodAutoscaler:
md = hpa.GetMetrics(dc.settings, o)
case *autoscalingv2beta2.HorizontalPodAutoscaler:
Expand Down
89 changes: 26 additions & 63 deletions receiver/k8sclusterreceiver/internal/cronjob/cronjobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@
package cronjob // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sclusterreceiver/internal/cronjob"

import (
agentmetricspb "github.com/census-instrumentation/opencensus-proto/gen-go/agent/metrics/v1"
metricspb "github.com/census-instrumentation/opencensus-proto/gen-go/metrics/v1"
resourcepb "github.com/census-instrumentation/opencensus-proto/gen-go/resource/v1"
conventions "go.opentelemetry.io/collector/semconv/v1.6.1"
"time"

"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/receiver"
batchv1 "k8s.io/api/batch/v1"
batchv1beta1 "k8s.io/api/batch/v1beta1"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/experimentalmetricmetadata"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sclusterreceiver/internal/constants"
imetadataphase "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sclusterreceiver/internal/cronjob/internal/metadata"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sclusterreceiver/internal/metadata"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sclusterreceiver/internal/utils"
)

const (
Expand All @@ -23,71 +24,33 @@ const (
cronJobKeyConcurrencyPolicy = "concurrency_policy"
)

var activeJobs = &metricspb.MetricDescriptor{
Name: "k8s.cronjob.active_jobs",
Description: "The number of actively running jobs for a cronjob",
Unit: "1",
Type: metricspb.MetricDescriptor_GAUGE_INT64,
}

// TODO: All the CronJob related functions below can be de-duplicated using generics from go 1.19
func GetMetrics(set receiver.CreateSettings, cj *batchv1.CronJob) pmetric.Metrics {
mbphase := imetadataphase.NewMetricsBuilder(imetadataphase.DefaultMetricsBuilderConfig(), set)
ts := pcommon.NewTimestampFromTime(time.Now())

func GetMetrics(cj *batchv1.CronJob) []*agentmetricspb.ExportMetricsServiceRequest {
metrics := []*metricspb.Metric{
{
MetricDescriptor: activeJobs,
Timeseries: []*metricspb.TimeSeries{
utils.GetInt64TimeSeries(int64(len(cj.Status.Active))),
},
},
}
mbphase.RecordK8sCronjobActiveJobsDataPoint(ts, int64(len(cj.Status.Active)))

return []*agentmetricspb.ExportMetricsServiceRequest{
{
Resource: getResourceForCronJob(cj),
Metrics: metrics,
},
}
return mbphase.Emit(
imetadataphase.WithK8sNamespaceName(cj.Namespace),
imetadataphase.WithK8sCronjobUID(string(cj.UID)),
imetadataphase.WithK8sCronjobName(cj.Name),
imetadataphase.WithOpencensusResourcetype("k8s"),
)
}

func GetMetricsBeta(cj *batchv1beta1.CronJob) []*agentmetricspb.ExportMetricsServiceRequest {
metrics := []*metricspb.Metric{
{
MetricDescriptor: activeJobs,
Timeseries: []*metricspb.TimeSeries{
utils.GetInt64TimeSeries(int64(len(cj.Status.Active))),
},
},
}
func GetMetricsBeta(set receiver.CreateSettings, cj *batchv1beta1.CronJob) pmetric.Metrics {
mbphase := imetadataphase.NewMetricsBuilder(imetadataphase.DefaultMetricsBuilderConfig(), set)
ts := pcommon.NewTimestampFromTime(time.Now())

return []*agentmetricspb.ExportMetricsServiceRequest{
{
Resource: getResourceForCronJobBeta(cj),
Metrics: metrics,
},
}
}
mbphase.RecordK8sCronjobActiveJobsDataPoint(ts, int64(len(cj.Status.Active)))

func getResourceForCronJob(cj *batchv1.CronJob) *resourcepb.Resource {
return &resourcepb.Resource{
Type: constants.K8sType,
Labels: map[string]string{
conventions.AttributeK8SCronJobUID: string(cj.UID),
conventions.AttributeK8SCronJobName: cj.Name,
conventions.AttributeK8SNamespaceName: cj.Namespace,
},
}
}
return mbphase.Emit(
imetadataphase.WithK8sNamespaceName(cj.Namespace),
imetadataphase.WithK8sCronjobUID(string(cj.UID)),
imetadataphase.WithK8sCronjobName(cj.Name),
imetadataphase.WithOpencensusResourcetype("k8s"),
)

func getResourceForCronJobBeta(cj *batchv1beta1.CronJob) *resourcepb.Resource {
return &resourcepb.Resource{
Type: constants.K8sType,
Labels: map[string]string{
conventions.AttributeK8SCronJobUID: string(cj.UID),
conventions.AttributeK8SCronJobName: cj.Name,
conventions.AttributeK8SNamespaceName: cj.Namespace,
},
}
}

func GetMetadata(cj *batchv1.CronJob) map[experimentalmetricmetadata.ResourceID]*metadata.KubernetesMetadata {
Expand Down
31 changes: 14 additions & 17 deletions receiver/k8sclusterreceiver/internal/cronjob/cronjobs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,35 @@
package cronjob

import (
"path/filepath"
"testing"

metricspb "github.com/census-instrumentation/opencensus-proto/gen-go/metrics/v1"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/receiver/receivertest"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"

"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sclusterreceiver/internal/constants"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/golden"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/pmetrictest"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sclusterreceiver/internal/metadata"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sclusterreceiver/internal/testutils"
)

func TestCronJobMetrics(t *testing.T) {
cj := newCronJob("1")

actualResourceMetrics := GetMetrics(cj)

require.Equal(t, 1, len(actualResourceMetrics))

require.Equal(t, 1, len(actualResourceMetrics[0].Metrics))
testutils.AssertResource(t, actualResourceMetrics[0].Resource, constants.K8sType,
map[string]string{
"k8s.cronjob.uid": "test-cronjob-1-uid",
"k8s.cronjob.name": "test-cronjob-1",
"k8s.namespace.name": "test-namespace",
},
m := GetMetrics(receivertest.NewNopCreateSettings(), cj)
expected, err := golden.ReadMetrics(filepath.Join("testdata", "expected.yaml"))
require.NoError(t, err)
require.NoError(t, pmetrictest.CompareMetrics(expected, m,
pmetrictest.IgnoreTimestamp(),
pmetrictest.IgnoreStartTimestamp(),
pmetrictest.IgnoreResourceMetricsOrder(),
pmetrictest.IgnoreMetricsOrder(),
pmetrictest.IgnoreScopeMetricsOrder(),
),
)

testutils.AssertMetricsInt(t, actualResourceMetrics[0].Metrics[0], "k8s.cronjob.active_jobs",
metricspb.MetricDescriptor_GAUGE_INT64, 2)
}

func TestCronJobMetadata(t *testing.T) {
Expand Down
6 changes: 6 additions & 0 deletions receiver/k8sclusterreceiver/internal/cronjob/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

//go:generate mdatagen metadata.yaml

package cronjob // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sclusterreceiver/internal/cronjob"
33 changes: 33 additions & 0 deletions receiver/k8sclusterreceiver/internal/cronjob/documentation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
[comment]: <> (Code generated by mdatagen. DO NOT EDIT.)

# k8s/cronjob

**Parent Component:** k8s_cluster

## Default Metrics

The following metrics are emitted by default. Each of them can be disabled by applying the following configuration:

```yaml
metrics:
<metric_name>:
enabled: false
```
### k8s.cronjob.active_jobs
The number of actively running jobs for a cronjob
| Unit | Metric Type | Value Type |
| ---- | ----------- | ---------- |
| 1 | Gauge | Int |
## Resource Attributes
| Name | Description | Values | Enabled |
| ---- | ----------- | ------ | ------- |
| k8s.cronjob.name | The k8s CronJob name | Any Str | true |
| k8s.cronjob.uid | The k8s CronJob uid. | Any Str | true |
| k8s.namespace.name | The k8s namespace name | Any Str | true |
| k8s.node.name | The k8s node name | Any Str | true |
| opencensus.resourcetype | The OpenCensus resource type. | Any Str | true |

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 3f548bb

Please sign in to comment.