-
Notifications
You must be signed in to change notification settings - Fork 208
Expose piped stat metrics at ops metrics endpoint #2202
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
78d42dd
Expose piped stat metrics
khanhtc1202 21e0fa2
Implement simple build piped stat logic
khanhtc1202 b967765
Add cache address to ops deplopment manifest
khanhtc1202 1847b7e
Rename function
khanhtc1202 b61b053
Using io.NopCloser
khanhtc1202 d448919
Using io.Reader instead
khanhtc1202 6cad0a6
Add builder test
khanhtc1202 2f1a89d
Update testdata
khanhtc1202 028e358
Remove ioutil
khanhtc1202 ac20e18
Using require
khanhtc1202 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
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,26 @@ | ||
| load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") | ||
|
|
||
| go_library( | ||
| name = "go_default_library", | ||
| srcs = ["builder.go"], | ||
| importpath = "github.com/pipe-cd/pipe/pkg/app/ops/pipedstatsbuilder", | ||
| visibility = ["//visibility:public"], | ||
| deps = [ | ||
| "//pkg/cache:go_default_library", | ||
| "@org_uber_go_zap//:go_default_library", | ||
| ], | ||
| ) | ||
|
|
||
| go_test( | ||
| name = "go_default_test", | ||
| size = "small", | ||
| srcs = ["builder_test.go"], | ||
| data = glob(["testdata/**"]), | ||
| embed = [":go_default_library"], | ||
| deps = [ | ||
| "//pkg/cache:go_default_library", | ||
| "@com_github_stretchr_testify//assert:go_default_library", | ||
| "@com_github_stretchr_testify//require:go_default_library", | ||
| "@org_uber_go_zap//:go_default_library", | ||
| ], | ||
| ) |
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,56 @@ | ||
| // Copyright 2021 The PipeCD Authors. | ||
| // | ||
| // 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 pipedstatsbuilder | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "errors" | ||
| "io" | ||
|
|
||
| "go.uber.org/zap" | ||
|
|
||
| "github.com/pipe-cd/pipe/pkg/cache" | ||
| ) | ||
|
|
||
| type PipedStatsBuilder struct { | ||
| backend cache.Cache | ||
| logger *zap.Logger | ||
| } | ||
|
|
||
| func NewPipedStatsBuilder(c cache.Cache, logger *zap.Logger) *PipedStatsBuilder { | ||
| return &PipedStatsBuilder{ | ||
| backend: c, | ||
| logger: logger.Named("piped-metrics-builder"), | ||
| } | ||
| } | ||
|
|
||
| func (b *PipedStatsBuilder) Build() (io.Reader, error) { | ||
| res, err := b.backend.GetAll() | ||
| if err != nil { | ||
| b.logger.Error("failed to fetch piped stats from cache", zap.Error(err)) | ||
| return nil, err | ||
| } | ||
| data := make([][]byte, 0, len(res)) | ||
| for _, v := range res { | ||
| value, okValue := v.([]byte) | ||
| if !okValue { | ||
| err = errors.New("error value not a bulk of string value") | ||
| b.logger.Error("failed to marshal piped stat data", zap.Error(err)) | ||
| return nil, err | ||
| } | ||
| data = append(data, value) | ||
| } | ||
| return bytes.NewReader(bytes.Join(data, []byte("\n"))), nil | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's alright to be later, adding a test for Build() makes it more clear.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, just added a simple test 6cad0a6 👀 |
||
| } | ||
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,65 @@ | ||
| // Copyright 2021 The PipeCD Authors. | ||
| // | ||
| // 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 pipedstatsbuilder | ||
|
|
||
| import ( | ||
| "io" | ||
| "os" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| "go.uber.org/zap" | ||
|
|
||
| "github.com/pipe-cd/pipe/pkg/cache" | ||
| ) | ||
|
|
||
| type mockBuilderBackend struct { | ||
| cache.Cache | ||
| srcs []string | ||
| } | ||
|
|
||
| func newMockBuilderBackend() *mockBuilderBackend { | ||
| return &mockBuilderBackend{ | ||
| srcs: []string{ | ||
| "./testdata/piped_stat_1", | ||
| "./testdata/piped_stat_2", | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func (m *mockBuilderBackend) GetAll() (map[string]interface{}, error) { | ||
| out := make(map[string]interface{}, len(m.srcs)) | ||
| for _, file := range m.srcs { | ||
| data, err := os.ReadFile(file) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| out[file] = data | ||
| } | ||
| return out, nil | ||
| } | ||
|
|
||
| func TestBuildPipedStat(t *testing.T) { | ||
| builder := NewPipedStatsBuilder(newMockBuilderBackend(), zap.NewNop()) | ||
| rc, err := builder.Build() | ||
| require.NoError(t, err) | ||
| require.NotNil(t, rc) | ||
| buf := new(strings.Builder) | ||
| io.Copy(buf, rc) | ||
| data, _ := os.ReadFile("./testdata/expected") | ||
| assert.Equal(t, string(data), buf.String()) | ||
| } |
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.
Ops' deployment need this too.
https://github.com/pipe-cd/pipe/blob/master/manifests/pipecd/templates/deployment.yaml#L96
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.
Nice catch 👍
Address by b967765 🙏