Skip to content
This repository has been archived by the owner on Jun 4, 2021. It is now read-only.

Initial implementation of a Prometheus source #663

Merged
merged 1 commit into from
Oct 25, 2019

Conversation

syedriko
Copy link
Contributor

@syedriko syedriko commented Oct 21, 2019

This is a barebones implementation of a Prometheus source for Knative Eventing.
It addresses #612.

@googlebot googlebot added the cla: yes Indicates the PR's author has signed the CLA. label Oct 21, 2019
@knative-prow-robot knative-prow-robot added size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Oct 21, 2019
@knative-prow-robot
Copy link
Contributor

Hi @syedriko. Thanks for your PR.

I'm waiting for a knative member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@knative-prow-robot knative-prow-robot added the do-not-merge/invalid-owners-file Indicates that a PR should not merge because it has an invalid OWNERS file in it. label Oct 21, 2019
@syedriko
Copy link
Contributor Author

/cc @matzew
/cc @lionelvillard

@lionelvillard
Copy link
Member

/ok-to-test

@knative-prow-robot knative-prow-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Oct 21, 2019
// ServiceAccountName holds the name of the Kubernetes service account
// as which the underlying K8s resources should be run. If unspecified
// this will default to the "default" service account for the namespace
// in which the CouchDbSource exists.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: PrometheusSource

}
)

// Reconciler reconciles a CouchDbSource object
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: PrometheusSource

eventTypes := make([]eventingv1alpha1.EventType, 0)

// Only create EventTypes for Broker sinks.
// We add this check here in case the CouchDbSource was changed from Broker to non-Broker sink.
Copy link
Member

@lionelvillard lionelvillard Oct 21, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same (CouchDb->Prometheus)

