forked from percona/pmm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PMM-12463 Add telemetry envvar datasource (percona#2532)
* PMM-12463 allow ireturn on DataSources * PMM-12463 fix some command hints * PMM-12463 add ENV_VAR dataSource * PMM-12463 add a provisionary envvar metrics extraction * PMM-12463 add a test for EnvVar datasource * PMM-12463 revert staticckeck linter * PMM-12463 fix linter warnings * PMM-12463 revert changes to main.go * PMM-12463 refactor the output format * PMM-12463 add a transform to strip values * PMM-12463 fix linter warnings * PMM-12463 refactor transform, add tests * PMM-12463 run format * PMM-12463 follow up on review comments * PMM-12185 remove the debug statement * PMM-12185 don't be too noisy when a ds is not initialized
- Loading branch information
Showing
17 changed files
with
500 additions
and
77 deletions.
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
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
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
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,84 @@ | ||
// Copyright (C) 2023 Percona LLC | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Affero General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Affero General Public License | ||
// along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
// Package telemetry provides telemetry functionality. | ||
package telemetry | ||
|
||
import ( | ||
"context" | ||
"os" | ||
|
||
pmmv1 "github.com/percona-platform/saas/gen/telemetry/events/pmm" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
type dsEnvvars struct { | ||
l *logrus.Entry | ||
config DSConfigEnvVars | ||
} | ||
|
||
// check interfaces. | ||
var ( | ||
_ DataSource = (*dsEnvvars)(nil) | ||
) | ||
|
||
// NewDataSourceEnvVars makes a new data source for collecting envvars. | ||
func NewDataSourceEnvVars(config DSConfigEnvVars, l *logrus.Entry) DataSource { | ||
return &dsEnvvars{ | ||
l: l, | ||
config: config, | ||
} | ||
} | ||
|
||
// Enabled flag that determines if data source is enabled. | ||
func (d *dsEnvvars) Enabled() bool { | ||
return d.config.Enabled | ||
} | ||
|
||
func (d *dsEnvvars) Init(_ context.Context) error { | ||
return nil | ||
} | ||
|
||
func (d *dsEnvvars) FetchMetrics(_ context.Context, config Config) ([]*pmmv1.ServerMetric_Metric, error) { | ||
var metrics []*pmmv1.ServerMetric_Metric | ||
|
||
check := make(map[string]bool, len(config.Data)) | ||
|
||
for _, col := range config.Data { | ||
if col.Column == "" { | ||
d.l.Warnf("no column defined or empty column name in config %s", config.ID) | ||
continue | ||
} | ||
if value, ok := os.LookupEnv(col.Column); ok && value != "" { | ||
if _, alreadyHasItem := check[col.MetricName]; alreadyHasItem { | ||
d.l.Warnf("repeated metric key %s found in config %s, the last will win", col.MetricName, config.ID) | ||
continue | ||
} | ||
|
||
check[col.MetricName] = true | ||
|
||
metrics = append(metrics, &pmmv1.ServerMetric_Metric{ | ||
Key: col.MetricName, | ||
Value: value, | ||
}) | ||
} | ||
} | ||
|
||
return metrics, nil | ||
} | ||
|
||
func (d *dsEnvvars) Dispose(_ context.Context) error { | ||
return nil | ||
} |
Oops, something went wrong.