-
Notifications
You must be signed in to change notification settings - Fork 275
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: When kube-eventer write data into InfluxDB will become more detial #87
Open
sober-wang
wants to merge
3
commits into
AliyunContainerService:master
Choose a base branch
from
sober-wang:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -28,6 +28,7 @@ import ( | |
influxdb "github.com/influxdata/influxdb/client" | ||
kube_api "k8s.io/api/core/v1" | ||
"k8s.io/klog" | ||
jsoniter "github.com/json-iterator/go" | ||
) | ||
|
||
type influxdbSink struct { | ||
|
@@ -37,12 +38,39 @@ type influxdbSink struct { | |
dbExists bool | ||
} | ||
|
||
// eventStruct influxDB field more detial. | ||
type eventStruct struct{ | ||
kind string | ||
evenType string | ||
nameSpace string | ||
podName string | ||
workloadName string | ||
reason string | ||
message string | ||
firstTimestamp string | ||
lastTimestamp string | ||
} | ||
|
||
const ( | ||
eventMeasurementName = "log/events" | ||
// Event special tags | ||
eventUID = "uid" | ||
// Value Field name | ||
valueField = "value" | ||
|
||
/* ---------------------------------*/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 不要有多余的注释。下同。 |
||
// Kubernetes Event const | ||
kubeKind = "kind" | ||
evenType = "type" | ||
nameSpace = "nameSpace" | ||
podName = "podName" | ||
workloadName = "workloadName" | ||
eventReason = "reason" | ||
eventMessage = "message" | ||
firstTimestamp = "firstTimestamp" | ||
lastTimestamp = "lastTimestamp" | ||
/* -------------------------------- */ | ||
|
||
// Event special tags | ||
dbNotFoundError = "database not found" | ||
|
||
|
@@ -90,17 +118,52 @@ func eventToPointWithFields(event *kube_api.Event) (*influxdb.Point, error) { | |
return &point, nil | ||
} | ||
|
||
// getWorkloadName get workload name, deployment is no problem | ||
func getWorkloadName(podName string) string{ | ||
s := strings.Split(podName,"-") | ||
n := len(s) - 2 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. s,n 变量命名不是很规范。 |
||
return strings.Join(s[:n],"-") | ||
} | ||
|
||
// selectEventData Select kubernetes events data from value | ||
func selectEventData(value string) eventStruct{ | ||
var eventData eventStruct | ||
data := []byte(value) | ||
eventData.evenType = jsoniter.Get(data,"type").ToString() | ||
eventData.kind = jsoniter.Get(data,"involvedObject","kind").ToString() | ||
eventData.nameSpace = jsoniter.Get(data,"involvedObject","namespace").ToString() | ||
eventData.podName = jsoniter.Get(data,"involvedObject","name").ToString() | ||
eventData.workloadName = getWorkloadName(eventData.podName) | ||
eventData.reason = jsoniter.Get(data,"reason").ToString() | ||
eventData.message = jsoniter.Get(data,"message").ToString() | ||
eventData.firstTimestamp = jsoniter.Get(data,"firstTimestamp").ToString() | ||
eventData.lastTimestamp = jsoniter.Get(data,"lastTimestamp").ToString() | ||
return eventData | ||
} | ||
|
||
|
||
// eventToPoint make influxdb point from kubernetes event data | ||
func eventToPoint(event *kube_api.Event) (*influxdb.Point, error) { | ||
value, err := getEventValue(event) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
eventData := selectEventData(value) | ||
point := influxdb.Point{ | ||
Measurement: eventMeasurementName, | ||
Time: event.LastTimestamp.Time.UTC(), | ||
Fields: map[string]interface{}{ | ||
valueField: value, | ||
kubeKind: eventData.kind, | ||
evenType: eventData.evenType, | ||
nameSpace: eventData.nameSpace, | ||
podName: eventData.podName, | ||
workloadName: eventData.workloadName, | ||
eventReason: eventData.reason, | ||
eventMessage: eventData.message, | ||
firstTimestamp: eventData.firstTimestamp, | ||
lastTimestamp: eventData.lastTimestamp, | ||
}, | ||
Tags: map[string]string{ | ||
eventUID: string(event.UID), | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you remove external libs. json.Unmarshal should be enough
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know. But the jsoniter handle JSON become so convenient , and the jsoniter is very quickly.
You can see the web pages:
https://github.com/json-iterator/go