func (a *prometheusAdapter) makeEvent(payload interface{}) (*cloudevents.Event, error) {
event := cloudevents.NewEvent()
event.SetSource(a.source)
event.SetID(`prometheus_source_adapter_id`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Source + ID must be unique.

What about documenting it by adding the CloudEvents adapter spec for Prometheus? (see https://github.com/cloudevents/spec/blob/master/adapters/github.md for an example)

(I'm doing the same for CouchDB)

func newAdapterTestClient() *adapterTestClient {
return &adapterTestClient{
kncetesting.NewTestClient(),
make(chan struct{}),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you might need a buffered channel size 1, as the read/write is done in the same goroutine.

// SchemeBuilder is used to add go types to the GroupVersionKind scheme
SchemeBuilder = &scheme.Builder{GroupVersion: SchemeGroupVersion}

AddToScheme = SchemeBuilder.AddToScheme
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return nil
}

func (r *Reconciler) getReceiveAdapterImage() string {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heads-up: when this PR gets merged we should do the same, which is to set the adapter image in controller

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure thing.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@syedriko @lionelvillard it got merged!

Name: "PROMETHEUS_PROM_QL",
Value: spec.PromQL,
}, {
Name: "SYSTEM_NAMESPACE",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be NAMESPACE

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

FieldRef: &corev1.ObjectFieldSelector{
FieldPath: "metadata.namespace",
},
},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

METRICS_DOMAIN, K_METRICS_CONFIG and K_LOGGING_CONFIG are missing. Can added after this PR is merged.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@syedriko just seeing this, but I added the required bits, se:

syedriko#1

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @matzew, I merged your branch.

@syedriko syedriko force-pushed the prometheus_source branch 6 times, most recently from fc19213 to d166dd7 Compare October 22, 2019 03:16
}

func (a *prometheusAdapter) makeEvent(payload interface{}) (*cloudevents.Event, error) {
event := cloudevents.NewEvent()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cloudevents.VersionV03 as argument ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added

kind: PrometheusSource
metadata:
name: prometheus-source
namespace: knative-sources
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's remove this ns, so (like other samples), it goes to default .

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

kind: Service
metadata:
name: event-display
namespace: knative-sources
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove ns

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed

kind: PrometheusSource
metadata:
name: prometheus-source
namespace: knative-sources
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove ns

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed

@@ -0,0 +1,12 @@
apiVersion: sources.eventing.knative.dev/v1alpha1
kind: PrometheusSource
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get:

2019/10/22 10:22:03 Error processing env var: required key K_METRICS_CONFIG missing value

@matzew
Copy link
Member

matzew commented Oct 22, 2019

Got it running:

☁️  cloudevents.Event
Validation: valid
Context Attributes,
  specversion: 0.2
  type: dev.knative.prometheus.promql
  source: prometheus-source
  id: 3b0f4924-0d1c-4a3b-b15a-566c0ca67c83
  time: 2019-10-22T12:28:35.239998202Z
  contenttype: application/json
Data,
  {
    "status": "success",
    "data": {
      "resultType": "vector",
      "result": [
        {
          "metric": {
            "__name__": "ALERTS",
            "alertname": "ExampleAlertAlwaysFiring",
            "alertstate": "firing",
            "job": "alertmanager"
          },
          "value": [
            1571747315.23,
            "1"
          ]
        },
        {
          "metric": {
            "__name__": "ALERTS",
            "alertname": "ExampleAlertAlwaysFiring",
            "alertstate": "firing",
            "job": "node"
          },
          "value": [
            1571747315.23,
            "1"
          ]
        },
        {
          "metric": {
            "__name__": "ALERTS",
            "alertname": "ExampleAlertAlwaysFiring",
            "alertstate": "firing",
            "job": "prometheus"
          },
          "value": [
            1571747315.23,
            "1"
          ]
        },
        {
          "metric": {
            "__name__": "ALERTS",
            "alertname": "ExampleAlertAlwaysFiring",
            "alertstate": "firing",
            "job": "pushgateway"
          },
          "value": [
            1571747315.23,
            "1"
          ]
        }
      ]
    }
  }

as said in the comments, other sources are on CE 0.3 - let's update that


func (a *prometheusAdapter) makeEvent(payload interface{}) (*cloudevents.Event, error) {
event := cloudevents.NewEvent()
event.SetSource(a.source)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should use something better here.

for instance in the kafka source, we add some context to it, like:

// KafkaEventSource returns the Kafka CloudEvent source.
func KafkaEventSource(namespace, kafkaSourceName, topic string) string {
	return fmt.Sprintf("/apis/v1/namespaces/%s/kafkasources/%s#%s", namespace, kafkaSourceName, topic)
}

were that is used as:

...
	event.SetSource(sourcesv1alpha1.KafkaEventSource(a.Namespace, a.Name, msg.Topic))

...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed

Copy link
Contributor

@vaikas vaikas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sweet! Couple of tweaks wrt the api definition.

serverURL:
type: string
promQL:
type: string
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we add "description" fields here as per:
https://github.com/knative/eventing/blob/master/docs/spec/sources.md

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We sure can

// Sink is a reference to an object that will resolve to a domain
// name to use as the sink.
// +optional
Sink *corev1.ObjectReference `json:"sink,omitempty"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a Destination:
https://github.com/knative/eventing/blob/master/docs/spec/sources.md

And we should also have CloudEventOverrides here as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to push back on this if that's ok. Can we merge this PR as it stands wrt sinks and ceOverrides and do the conversion of sinks to Destinations across the board in all the sources?
It would be nice to update https://github.com/knative/eventing/blob/master/pkg/duck/sinks.go to retrieve URIs from Destinations according to https://github.com/knative/eventing/blob/master/docs/spec/sources.md, update vendored knative.dev/pkg and knative.dev/eventing in eventing-contrib.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree w/ @syedriko on this subject

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keeping it consistent and doing conversion and testing (and upgrade docs if needed) for all sources in contrib sounds good to me

"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"knative.dev/pkg/apis/duck"
duckv1beta1 "knative.dev/pkg/apis/duck/v1beta1"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might as well use duck v1 instead of the v1beta1?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We sure might


// PrometheusSourceStatus defines the observed state of PrometheusSource
type PrometheusSourceStatus struct {
// inherits duck/v1alpha1 Status, which currently provides:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

v1alpha1 => v1

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep

// Check that Prometheus source can be validated and can be defaulted.
var _ runtime.Object = (*PrometheusSource)(nil)

// Check that we can create OwnerReferences to a Configuration.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/Configuration/Prometheus Source/?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course.

}

func (a *prometheusAdapter) Start(stopCh <-chan struct{}) error {
return wait.PollUntil(5*time.Second, a.send, stopCh)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think wait.Until is better here. PollUntil stops when there is an error, meaning that the adapter will stop running when an event couldn't be sent, which is not what we want. Instead we should log the error (for now) and keep going.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wondering about that, too, tbh. And the hardcoded interval here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe. I'm not so sure that all errors can be caught at initialization time. But this is something to consider.

@matzew
Copy link
Member

matzew commented Oct 23, 2019

/lgtm

Leaving the final approve for @vaikas-google

@knative-prow-robot knative-prow-robot added the lgtm Indicates that a PR is ready to be merged. label Oct 23, 2019
*reconciler.Base

receiveAdapterImage string
once sync.Once
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not used?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

evidently not

@knative-prow-robot knative-prow-robot removed the lgtm Indicates that a PR is ready to be merged. label Oct 23, 2019
@matzew
Copy link
Member

matzew commented Oct 24, 2019

/lgtm
/approve

@knative-prow-robot knative-prow-robot added the lgtm Indicates that a PR is ready to be merged. label Oct 24, 2019
@knative-prow-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: matzew, syedriko

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@knative-prow-robot knative-prow-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Oct 24, 2019
@knative-prow-robot knative-prow-robot removed the lgtm Indicates that a PR is ready to be merged. label Oct 24, 2019
@syedriko
Copy link
Contributor Author

syedriko commented Oct 24, 2019

Squashed the commits.

Can I be accepted into the knative org or should I remove myself from OWNERS_ALIASES?

The question went to knative-admins@googlegroups.com.

@knative-metrics-robot
Copy link

The following is the coverage report on the affected files.
Say /test pull-knative-eventing-contrib-go-coverage to re-run this coverage report

File Old Coverage New Coverage Delta
prometheus/pkg/adapter/adapter.go Do not exist 70.0%
prometheus/pkg/apis/sources/v1alpha1/prometheus_lifecycle.go Do not exist 53.8%
prometheus/pkg/apis/sources/v1alpha1/prometheussource_types.go Do not exist 100.0%
prometheus/pkg/apis/sources/v1alpha1/register.go Do not exist 100.0%

@syedriko
Copy link
Contributor Author

/verify-owners

@knative-prow-robot knative-prow-robot removed the do-not-merge/invalid-owners-file Indicates that a PR should not merge because it has an invalid OWNERS file in it. label Oct 24, 2019
@matzew
Copy link
Member

matzew commented Oct 25, 2019

/lgtm

@knative-prow-robot knative-prow-robot added the lgtm Indicates that a PR is ready to be merged. label Oct 25, 2019
@knative-prow-robot knative-prow-robot merged commit d9b9399 into knative:master Oct 25, 2019
@syedriko syedriko deleted the prometheus_source branch October 25, 2019 15:43
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. area/test-and-release cla: yes Indicates the PR's author has signed the CLA. lgtm Indicates that a PR is ready to be merged. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants