-
-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add JSON log parser * Update README.adoc Fixed some grammatical errors. Co-authored-by: Martin Helmich <[email protected]> * Update parser/jsonparser/jsonparser_test.go Use the assertions of testify package. Co-authored-by: Martin Helmich <[email protected]> * Update parser/jsonparser/jsonparser.go Optimize performance. Co-authored-by: Martin Helmich <[email protected]> * Update jsonparser_test.go Use the testify package. * 1. Add Parser flag in LoadConfigFromFlags function. 2. Use testify package in parser/*._test file. Co-authored-by: Martin Helmich <[email protected]>
- Loading branch information
1 parent
dabff82
commit b033474
Showing
11 changed files
with
192 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
FROM golang:1.11 | ||
FROM golang:1.13 | ||
|
||
COPY . /work | ||
WORKDIR /work | ||
|
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,34 @@ | ||
package jsonparser | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
) | ||
|
||
// JsonParser parse a JSON string. | ||
type JsonParser struct{} | ||
|
||
// NewJsonParser returns a new json parser. | ||
func NewJsonParser() *JsonParser { | ||
return &JsonParser{} | ||
} | ||
|
||
// ParseString implements the Parser interface. | ||
// The value in the map is not necessarily a string, so it needs to be converted. | ||
func (j *JsonParser) ParseString(line string) (map[string]string, error) { | ||
var parsed map[string]interface{} | ||
err := json.Unmarshal([]byte(line), &parsed) | ||
if err != nil { | ||
return nil, fmt.Errorf("json log parsing err: %w", err) | ||
} | ||
|
||
fields := make(map[string]string, len(parsed)) | ||
for k, v := range parsed { | ||
if s, ok := v.(string); ok { | ||
fields[k] = s | ||
} else { | ||
fields[k] = fmt.Sprintf("%v", v) | ||
} | ||
} | ||
return fields, 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,44 @@ | ||
package jsonparser | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestJsonParse(t *testing.T) { | ||
parser := NewJsonParser() | ||
line := `{"time_local":"2021-02-03T11:22:33+08:00","request_length":123,"request_method":"GET","request":"GET /order/2145 HTTP/1.1","body_bytes_sent":518,"status": 200,"request_time":0.544,"upstream_response_time":"0.543"}` | ||
|
||
got, err := parser.ParseString(line) | ||
require.NoError(t, err) | ||
|
||
want := map[string]string{ | ||
"time_local": "2021-02-03T11:22:33+08:00", | ||
"request_time": "0.544", | ||
"request_length": "123", | ||
"upstream_response_time": "0.543", | ||
"status": "200", | ||
"body_bytes_sent": "518", | ||
"request": "GET /order/2145 HTTP/1.1", | ||
"request_method": "GET", | ||
} | ||
if !reflect.DeepEqual(got, want) { | ||
t.Errorf("JsonParser.Parse() = %v, want %v", got, want) | ||
} | ||
} | ||
|
||
func BenchmarkParseJson(b *testing.B) { | ||
parser := NewJsonParser() | ||
line := `{"time_local":"2021-02-03T11:22:33+08:00","request_length":123,"request_method":"GET","request":"GET /order/2145 HTTP/1.1","body_bytes_sent":518,"status": 200,"request_time":0.544,"upstream_response_time":"0.543"}` | ||
|
||
for i := 0; i < b.N; i++ { | ||
res, err := parser.ParseString(line) | ||
if err != nil { | ||
b.Error(err) | ||
} | ||
_ = fmt.Sprintf("%v", res) | ||
} | ||
} |
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,24 @@ | ||
package parser | ||
|
||
import ( | ||
"github.com/martin-helmich/prometheus-nginxlog-exporter/config" | ||
"github.com/martin-helmich/prometheus-nginxlog-exporter/parser/jsonparser" | ||
"github.com/martin-helmich/prometheus-nginxlog-exporter/parser/textparser" | ||
) | ||
|
||
// Parser parses a line of log to a map[string]string. | ||
type Parser interface { | ||
ParseString(line string) (map[string]string, error) | ||
} | ||
|
||
// NewParser returns a Parser with the given config.NamespaceConfig. | ||
func NewParser(nsCfg config.NamespaceConfig) Parser { | ||
switch nsCfg.Parser { | ||
case "text": | ||
return textparser.NewTextParser(nsCfg.Format) | ||
case "json": | ||
return jsonparser.NewJsonParser() | ||
default: | ||
return textparser.NewTextParser(nsCfg.Format) | ||
} | ||
} |
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,29 @@ | ||
package textparser | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/satyrius/gonx" | ||
) | ||
|
||
// TextParser parses variables patterns using config.NamespaceConfig.Format. | ||
type TextParser struct { | ||
parser *gonx.Parser | ||
} | ||
|
||
// NewTextParser returns a new text parser. | ||
func NewTextParser(format string) *TextParser { | ||
return &TextParser{ | ||
parser: gonx.NewParser(format), | ||
} | ||
} | ||
|
||
// ParseString implements the Parser interface. | ||
func (t *TextParser) ParseString(line string) (map[string]string, error) { | ||
entry, err := t.parser.ParseString(line) | ||
if err != nil { | ||
return nil, fmt.Errorf("text log parsing err: %w", err) | ||
} | ||
|
||
return entry.Fields(), 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,44 @@ | ||
package textparser | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestTextParse(t *testing.T) { | ||
parser := NewTextParser(`[$time_local] $request_method "$request" $request_length $body_bytes_sent $status $request_time $upstream_response_time`) | ||
line := `[03/Feb/2021:11:22:33 +0800] GET "GET /order/2145 HTTP/1.1" 123 518 200 0.544 0.543` | ||
|
||
got, err := parser.ParseString(line) | ||
require.NoError(t, err) | ||
|
||
want := map[string]string{ | ||
"time_local": "03/Feb/2021:11:22:33 +0800", | ||
"request_time": "0.544", | ||
"request_length": "123", | ||
"upstream_response_time": "0.543", | ||
"status": "200", | ||
"body_bytes_sent": "518", | ||
"request": "GET /order/2145 HTTP/1.1", | ||
"request_method": "GET", | ||
} | ||
if !reflect.DeepEqual(got, want) { | ||
t.Errorf("TextParser.Parse() = %v, want %v", got, want) | ||
} | ||
} | ||
|
||
func BenchmarkParseText(b *testing.B) { | ||
parser := NewTextParser(`[$time_local] $request_method "$request" $request_length $body_bytes_sent $status $request_time $upstream_response_time`) | ||
line := `[03/Feb/2021:11:22:33 +0800] GET "GET /order/2145 HTTP/1.1" 123 518 200 0.544 0.543` | ||
|
||
for i := 0; i < b.N; i++ { | ||
res, err := parser.ParseString(line) | ||
if err != nil { | ||
b.Error(err) | ||
} | ||
_ = fmt.Sprintf("%v", res) | ||
} | ||
} |