-
Notifications
You must be signed in to change notification settings - Fork 715
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add skywalking metrics analysis provider
Signed-off-by: kezhenxu94 <kezhenxu94@apache.org>
- Loading branch information
1 parent
89b0487
commit c3a75f5
Showing
9 changed files
with
304 additions
and
8 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
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,136 @@ | ||
/* | ||
Copyright 2020 The Flux 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 providers | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"time" | ||
|
||
flaggerv1 "github.com/fluxcd/flagger/pkg/apis/flagger/v1beta1" | ||
"github.com/machinebox/graphql" | ||
) | ||
|
||
const ( | ||
healthCheckEndpoint = "/internal/l7check" | ||
queryEndpoint = "/graphql" | ||
) | ||
|
||
type skywalkingResponse = map[string]struct { | ||
Label string `json:"label"` | ||
Values struct { | ||
Values []struct { | ||
Value *float64 `json:"value"` | ||
} `json:"values"` | ||
} `json:"values"` | ||
} | ||
|
||
type duration struct { | ||
Start string `json:"start"` | ||
End string `json:"end"` | ||
Step string `json:"step"` | ||
} | ||
|
||
// SkyWalkingProvider executes SkyWalking render URL API queries. | ||
type SkyWalkingProvider struct { | ||
address string | ||
timeout time.Duration | ||
client graphql.Client | ||
interval time.Duration | ||
} | ||
|
||
// NewSkyWalkingProvider takes a provider spec and credentials map, | ||
// validates the address, extracts the credentials map's username | ||
// and password values if provided, and returns a skywalking client | ||
// ready to execute queries against the skywalking render URL API. | ||
func NewSkyWalkingProvider(metricInterval string, provider flaggerv1.MetricTemplateProvider, credentials map[string][]byte) (*SkyWalkingProvider, error) { | ||
md, err := time.ParseDuration(metricInterval) | ||
if err != nil { | ||
return nil, fmt.Errorf("error parsing metric interval: %w", err) | ||
} | ||
|
||
sw := SkyWalkingProvider{ | ||
timeout: 5 * time.Second, | ||
address: provider.Address, | ||
client: *graphql.NewClient(provider.Address + queryEndpoint), | ||
interval: time.Duration(md.Nanoseconds()), | ||
} | ||
|
||
return &sw, nil | ||
} | ||
|
||
// RunQuery executes the skywalking render URL API query and returns the | ||
// the first result as float64. | ||
func (p *SkyWalkingProvider) RunQuery(query string) (float64, error) { | ||
req := graphql.NewRequest(query) | ||
req.Var("duration", duration{ | ||
Start: time.Now().Add(-p.interval).Format("2006-01-02 1504"), | ||
End: time.Now().Format("2006-01-02 1504"), | ||
Step: "MINUTE", | ||
}) | ||
res := skywalkingResponse{} | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), p.timeout) | ||
defer cancel() | ||
|
||
err := p.client.Run(ctx, req, &res) | ||
if err != nil { | ||
return 0, fmt.Errorf("request failed: %w", err) | ||
} | ||
|
||
for _, r := range res { | ||
for _, v := range r.Values.Values { | ||
if v.Value != nil { | ||
return *v.Value, nil | ||
} | ||
} | ||
} | ||
|
||
return 0, ErrNoValuesFound | ||
} | ||
|
||
// IsOnline runs a simple skywalking render URL API query and returns | ||
// an error if the API is unreachable. | ||
func (p *SkyWalkingProvider) IsOnline() (bool, error) { | ||
req, err := http.NewRequest("GET", p.address+healthCheckEndpoint, nil) | ||
if err != nil { | ||
return false, fmt.Errorf("error http.NewRequest: %w", err) | ||
} | ||
|
||
ctx, cancel := context.WithTimeout(req.Context(), p.timeout) | ||
defer cancel() | ||
r, err := http.DefaultClient.Do(req.WithContext(ctx)) | ||
if err != nil { | ||
return false, fmt.Errorf("request failed: %w", err) | ||
} | ||
|
||
defer r.Body.Close() | ||
|
||
b, err := io.ReadAll(r.Body) | ||
if err != nil { | ||
return false, fmt.Errorf("error reading body: %w", err) | ||
} | ||
|
||
// healthCheckEndpoint is added recently, for older versions of skywalking GET method is not allowed. | ||
if r.StatusCode != http.StatusOK && r.StatusCode != http.StatusMethodNotAllowed { | ||
return false, fmt.Errorf("error response: %s", string(b)) | ||
} | ||
|
||
return true, nil | ||
} |
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,112 @@ | ||
/* | ||
Copyright 2020 The Flux 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 providers | ||
|
||
import ( | ||
"errors" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
"time" | ||
|
||
flaggerv1 "github.com/fluxcd/flagger/pkg/apis/flagger/v1beta1" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestNewSkyWalkingProvider(t *testing.T) { | ||
sw, err := NewSkyWalkingProvider("1m", flaggerv1.MetricTemplateProvider{ | ||
Address: "http://skywalking-oap.istio-system.svc.cluster.local:12800", | ||
}, nil) | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, 1*time.Minute, sw.interval) | ||
} | ||
|
||
func TestSkywalkingProvider_RunQuery(t *testing.T) { | ||
t.Run("ok", func(t *testing.T) { | ||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
json := `{"data":{"service_apdex0":{"values":{"values":[{"value":10000},{"value":10010}]}}}}` | ||
w.Write([]byte(json)) | ||
})) | ||
defer ts.Close() | ||
|
||
dp, err := NewSkyWalkingProvider("1m", | ||
flaggerv1.MetricTemplateProvider{Address: ts.URL}, | ||
nil, | ||
) | ||
require.NoError(t, err) | ||
|
||
f, err := dp.RunQuery(`{ "query": "query queryData($duration: Duration!) { service_apdex: readMetricsValues( condition: { name: \"service_apdex\", entity: { scope: Service, serviceName: \"agent::songs\", normal: true } }, duration: $duration) { label values { values { value } } } }" }`) | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, 10000.0, f) | ||
}) | ||
|
||
t.Run("no values", func(t *testing.T) { | ||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
json := `{"series": [{"pointlist": []}]}` | ||
w.Write([]byte(json)) | ||
})) | ||
defer ts.Close() | ||
|
||
sw, err := NewSkyWalkingProvider("1m", | ||
flaggerv1.MetricTemplateProvider{Address: ts.URL}, | ||
nil, | ||
) | ||
require.NoError(t, err) | ||
_, err = sw.RunQuery("") | ||
require.True(t, errors.Is(err, ErrNoValuesFound)) | ||
}) | ||
} | ||
|
||
func TestSkywalkingProvider_IsOnline(t *testing.T) { | ||
t.Run("ok if method not allowed", func(t *testing.T) { | ||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusMethodNotAllowed) | ||
})) | ||
defer ts.Close() | ||
|
||
sw, err := NewSkyWalkingProvider("1m", flaggerv1.MetricTemplateProvider{ | ||
Address: ts.URL, | ||
}, nil) | ||
require.NoError(t, err) | ||
|
||
ok, err := sw.IsOnline() | ||
require.NoError(t, err) | ||
|
||
assert.True(t, ok) | ||
}) | ||
|
||
t.Run("ok if 200", func(t *testing.T) { | ||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
response := `healthy` | ||
w.Write([]byte(response)) | ||
})) | ||
defer ts.Close() | ||
|
||
sw, err := NewSkyWalkingProvider("1m", flaggerv1.MetricTemplateProvider{ | ||
Address: ts.URL, | ||
}, nil) | ||
require.NoError(t, err) | ||
|
||
ok, err := sw.IsOnline() | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, true, ok) | ||
}) | ||
} |