Skip to content

Commit f73c10a

Browse files
committed
cleanup
- constify conext shutdonw timeout - restore header - remove unnecessary metric attribute on exporter setup - restore test coverage from rebase conflict
1 parent 49f9bcd commit f73c10a

File tree

17 files changed

+34
-6905
lines changed

17 files changed

+34
-6905
lines changed

cmd/hydration-controller/main.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"flag"
2020
"os"
2121
"strings"
22-
"time"
2322

2423
"github.com/GoogleContainerTools/config-sync/pkg/api/configsync"
2524
"github.com/GoogleContainerTools/config-sync/pkg/hydrate"
@@ -93,7 +92,7 @@ func main() {
9392
}
9493

9594
defer func() {
96-
shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
95+
shutdownCtx, cancel := context.WithTimeout(context.Background(), kmetrics.ShutdownTimeout)
9796
defer cancel()
9897
if err := oce.Shutdown(shutdownCtx); err != nil {
9998
klog.Fatalf("Unable to stop the OC Agent exporter: %v", err)

cmd/reconciler-manager/main.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"fmt"
2121
"net/http"
2222
"os"
23-
"time"
2423

2524
traceapi "cloud.google.com/go/trace/apiv2"
2625
"github.com/GoogleContainerTools/config-sync/pkg/api/configsync"
@@ -197,7 +196,7 @@ func main() {
197196
}
198197

199198
defer func() {
200-
shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
199+
shutdownCtx, cancel := context.WithTimeout(context.Background(), metrics.ShutdownTimeout)
201200
defer cancel()
202201
if err := oce.Shutdown(shutdownCtx); err != nil {
203202
setupLog.Error(err, "failed to stop the OC Agent exporter")
@@ -210,7 +209,7 @@ func main() {
210209
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
211210
setupLog.Error(err, "problem running manager")
212211
// os.Exit(1) does not run deferred functions so explicitly stopping the OC Agent exporter.
213-
shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
212+
shutdownCtx, cancel := context.WithTimeout(context.Background(), metrics.ShutdownTimeout)
214213
defer cancel()
215214
if err := oce.Shutdown(shutdownCtx); err != nil {
216215
setupLog.Error(err, "failed to stop the OC Agent exporter")

cmd/reconciler/main.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"fmt"
2121
"os"
2222
"strings"
23-
"time"
2423

2524
"github.com/GoogleContainerTools/config-sync/pkg/api/configsync"
2625
"github.com/GoogleContainerTools/config-sync/pkg/declared"
@@ -155,7 +154,7 @@ func main() {
155154
}
156155

157156
defer func() {
158-
shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
157+
shutdownCtx, cancel := context.WithTimeout(context.Background(), ocmetrics.ShutdownTimeout)
159158
defer cancel()
160159
if err := oce.Shutdown(shutdownCtx); err != nil {
161160
klog.Fatalf("Unable to stop the OC Agent exporter: %v", err)

pkg/kmetrics/record.go

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
/*
2-
Copyright 2021 Google LLC.
3-
4-
Licensed under the Apache License, Version 2.0 (the "License");
5-
you may not use this file except in compliance with the License.
6-
You may obtain a copy of the License at
7-
8-
http://www.apache.org/licenses/LICENSE-2.0
9-
10-
Unless required by applicable law or agreed to in writing, software
11-
distributed under the License is distributed on an "AS IS" BASIS,
12-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
See the License for the specific language governing permissions and
14-
limitations under the License.
15-
*/
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
1614

1715
package kmetrics
1816

pkg/kmetrics/register.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,17 @@ package kmetrics
1717
import (
1818
"context"
1919
"os"
20+
"time"
2021

2122
"go.opentelemetry.io/otel"
2223
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc"
2324
"go.opentelemetry.io/otel/sdk/metric"
2425
"go.opentelemetry.io/otel/sdk/resource"
25-
semconv "go.opentelemetry.io/otel/semconv/v1.21.0"
26+
)
27+
28+
const (
29+
// ShutdownTimeout is the timeout for shutting down the Otel exporter
30+
ShutdownTimeout = 5 * time.Second
2631
)
2732

2833
// RegisterOTelExporter creates the OTLP metrics exporter.
@@ -38,10 +43,6 @@ func RegisterOTelExporter(ctx context.Context, containerName string) (*otlpmetri
3843
res, err := resource.New(
3944
ctx,
4045
resource.WithFromEnv(),
41-
resource.WithAttributes(
42-
semconv.ServiceNameKey.String("config-sync-kmetric"),
43-
semconv.ServiceVersionKey.String("1.0.0"),
44-
),
4546
)
4647
if err != nil {
4748
return nil, err

pkg/metrics/register.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,20 @@ package metrics
1717
import (
1818
"context"
1919
"os"
20+
"time"
2021

2122
"go.opentelemetry.io/otel"
2223
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc"
2324
"go.opentelemetry.io/otel/sdk/metric"
2425
"go.opentelemetry.io/otel/sdk/resource"
25-
semconv "go.opentelemetry.io/otel/semconv/v1.21.0"
2626
"k8s.io/klog/v2"
2727
)
2828

29+
const (
30+
// ShutdownTimeout is the timeout for shutting down the Otel exporter
31+
ShutdownTimeout = 5 * time.Second
32+
)
33+
2934
// RegisterOTelExporter creates the OTLP metrics exporter.
3035
func RegisterOTelExporter(ctx context.Context, containerName string) (*otlpmetricgrpc.Exporter, error) {
3136

@@ -40,10 +45,6 @@ func RegisterOTelExporter(ctx context.Context, containerName string) (*otlpmetri
4045
res, err := resource.New(
4146
ctx,
4247
resource.WithFromEnv(),
43-
resource.WithAttributes(
44-
semconv.ServiceNameKey.String("config-sync"),
45-
semconv.ServiceVersionKey.String("1.0.0"),
46-
),
4748
)
4849
if err != nil {
4950
klog.V(5).ErrorS(err, "METRIC DEBUG: Failed to create resource")

pkg/resourcegroup/controllers/metrics/register.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc"
2323
"go.opentelemetry.io/otel/sdk/metric"
2424
"go.opentelemetry.io/otel/sdk/resource"
25-
semconv "go.opentelemetry.io/otel/semconv/v1.21.0"
2625
)
2726

2827
// RegisterOTelExporter creates the OTLP metrics exporter.
@@ -38,10 +37,6 @@ func RegisterOTelExporter(ctx context.Context) (*otlpmetricgrpc.Exporter, error)
3837
res, err := resource.New(
3938
ctx,
4039
resource.WithFromEnv(),
41-
resource.WithAttributes(
42-
semconv.ServiceNameKey.String("config-sync-resourcegroup"),
43-
semconv.ServiceVersionKey.String("1.0.0"),
44-
),
4540
)
4641
if err != nil {
4742
return nil, err

pkg/resourcegroup/controllers/resourcemap/update_metrics_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,11 @@ func TestResourceMapUpdateMetrics(t *testing.T) {
252252
if diff := exporter.ValidateMetrics(expected); diff != "" {
253253
t.Errorf("Unexpected metrics recorded: %v", diff)
254254
}
255+
256+
// Verify the resource map state
257+
if len(m.resgroupToResources) != tc.expectedResourceGroups {
258+
t.Errorf("Expected %d resource groups in map, got %d", tc.expectedResourceGroups, len(m.resgroupToResources))
259+
}
255260
})
256261
}
257262
}

vendor/go.opentelemetry.io/otel/semconv/v1.21.0/README.md

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)