Skip to content

Commit

Permalink
Add support for an additional chart query type (#72)
Browse files Browse the repository at this point in the history
* add support for spans query

* update tests to run on any environment

* rename from spans_query to spans block

* fix tflint errors

* add exporter support

* embed version number in releaser

* bump version
  • Loading branch information
Clay Smith authored Feb 10, 2022
1 parent aa03a37 commit 5e213c0
Show file tree
Hide file tree
Showing 16 changed files with 318 additions and 79 deletions.
13 changes: 2 additions & 11 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,6 @@ jobs:
with:
tflint_version: v0.26.0

- name: Lint root module
run: |
tflint --config ${{ github.workspace }}/.tflint.hcl ${{ github.workspace }}
- name: Lint modules directory in a loop
run: |
for m in $(ls -1d modules/*/)
do
tflint \
--config ${{ github.workspace }}/.tflint.hcl \
${{ github.workspace }}/${m}
done
- name: Lint examples directory in a loop
run: |
for m in $(ls -1d examples/*/)
Expand Down Expand Up @@ -90,6 +79,8 @@ jobs:
runs-on: ubuntu-latest
env:
LIGHTSTEP_API_KEY_PUBLIC: ${{ secrets.LIGHTSTEP_API_KEY_PUBLIC }}
LIGHTSTEP_API_KEY_STAGING: ${{ secrets.LIGHTSTEP_API_KEY_STAGING }}

steps:
- name: Install Go
uses: actions/setup-go@v2
Expand Down
2 changes: 1 addition & 1 deletion .go-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.51.6
1.60.0
2 changes: 1 addition & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ builds:
flags:
- -trimpath
ldflags:
- '-s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X github.com/terraform-providers/terraform-provider-lightstep/version.ProviderVersion={{.Version}}'
- '-s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X github.com/lightstep/terraform-provider-lightstep/version.ProviderVersion={{.Version}}'
goos:
- freebsd
- windows
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ lint:

.PHONY: acc-test
acc-test:
@TF_ACC=true LIGHTSTEP_API_KEY=${LIGHTSTEP_API_KEY_PUBLIC} LIGHTSTEP_ORG="LightStep" go test -v ./lightstep
@TF_ACC=true LIGHTSTEP_API_KEY=${LIGHTSTEP_API_KEY_STAGING} LIGHTSTEP_ORG="LightStep" LIGHTSTEP_ENV="staging" go test -v ./lightstep

.PHONY: ensure-clean-repo
ensure-clean-repo:
Expand Down
20 changes: 14 additions & 6 deletions client/metric_conditions.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,13 @@ type Thresholds struct {
}

type MetricQueryWithAttributes struct {
Name string `json:"query-name"`
Type string `json:"query-type"`
Hidden bool `json:"hidden"`
Display string `json:"display-type"`
Query MetricQuery `json:"metric-query"`
TQLQuery string `json:"tql-query"`
Name string `json:"query-name"`
Type string `json:"query-type"`
Hidden bool `json:"hidden"`
Display string `json:"display-type"`
Query MetricQuery `json:"metric-query"`
SpansQuery SpansQuery `json:"spans-query,omitempty"`
TQLQuery string `json:"tql-query"`
}

type MetricQuery struct {
Expand All @@ -59,6 +60,13 @@ type MetricQuery struct {
GroupBy GroupBy `json:"group-by,omitempty"`
}

type SpansQuery struct {
Query string `json:"query"`
Operator string `json:"operator"`
LatencyPercentiles []float64 `json:"latency-percentiles,omitempty"`
GroupByKeys []string `json:"group-by,omitempty"`
}

type LabelFilter struct {
Key string `json:"key"`
Value string `json:"value"`
Expand Down
19 changes: 0 additions & 19 deletions examples/main.tf
Original file line number Diff line number Diff line change
@@ -1,22 +1,3 @@
provider "lightstep" {
environment = "public"
api_key_env_var = "LIGHTSTEP_API_KEY_PUBLIC"
organization = "LightStep"
}

terraform {
required_providers {
lightstep = {
source = "lightstep/lightstep"
version = "1.51.2"
# For more information, see the provider source documentation:
#
# https://www.terraform.io/docs/configuration/providers.html#provider-source
}
}
required_version = "~> 1.0.0"
}

##############################################################
## Streams
##############################################################
Expand Down
2 changes: 1 addition & 1 deletion examples/versions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ terraform {
}
}
required_version = "~> 1.1.0"
}
}
22 changes: 19 additions & 3 deletions exporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package exporter

