forked from intel/gprofiler-performance-studio
-
Notifications
You must be signed in to change notification settings - Fork 0
Add metrics publisher to indexer with S3 endpoint configuration #35
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
Open
ashokbytebytego
wants to merge
6
commits into
master
Choose a base branch
from
metrics_publisher_indexer
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,741
β11
Open
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
94f2452
feat: Add metrics publisher to indexer with S3 endpoint configuration
ashokbytebytego c9041de
Improve indexer SLI metrics publishing strategy and SQS event handling
artursarlo 24ad246
Remove LOCAL_TESTING_GUIDE.md per review
ashokbytebytego 9316d78
Address review feedback: move documentation to docs folder
ashokbytebytego 0e306e7
Add LocalStack configuration for local development and testing
ashokbytebytego a0f4fc2
Merge master into metrics_publisher_indexer
ashokbytebytego File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,234 @@ | ||
| // | ||
| // Copyright (C) 2023 Intel Corporation | ||
| // | ||
| // 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 main | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "net" | ||
| "strings" | ||
| "sync" | ||
| "time" | ||
|
|
||
| log "github.com/sirupsen/logrus" | ||
| ) | ||
|
|
||
| // Response type constants for SLI metrics | ||
| const ( | ||
| ResponseTypeSuccess = "success" | ||
| ResponseTypeFailure = "failure" | ||
| ResponseTypeIgnoredFailure = "ignored_failure" | ||
| ) | ||
|
|
||
| // MetricsPublisher handles sending metrics to metrics agent via TCP | ||
| type MetricsPublisher struct { | ||
| host string | ||
| port string | ||
| serviceName string | ||
| sliMetricUUID string | ||
| enabled bool | ||
| connectionFailed bool | ||
| lastErrorLogTime int64 | ||
| errorLogInterval int64 | ||
| mutex sync.Mutex | ||
| } | ||
|
|
||
| var ( | ||
| metricsInstance *MetricsPublisher | ||
| metricsOnce sync.Once | ||
| ) | ||
|
|
||
| // NewMetricsPublisher creates or returns the singleton MetricsPublisher instance | ||
| func NewMetricsPublisher(serverURL, serviceName, sliUUID string, enabled bool) *MetricsPublisher { | ||
| metricsOnce.Do(func() { | ||
| instance := &MetricsPublisher{ | ||
| serviceName: serviceName, | ||
| sliMetricUUID: sliUUID, | ||
| enabled: enabled, | ||
| errorLogInterval: 300, // Log errors at most once every 5 minutes | ||
| } | ||
|
|
||
| // Parse server URL (tcp://host:port) | ||
| if strings.HasPrefix(serverURL, "tcp://") { | ||
| urlParts := strings.Split(serverURL[6:], ":") | ||
| instance.host = urlParts[0] | ||
| if len(urlParts) > 1 { | ||
| instance.port = urlParts[1] | ||
| } else { | ||
| instance.port = "18126" | ||
| } | ||
| } else { | ||
| if enabled { | ||
| log.Fatalf("Unsupported server URL format: %s. Expected tcp://host:port", serverURL) | ||
| } | ||
| instance.host = "localhost" | ||
| instance.port = "18126" | ||
| } | ||
|
|
||
| if enabled { | ||
| log.Infof("MetricsPublisher initialized: service=%s, server=%s:%s, sli_enabled=%t", | ||
| serviceName, instance.host, instance.port, sliUUID != "") | ||
| } else { | ||
| log.Info("MetricsPublisher disabled") | ||
| } | ||
|
|
||
| metricsInstance = instance | ||
| }) | ||
|
|
||
| return metricsInstance | ||
| } | ||
|
|
||
| // GetInstance returns the singleton MetricsPublisher instance | ||
| // Returns nil if not initialized | ||
| func GetMetricsPublisher() *MetricsPublisher { | ||
| return metricsInstance | ||
| } | ||
|
|
||
| // SendSLIMetric sends an SLI metric for tracking HTTP success rate | ||
| // responseType: success, failure, or ignored_failure | ||
| // methodName: The method/operation being tracked (e.g., "event_processing") | ||
| // extraTags: Additional tags as key-value pairs | ||
| func (m *MetricsPublisher) SendSLIMetric(responseType, methodName string, extraTags map[string]string) bool { | ||
| if m == nil || !m.enabled || m.sliMetricUUID == "" { | ||
| return false | ||
| } | ||
|
|
||
| // Build metric name using configured SLI UUID | ||
| metricName := fmt.Sprintf("error-budget.counters.%s", m.sliMetricUUID) | ||
|
|
||
| // Get current epoch timestamp | ||
| timestamp := time.Now().Unix() | ||
|
|
||
| // Build tag string with required SLI tags (Graphite plaintext protocol format) | ||
| tags := []string{ | ||
| fmt.Sprintf("service=%s", m.serviceName), | ||
| fmt.Sprintf("response_type=%s", responseType), | ||
| fmt.Sprintf("method_name=%s", methodName), | ||
| } | ||
|
|
||
| if extraTags != nil { | ||
| for key, value := range extraTags { | ||
| tags = append(tags, fmt.Sprintf("%s=%s", key, value)) | ||
| } | ||
| } | ||
|
|
||
| tagString := strings.Join(tags, " ") | ||
|
|
||
| // Format: put metric_name timestamp value tag1=value1 tag2=value2 ... | ||
| metricLine := fmt.Sprintf("put %s %d 1 %s", metricName, timestamp, tagString) | ||
|
|
||
| log.Infof("π Sending SLI metric: %s", metricLine) | ||
|
|
||
| return m.sendMetric(metricLine) | ||
| } | ||
|
|
||
| // SendErrorMetric sends an operational error metric | ||
| func (m *MetricsPublisher) SendErrorMetric(metricName string, extraTags map[string]string) bool { | ||
| if m == nil || !m.enabled { | ||
| return false | ||
| } | ||
|
|
||
| // Get current epoch timestamp | ||
| timestamp := time.Now().Unix() | ||
|
|
||
| // Build tag string | ||
| tags := []string{ | ||
| fmt.Sprintf("service=%s", m.serviceName), | ||
| } | ||
|
|
||
| if extraTags != nil { | ||
| for key, value := range extraTags { | ||
| tags = append(tags, fmt.Sprintf("%s=%s", key, value)) | ||
| } | ||
| } | ||
|
|
||
| tagString := strings.Join(tags, " ") | ||
|
|
||
| // Format: put metric_name timestamp value tag1=value1 tag2=value2 ... | ||
| metricLine := fmt.Sprintf("put %s %d 1 %s", metricName, timestamp, tagString) | ||
|
|
||
| log.Debugf("π Sending error metric: %s", metricLine) | ||
|
|
||
| return m.sendMetric(metricLine) | ||
| } | ||
|
|
||
| // sendMetric sends a metric line via TCP socket | ||
| func (m *MetricsPublisher) sendMetric(metricLine string) bool { | ||
| if m == nil || !m.enabled { | ||
| return false | ||
| } | ||
|
|
||
| // Check if we should throttle error logging | ||
| m.mutex.Lock() | ||
| now := time.Now().Unix() | ||
| shouldLogError := now-m.lastErrorLogTime >= m.errorLogInterval | ||
| m.mutex.Unlock() | ||
|
|
||
| // Ensure metric line ends with newline | ||
| if !strings.HasSuffix(metricLine, "\n") { | ||
| metricLine = metricLine + "\n" | ||
| } | ||
|
|
||
| // Create TCP connection with timeout | ||
| address := net.JoinHostPort(m.host, m.port) | ||
| conn, err := net.DialTimeout("tcp", address, 1*time.Second) | ||
| if err != nil { | ||
| if shouldLogError { | ||
| log.Warnf("Failed to connect to metrics agent at %s: %v", address, err) | ||
| m.mutex.Lock() | ||
| m.lastErrorLogTime = now | ||
| m.connectionFailed = true | ||
| m.mutex.Unlock() | ||
| } | ||
| return false | ||
| } | ||
| defer conn.Close() | ||
|
|
||
| // Set write timeout | ||
| conn.SetWriteDeadline(time.Now().Add(1 * time.Second)) | ||
|
|
||
| // Send metric | ||
| _, err = conn.Write([]byte(metricLine)) | ||
| if err != nil { | ||
| if shouldLogError { | ||
| log.Warnf("Failed to send metric: %v", err) | ||
| m.mutex.Lock() | ||
| m.lastErrorLogTime = now | ||
| m.mutex.Unlock() | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| // Reset connection failed flag on success | ||
| m.mutex.Lock() | ||
| if m.connectionFailed { | ||
| log.Info("Successfully reconnected to metrics agent") | ||
| m.connectionFailed = false | ||
| } | ||
| m.mutex.Unlock() | ||
|
|
||
| return true | ||
| } | ||
|
|
||
| // FlushAndClose flushes any pending metrics and closes the publisher | ||
| func (m *MetricsPublisher) FlushAndClose() { | ||
| m.mutex.Lock() | ||
| defer m.mutex.Unlock() | ||
|
|
||
| log.Info("MetricsPublisher closed") | ||
| m.enabled = false | ||
| } | ||
|
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can remove this file. Not really needed.