Skip to content

Commit

Permalink
Merge pull request #64 from slrtbtfs/alpha0.0.1
Browse files Browse the repository at this point in the history
First alpha release
  • Loading branch information
slrtbtfs authored Nov 13, 2019
2 parents 586ad86 + d4db8b1 commit 18ee6d5
Show file tree
Hide file tree
Showing 11 changed files with 137 additions and 85 deletions.
45 changes: 43 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,55 @@ Most of the planned features are not implemented yet.
- [x] Function documentation
- [ ] Operator documentation
- [x] Metric and label help strings from a prometheus instance
- [ ] Even when the has syntax errors
- [ ] Even when the Query has syntax errors
- [ ] Completion
- [ ] Functions
- [x] Metrics
- [ ] Labels
- [ ] Context sensitive, i.e respecting function argument types
- [ ] Even when the has syntax errors
- [ ] Even when the Query has syntax errors
- [ ] Signature information for functions (while typing)
- [ ] (Linting)
- [ ] (Formatting)

## Using the Language Server

A Language Server on its own is not very useful. You need some Language Client to use it with.

The following Language Clients have been tested with this language server. More will be added in the future.

Feel free to reach out if you want to use it with another Editor/Tool.

### VS Code

There exists a VS Code extension based on this language server: <https://github.com/slrtbtfs/vscode-prometheus>

It is used as the main test platform for this langauge server.

Since it isn't published a on the Extensions Marketplace yet, you'll have to follow the somewhat more complicated installation steps described in the README there.

### (Neo)Vim

With Vim, currently only PromQL queries inside YAML files work without additional support. Generally the experience with Vim is more buggy than with VS Code.

#### Setup

