-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loki Exporter - Adding a feature for loki exporter to encode JSON for…
… log entry (#5846) * initial commit * revert un-needed changes * test json encode as entry * add encoding files * merge loki encoding with the resource atttribute branch * add format option to loki exporter config * add test for loki/json test data * improve variable names and comments * Create feature for loki exporter to encode json * mapping convert functions * Fix linting errors * update pdata package * update to use otel 0.36 * Add resources in encoded json structure * Add missing licence * Fix review * Group similar errors * Fix serialization * Add a test for unsuported body type * Fix tests * Fix lint * Change error level to debug for dropped logs to avoid flood, keeping a trace of the reason Co-authored-by: Shaun Creary <[email protected]>
- Loading branch information
Showing
11 changed files
with
283 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Copyright The OpenTelemetry 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 lokiexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/lokiexporter" | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
"go.opentelemetry.io/collector/model/pdata" | ||
) | ||
|
||
// JSON representation of the LogRecord as described by https://developers.google.com/protocol-buffers/docs/proto3#json | ||
|
||
type lokiEntry struct { | ||
Name string `json:"name,omitempty"` | ||
Body string `json:"body,omitempty"` | ||
TraceID string `json:"traceid,omitempty"` | ||
SpanID string `json:"spanid,omitempty"` | ||
Severity string `json:"severity,omitempty"` | ||
Attributes map[string]interface{} `json:"attributes,omitempty"` | ||
Resources map[string]interface{} `json:"resources,omitempty"` | ||
} | ||
|
||
func serializeBody(body pdata.AttributeValue) (string, error) { | ||
str := "" | ||
var err error | ||
if body.Type() == pdata.AttributeValueTypeString { | ||
str = body.StringVal() | ||
} else { | ||
err = fmt.Errorf("unsuported body type to serialize") | ||
} | ||
return str, err | ||
} | ||
|
||
func encodeJSON(lr pdata.LogRecord, res pdata.Resource) (string, error) { | ||
var logRecord lokiEntry | ||
var jsonRecord []byte | ||
var err error | ||
var body string | ||
|
||
body, err = serializeBody(lr.Body()) | ||
if err != nil { | ||
return "", err | ||
} | ||
logRecord = lokiEntry{ | ||
Name: lr.Name(), | ||
Body: body, | ||
TraceID: lr.TraceID().HexString(), | ||
SpanID: lr.SpanID().HexString(), | ||
Severity: lr.SeverityText(), | ||
Attributes: lr.Attributes().AsRaw(), | ||
Resources: res.Attributes().AsRaw(), | ||
} | ||
lr.Body().Type() | ||
|
||
jsonRecord, err = json.Marshal(logRecord) | ||
if err != nil { | ||
return "", err | ||
} | ||
return string(jsonRecord), 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,66 @@ | ||
// Copyright The OpenTelemetry 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 lokiexporter | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"go.opentelemetry.io/collector/model/pdata" | ||
) | ||
|
||
func exampleLog() (pdata.LogRecord, pdata.Resource) { | ||
|
||
buffer := pdata.NewLogRecord() | ||
buffer.Body().SetStringVal("Example log") | ||
buffer.SetName("name") | ||
buffer.SetSeverityText("error") | ||
buffer.Attributes().Insert("attr1", pdata.NewAttributeValueString("1")) | ||
buffer.Attributes().Insert("attr2", pdata.NewAttributeValueString("2")) | ||
buffer.SetTraceID(pdata.NewTraceID([16]byte{1, 2, 3, 4})) | ||
buffer.SetSpanID(pdata.NewSpanID([8]byte{5, 6, 7, 8})) | ||
|
||
resource := pdata.NewResource() | ||
resource.Attributes().Insert("host.name", pdata.NewAttributeValueString("something")) | ||
|
||
return buffer, resource | ||
} | ||
|
||
func exampleJSON() string { | ||
jsonExample := `{"name":"name","body":"Example log","traceid":"01020304000000000000000000000000","spanid":"0506070800000000","severity":"error","attributes":{"attr1":"1","attr2":"2"},"resources":{"host.name":"something"}}` | ||
return jsonExample | ||
} | ||
|
||
func TestConvertString(t *testing.T) { | ||
in := exampleJSON() | ||
out, err := encodeJSON(exampleLog()) | ||
t.Log(in) | ||
t.Log(out, err) | ||
assert.Equal(t, in, out) | ||
} | ||
|
||
func TestConvertNonString(t *testing.T) { | ||
in := exampleJSON() | ||
log, resource := exampleLog() | ||
mapVal := pdata.NewAttributeValueMap() | ||
mapVal.MapVal().Insert("key1", pdata.NewAttributeValueString("value")) | ||
mapVal.MapVal().Insert("key2", pdata.NewAttributeValueString("value")) | ||
mapVal.CopyTo(log.Body()) | ||
|
||
out, err := encodeJSON(log, resource) | ||
t.Log(in) | ||
t.Log(out, err) | ||
assert.EqualError(t, err, "unsuported body type to serialize") | ||
} |
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
Oops, something went wrong.