-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an option to expose Prometheus metrics via http/s server
Signed-off-by: Botond Szirtes <botond.szirtes@est.tech>
- Loading branch information
Showing
3 changed files
with
146 additions
and
1 deletion.
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,35 @@ | ||
// Copyright (c) 2024 Nordix Foundation. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at: | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// Package prometheus provides a set of utilities for assisting with Prometheus data | ||
package prometheus | ||
|
||
import ( | ||
"os" | ||
"strconv" | ||
) | ||
|
||
const ( | ||
prometheusEnv = "PROMETHEUS" | ||
) | ||
|
||
// IsEnabled returns true if prometheus is enabled | ||
func IsEnabled() bool { | ||
if v, err := strconv.ParseBool(os.Getenv(prometheusEnv)); err == nil { | ||
return v | ||
} | ||
return false | ||
} |
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,110 @@ | ||
// Copyright (c) 2024 Nordix Foundation. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at: | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// Package prometheus provides a set of utilities for assisting with Prometheus data | ||
package prometheus | ||
|
||
import ( | ||
"context" | ||
"crypto/tls" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/prometheus/client_golang/prometheus/promhttp" | ||
"github.com/spiffe/go-spiffe/v2/spiffetls/tlsconfig" | ||
"github.com/spiffe/go-spiffe/v2/workloadapi" | ||
|
||
"github.com/networkservicemesh/sdk/pkg/tools/log" | ||
) | ||
|
||
func ListenAndServe(ctx context.Context, listenOn string, headerTimeout time.Duration, cancel context.CancelFunc) { | ||
metricsServer := Server{ | ||
ListenOn: listenOn, | ||
HeaderTimeout: headerTimeout, | ||
} | ||
|
||
tlsConfig := &tls.Config{ | ||
MinVersion: tls.VersionTLS12, | ||
} | ||
source, err := workloadapi.NewX509Source(ctx) | ||
if err != nil { | ||
log.FromContext(ctx).Fatalf("error getting x509 source: %v", err.Error()) | ||
} | ||
tlsConfig.GetCertificate = tlsconfig.GetCertificate(source) | ||
metricsServer.TLSConfig = tlsConfig | ||
|
||
select { | ||
case <-ctx.Done(): | ||
err = source.Close() | ||
log.FromContext(ctx).Errorf("unable to close x509 source: %v", err.Error()) | ||
default: | ||
} | ||
|
||
go func() { | ||
err := metricsServer.Start(ctx) | ||
if err != nil { | ||
log.FromContext(ctx).Error(err.Error()) | ||
cancel() | ||
} | ||
}() | ||
} | ||
|
||
// Server is a server type for handling requests | ||
type Server struct { | ||
TLSConfig *tls.Config | ||
ListenOn string | ||
HeaderTimeout time.Duration | ||
} | ||
|
||
// Start initiates the server to begin handling incoming requests | ||
func (s *Server) Start(ctx context.Context) error { | ||
log.FromContext(ctx).Info("Start metrics server on ", s.ListenOn) | ||
|
||
server := &http.Server{ | ||
Addr: s.ListenOn, | ||
TLSConfig: s.TLSConfig, | ||
ReadHeaderTimeout: s.HeaderTimeout, | ||
} | ||
|
||
http.Handle("/metrics", promhttp.Handler()) | ||
|
||
serverCtx, cancel := context.WithCancel(ctx) | ||
var ListenAndServeErr error | ||
|
||
go func() { | ||
ListenAndServeErr = server.ListenAndServeTLS("", "") | ||
if ListenAndServeErr != nil { | ||
cancel() | ||
} | ||
}() | ||
|
||
<-serverCtx.Done() | ||
|
||
if ListenAndServeErr != nil { | ||
return errors.Errorf("failed to ListenAndServe on metrics server: %s", ListenAndServeErr) | ||
} | ||
|
||
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 1*time.Second) | ||
defer shutdownCancel() | ||
|
||
err := server.Shutdown(shutdownCtx) | ||
if err != nil { | ||
return errors.Errorf("failed to shutdown metrics server: %s", err) | ||
} | ||
|
||
return nil | ||
} |