1. Install the [YouCompleteMe](https://github.com/ycm-core/YouCompleteMe) Plugin.
2. Put the configuration following configuration file for the langauge server in `.vim/promql-lsp.yaml`.

# Change this adress to the address of the prometheus server you want to use for metadata
prometheus_url: http://localhost:9090
rpc_trace: text

3. Add the following to your `.vimrc`

let g:ycm_language_server = [
\ { 'name': 'promql',
\ 'filetypes': [ 'yaml' ],
\ 'cmdline': [ 'promql-langserver', '--config-file', expand('~/.vim/promql-lsp.yaml')]
\ },
\ ]

#### Debugging

The Vim command `:YcmDebugInfo` gives status information and points to logfiles.
1 change: 1 addition & 0 deletions VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.1.0
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ require (
github.com/json-iterator/go v1.1.8 // indirect
github.com/prometheus/client_golang v1.2.1
github.com/prometheus/procfs v0.0.6 // indirect
github.com/prometheus/prometheus v2.14.0+incompatible
github.com/prometheus/tsdb v0.10.0 // indirect
github.com/rakyll/statik v0.1.7-0.20191104211043-6b2f3ee522b6
github.com/slrtbtfs/prometheus v1.8.2-0.20191113134020-c261cd341add
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e
Expand Down
12 changes: 0 additions & 12 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgrijalva/jwt-go v0.0.0-20160705203006-01aeca54ebda/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
github.com/dgryski/go-sip13 v0.0.0-20190329191031-25c5027a8c7b/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
github.com/docker/go-units v0.3.3/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
github.com/docker/go-units v0.4.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
Expand Down Expand Up @@ -206,7 +205,6 @@ github.com/influxdata/influxdb v1.7.7/go.mod h1:qZna6X/4elxqT3yI9iZYdZrWWdeFOOpr
github.com/jessevdk/go-flags v0.0.0-20180331124232-1c38ed7ad0cc/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
github.com/jpillora/backoff v0.0.0-20180909062703-3050d21c67d7/go.mod h1:2iMrUgbbvHEiQClaW2NsSzMyGHqN+rDFqY705q49KG0=
github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4=
github.com/json-iterator/go v0.0.0-20180612202835-f2b4162afba3/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
github.com/json-iterator/go v0.0.0-20180701071628-ab8a2e0c74be/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
Expand Down Expand Up @@ -307,12 +305,6 @@ github.com/prometheus/procfs v0.0.5/go.mod h1:4A/X28fw3Fc593LaREMrKMqOKvUAntwMDa
github.com/prometheus/procfs v0.0.6 h1:0qbH+Yqu/cj1ViVLvEWCP6qMQ4efWUj6bQqOEA0V0U4=
github.com/prometheus/procfs v0.0.6/go.mod h1:7Qr8sr6344vo1JqZ6HhLceV9o3AJ1Ff+GxbHq6oeK9A=
github.com/prometheus/prometheus v0.0.0-20180315085919-58e2a31db8de/go.mod h1:oAIUtOny2rjMX0OWN5vPR5/q/twIROJvdqnQKDdil/s=
github.com/prometheus/prometheus v2.5.0+incompatible h1:7QPitgO2kOFG8ecuRn9O/4L9+10He72rVRJvMXrE9Hg=
github.com/prometheus/prometheus v2.5.0+incompatible/go.mod h1:oAIUtOny2rjMX0OWN5vPR5/q/twIROJvdqnQKDdil/s=
github.com/prometheus/prometheus v2.14.0+incompatible h1:rKu414OnUZx/APkuA6Mg5iadtxhY5X64HcUq2C9O8x4=
github.com/prometheus/prometheus v2.14.0+incompatible/go.mod h1:PVTKYlgELGIDbIKIyWRzD4WKjnavPynGOFLSuDpvOwU=
github.com/prometheus/tsdb v0.10.0 h1:If5rVCMTp6W2SiRAQFlbpJNgVlgMEd+U2GZckwK38ic=
github.com/prometheus/tsdb v0.10.0/go.mod h1:oi49uRhEe9dPUTlS3JRZOwJuVi6tmh10QSgwXEyGCt4=
github.com/rakyll/statik v0.1.7-0.20191104211043-6b2f3ee522b6 h1:aTZBZ+7KwHnr9TPzd4l4PGvUJckC0Vw30gxXBxMGhyo=
github.com/rakyll/statik v0.1.7-0.20191104211043-6b2f3ee522b6/go.mod h1:AlZONWzMtEnMs7W4e/1LURLiI49pIMmp6V9Unghqrcc=
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
Expand All @@ -328,12 +320,8 @@ github.com/shurcooL/vfsgen v0.0.0-20180825020608-02ddb050ef6b/go.mod h1:TrYk7fJV
github.com/shurcooL/vfsgen v0.0.0-20181202132449-6a9ea43bcacd/go.mod h1:TrYk7fJVaAttu97ZZKrO9UbRa8izdowaMIZcxYMbVaw=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/slrtbtfs/prometheus v1.8.2-0.20191111173200-f07eddb4fa6d h1:qknk6TkXxIlaRhlRq0fN2SZ2x0O9kZVgQxy2HIGeSFU=
github.com/slrtbtfs/prometheus v1.8.2-0.20191111173200-f07eddb4fa6d/go.mod h1:Y+4t3nnYwtkUPBvX/ULNwGtDS994WAEY+eLVhk75/eM=
github.com/slrtbtfs/prometheus v1.8.2-0.20191113134020-c261cd341add h1:P0DukZaIU1AlJB3KgmqbH50VUQdw3io0axV/2cO2wUA=
github.com/slrtbtfs/prometheus v1.8.2-0.20191113134020-c261cd341add/go.mod h1:4Xj9dJhnWZeOgrtICgocaD1jVcfrHdCiiUJdDcxKqOY=
github.com/slrtbtfs/prometheus v2.5.0+incompatible h1:N6m9IoYp6m1dtEgEOuhul959egDQ/JXoPUWQHvCCeAk=
github.com/slrtbtfs/prometheus v2.5.0+incompatible/go.mod h1:gMzPp53v0KKaVsG3p7sRKGryLVWaPuvkfzUB+heZjDo=
github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM=
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
Expand Down
21 changes: 14 additions & 7 deletions langserver/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (

// Completion is required by the protocol.Server interface
// nolint: wsl
func (s *Server) Completion(ctx context.Context, params *protocol.CompletionParams) (*protocol.CompletionList, error) {
func (s *server) Completion(ctx context.Context, params *protocol.CompletionParams) (*protocol.CompletionList, error) {
fmt.Fprintln(os.Stderr, "0")
doc, docCtx, err := s.cache.GetDocument(params.TextDocument.URI)
if err != nil {
Expand All @@ -54,12 +54,23 @@ func (s *Server) Completion(ctx context.Context, params *protocol.CompletionPara
return nil, nil
}

return s.getCompletions(ctx, node, pos)
if completions, err := s.getCompletions(ctx, node); err == nil && completions != nil {
return completions, nil
}

node = getSmallestSurroundingNode(query.Ast, pos)
if node == nil {
return nil, nil
}

return s.getCompletions(ctx, node)
}

func (s *Server) getCompletions(ctx context.Context, node promql.Node, pos token.Pos) (*protocol.CompletionList, error) { // nolint:lll
func (s *server) getCompletions(ctx context.Context, node promql.Node) (*protocol.CompletionList, error) { // nolint:lll
var metricName string

fmt.Fprintln(os.Stderr, "Yo", node)

switch n := node.(type) {
case *promql.VectorSelector:
metricName = n.Name
Expand All @@ -69,10 +80,6 @@ func (s *Server) getCompletions(ctx context.Context, node promql.Node, pos token
return nil, nil
}

if node.Pos()+token.Pos(len(metricName)) != pos {
return nil, nil
}

if s.prometheus == nil {
return nil, nil
}
Expand Down
4 changes: 2 additions & 2 deletions langserver/diagnostics.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
)

// nolint:funlen
func (s *Server) diagnostics(uri string) {
func (s *server) diagnostics(uri string) {
d, ctx, err := s.cache.GetDocument(uri)
if err != nil {
fmt.Fprintf(os.Stderr, "Document %v doesn't exist any more", uri)
Expand Down Expand Up @@ -51,7 +51,7 @@ func (s *Server) diagnostics(uri string) {
}
}

func (s *Server) clearDiagnostics(ctx context.Context, uri string, version float64) {
func (s *server) clearDiagnostics(ctx context.Context, uri string, version float64) {
diagnostics := &protocol.PublishDiagnosticsParams{
URI: uri,
Version: version,
Expand Down
22 changes: 16 additions & 6 deletions langserver/general.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ import (

// Initialize handles a call from the client to initialize the server
// required by the protocol.Server interface
func (s *Server) Initialize(ctx context.Context, params *protocol.ParamInitia) (*protocol.InitializeResult, error) {
// nolint:funlen
func (s *server) Initialize(ctx context.Context, params *protocol.ParamInitia) (*protocol.InitializeResult, error) {
s.stateMu.Lock()
state := s.state
s.stateMu.Unlock()
Expand All @@ -45,8 +46,17 @@ func (s *Server) Initialize(ctx context.Context, params *protocol.ParamInitia) (
// Support incremental changes
Change: 2,
},
HoverProvider: true,
CompletionProvider: &protocol.CompletionOptions{},
HoverProvider: true,
CompletionProvider: &protocol.CompletionOptions{
TriggerCharacters: []string{
" ", "\n", "\t", "(", ")", "[", "]", "{", "}", "+", "-", "*", "/", "!", "=", "\"", ",",
},
AllCommitCharacters: nil,
ResolveProvider: false,
WorkDoneProgressOptions: protocol.WorkDoneProgressOptions{
WorkDoneProgress: false,
},
},
SignatureHelpProvider: nil,
DefinitionProvider: false,
ReferencesProvider: false,
Expand Down Expand Up @@ -88,7 +98,7 @@ func (s *Server) Initialize(ctx context.Context, params *protocol.ParamInitia) (

// Initialized receives a confirmation by the client that the connection has been initialized
// required by the protocol.Server interface
func (s *Server) Initialized(ctx context.Context, params *protocol.InitializedParams) error {
func (s *server) Initialized(ctx context.Context, params *protocol.InitializedParams) error {
s.stateMu.Lock()
s.state = serverInitialized
s.stateMu.Unlock()
Expand All @@ -98,7 +108,7 @@ func (s *Server) Initialized(ctx context.Context, params *protocol.InitializedPa

// Shutdown receives a call from the client to shutdown the connection
// required by the protocol.Server interface
func (s *Server) Shutdown(ctx context.Context) error {
func (s *server) Shutdown(ctx context.Context) error {
s.stateMu.Lock()
defer s.stateMu.Unlock()

Expand All @@ -113,7 +123,7 @@ func (s *Server) Shutdown(ctx context.Context) error {

// Exit ends the connection
// required by the protocol.Server interface
func (s *Server) Exit(ctx context.Context) error {
func (s *server) Exit(ctx context.Context) error {
s.stateMu.Lock()

defer s.stateMu.Unlock()
Expand Down
22 changes: 12 additions & 10 deletions langserver/hover.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func initializeFunctionDocumentation() http.FileSystem {

// Hover shows documentation on hover
// required by the protocol.Server interface
func (s *Server) Hover(ctx context.Context, params *protocol.HoverParams) (*protocol.Hover, error) {
func (s *server) Hover(ctx context.Context, params *protocol.HoverParams) (*protocol.Hover, error) {
doc, docCtx, err := s.cache.GetDocument(params.TextDocumentPositionParams.TextDocument.URI)
if err != nil {
return nil, err
Expand Down Expand Up @@ -102,16 +102,9 @@ func (s *Server) Hover(ctx context.Context, params *protocol.HoverParams) (*prot
}, nil
}

func (s *Server) nodeToDocMarkdown(ctx context.Context, node promql.Node) string {
func (s *server) nodeToDocMarkdown(ctx context.Context, node promql.Node) string {
var ret bytes.Buffer

if expr, ok := node.(promql.Expr); ok {
_, err := ret.WriteString(fmt.Sprintf("__Type:__ %v\n\n", expr.Type()))
if err != nil {
return ""
}
}

if call, ok := node.(*promql.Call); ok {
doc := funcDocStrings(call.Func.Name)

Expand Down Expand Up @@ -150,6 +143,13 @@ func (s *Server) nodeToDocMarkdown(ctx context.Context, node promql.Node) string
}
}

if expr, ok := node.(promql.Expr); ok {
_, err := ret.WriteString(fmt.Sprintf("__PromQL Type:__ %v\n\n", expr.Type()))
if err != nil {
return ""
}
}

return ret.String()
}

Expand Down Expand Up @@ -179,7 +179,7 @@ func funcDocStrings(name string) string {
return string(ret)
}

func (s *Server) getMetricDocs(ctx context.Context, metric string) (string, error) {
func (s *server) getMetricDocs(ctx context.Context, metric string) (string, error) {
if s.prometheus == nil {
return "", nil
}
Expand All @@ -195,6 +195,8 @@ func (s *Server) getMetricDocs(ctx context.Context, metric string) (string, erro

var ret strings.Builder

fmt.Fprintf(&ret, "### %s\n\n", metric)

if metadata[0].Help != "" {
fmt.Fprintf(&ret, "__Metric Help:__ %s\n\n", metadata[0].Help)
}
Expand Down
Loading

0 comments on commit 18ee6d5

Please sign in to comment.