Skip to content
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

frontend: set default otel resource attributes #1184

Merged
merged 2 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 5 additions & 20 deletions frontend/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"net/http"
"os"
"os/signal"
"runtime/debug"
"syscall"

"github.com/Azure/azure-sdk-for-go/sdk/azcore"
Expand All @@ -19,8 +18,8 @@ import (
"github.com/spf13/cobra"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"

"github.com/Azure/ARO-HCP/frontend/pkg/config"
"github.com/Azure/ARO-HCP/frontend/pkg/frontend"
"github.com/Azure/ARO-HCP/frontend/pkg/util"
"github.com/Azure/ARO-HCP/internal/api"
"github.com/Azure/ARO-HCP/internal/api/arm"
"github.com/Azure/ARO-HCP/internal/database"
Expand All @@ -46,7 +45,7 @@ func NewRootCmd() *cobra.Command {
opts := &FrontendOpts{}
rootCmd := &cobra.Command{
Use: "aro-hcp-frontend",
Version: version(),
Version: util.Version(),
Args: cobra.NoArgs,
Short: "Serve the ARO HCP Frontend",
Long: `Serve the ARO HCP Frontend
Expand Down Expand Up @@ -102,8 +101,8 @@ func correlationIDPolicy(req *policy.Request) (*http.Response, error) {
}

func (opts *FrontendOpts) Run() error {
logger := config.DefaultLogger()
logger.Info(fmt.Sprintf("%s (%s) started", frontend.ProgramName, version()))
logger := util.DefaultLogger()
logger.Info(fmt.Sprintf("%s (%s) started", frontend.ProgramName, util.Version()))

// Init the Prometheus emitter.
prometheusEmitter := frontend.NewPrometheusEmitter(prometheus.DefaultRegisterer)
Expand Down Expand Up @@ -177,21 +176,7 @@ func (opts *FrontendOpts) Run() error {
close(stop)

f.Join()
logger.Info(fmt.Sprintf("%s (%s) stopped", frontend.ProgramName, version()))
logger.Info(fmt.Sprintf("%s (%s) stopped", frontend.ProgramName, util.Version()))

return nil
}

func version() string {
version := "unknown"
if info, ok := debug.ReadBuildInfo(); ok {
for _, setting := range info.Settings {
if setting.Key == "vcs.revision" {
version = setting.Value
break
}
}
}

return version
}
4 changes: 2 additions & 2 deletions frontend/pkg/frontend/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

azcorearm "github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"

"github.com/Azure/ARO-HCP/frontend/pkg/config"
"github.com/Azure/ARO-HCP/frontend/pkg/util"
"github.com/Azure/ARO-HCP/internal/api"
"github.com/Azure/ARO-HCP/internal/api/arm"
"github.com/Azure/ARO-HCP/internal/database"
Expand Down Expand Up @@ -80,7 +80,7 @@ func LoggerFromContext(ctx context.Context) *slog.Logger {
}
// Return the default logger as a fail-safe, but log
// the failure to obtain the logger from the context.
logger = config.DefaultLogger()
logger = util.DefaultLogger()
logger.Error(err.Error())
}
return logger
Expand Down
4 changes: 2 additions & 2 deletions frontend/pkg/frontend/middleware_logging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

"github.com/google/uuid"

"github.com/Azure/ARO-HCP/frontend/pkg/config"
"github.com/Azure/ARO-HCP/frontend/pkg/util"
"github.com/Azure/ARO-HCP/internal/api"
"github.com/Azure/ARO-HCP/internal/api/arm"
)
Expand Down Expand Up @@ -45,7 +45,7 @@ func TestMiddlewareLoggingPostMux(t *testing.T) {
request.Header = tt.header

// we assume the request carries a logger, we set it explicitly to not fail
ctx := ContextWithLogger(request.Context(), config.DefaultLogger())
ctx := ContextWithLogger(request.Context(), util.DefaultLogger())
request = request.WithContext(ctx)

next := func(w http.ResponseWriter, r *http.Request) {
Expand Down
7 changes: 7 additions & 0 deletions frontend/pkg/frontend/otel_sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import (
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/sdk/trace"
tracesdk "go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.26.0"

"github.com/Azure/ARO-HCP/frontend/pkg/util"

"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/sdk/resource"
Expand All @@ -39,6 +42,10 @@ func InstallOpenTelemetryTracer(ctx context.Context, logger *slog.Logger, resour
if len(resourceAttrs) > 0 {
opts = append(opts, resource.WithAttributes(resourceAttrs...))
}
opts = append(opts, resource.WithAttributes(
semconv.ServiceNameKey.String(ProgramName),
semconv.ServiceVersionKey.String(util.Version()),
))
resources, err := resource.New(ctx, opts...)
if err != nil {
return nil, fmt.Errorf("failed to initialise trace resources: %w", err)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
package config
package util

// Copyright (c) Microsoft Corporation.
// Licensed under the Apache License 2.0.

import (
"log/slog"
Expand Down
20 changes: 20 additions & 0 deletions frontend/pkg/util/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package util

// Copyright (c) Microsoft Corporation.
// Licensed under the Apache License 2.0.

import "runtime/debug"

func Version() string {
version := "unknown"
if info, ok := debug.ReadBuildInfo(); ok {
for _, setting := range info.Settings {
if setting.Key == "vcs.revision" {
version = setting.Value
break
}
}
}

return version
}
Loading