import (
"context"
"github.com/lightstep/terraform-provider-lightstep/client"
"log"
"os"
"strings"
"text/template"

"github.com/lightstep/terraform-provider-lightstep/client"
)

const dashboardTemplate = `
Expand All @@ -22,7 +24,14 @@ resource "lightstep_metric_dashboard" "exported_dashboard" {
query_name = "{{.Name}}"
display = "{{.Display}}"
hidden = {{.Hidden}}
{{if .TQLQuery}}
{{if (and .SpansQuery .SpansQuery.Query) }}
spans {
query = "{{escapeSpanQuery .SpansQuery.Query}}"
operator = "{{.SpansQuery.Operator}}"
group_by_keys = [{{range .SpansQuery.GroupByKeys}}"{{.}}",{{end}}]{{if eq .SpansQuery.Operator "latency"}}
latency_percentiles = [{{range .SpansQuery.LatencyPercentiles}}{{.}},{{end}}]{{end}}
}
{{end}}{{if .TQLQuery}}
tql = "{{.TQLQuery}}"
{{end}}{{if .Query.Metric}}
metric = "{{.Query.Metric}}"
Expand All @@ -48,6 +57,10 @@ resource "lightstep_metric_dashboard" "exported_dashboard" {
}
`

func escapeSpanQuery(input string) string {
return strings.Replace(input, "\"", "\\\"", -1)
}

func Run(args ...string) error {
if len(os.Getenv("LIGHTSTEP_API_KEY")) == 0 {
log.Fatalf("error: LIGHTSTEP_API_KEY env variable must be set")
Expand All @@ -72,7 +85,10 @@ func Run(args ...string) error {
log.Fatalf("error: could not get dashboard: %v", err)
}

t := template.New("HCL Dashboard template")
t := template.New("").Funcs(template.FuncMap{
"escapeSpanQuery": escapeSpanQuery,
})

t, err = t.Parse(dashboardTemplate)
if err != nil {
log.Fatal("Dashboard parsing error: ", err)
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.16

require (
github.com/aws/aws-sdk-go v1.30.12 // indirect
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
github.com/hashicorp/go-retryablehttp v0.7.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.10.0
github.com/stretchr/testify v1.7.0
Expand Down
2 changes: 1 addition & 1 deletion lightstep/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func Provider() *schema.Provider {
"environment": {
Type: schema.TypeString,
Optional: true,
Default: "public",
DefaultFunc: schema.EnvDefaultFunc("LIGHTSTEP_ENV", "public"),
ValidateFunc: validation.StringMatch(regexp.MustCompile(`^(staging|meta|public)$`), "Must be one of: staging, meta, public"),
Description: "The name of the Lightstep environment, must be one of: staging, meta, public.",
},
Expand Down
25 changes: 22 additions & 3 deletions lightstep/resource_alerting_rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,31 @@ func TestAccAlertingRuleImport(t *testing.T) {
Steps: []resource.TestStep{
{
Config: `
resource "lightstep_stream" "stream_import_alerting_rule_import" {
project_name = ` + fmt.Sprintf("\"%s\"", test_project) + `
stream_name = "Alerting rule import"
query = "operation IN (\"api/v1/import_rule\")"
}
resource "lightstep_stream_condition" "stream_condition_alerting_rule_import" {
project_name = ` + fmt.Sprintf("\"%s\"", test_project) + `
condition_name = "Importing rule errors for BEEMO"
expression = "err > 0.4"
evaluation_window_ms = 300000
stream_id = lightstep_stream.stream_import_alerting_rule_import.id
}
resource "lightstep_alerting_rule" "import-cond" {
project_name = "terraform-provider-tests"
destination_id = "tvydr9gV"
condition_id = "KN6SX47x"
project_name = ` + fmt.Sprintf("\"%s\"", test_project) + `
destination_id = lightstep_slack_destination.ari_slack.id
condition_id = lightstep_stream_condition.stream_condition_alerting_rule_import.id
update_interval = "1h"
}
resource "lightstep_slack_destination" "ari_slack" {
project_name = ` + fmt.Sprintf("\"%s\"", test_project) + `
channel = "#urgent-care"
}
`,
},
{
Expand Down
Loading

0 comments on commit 5e213c0

Please sign in to comment.