Code Monkey home page Code Monkey logo

stackdriver-logging_pubsub_functions's Introduction

stackdriver-logging_pubsub_functions

Summary

this document show steps to filter stackdriver logs, stream them to cloud pub/sub and trigger a cloud function to further process them.

For the sake of the demo we will be creating a kubernetes cluster which we will be using to filter its logs and ship them to Cloud PubSub.

Steps

  1. create a k8s cluster called log-demo

     zone=us-west1
     gcloud container clusters create log-demo-cluster --zone $zone
    
  2. create a pubsub topic where we will send our cluster logs

     gcloud pubsub topics create log-demo-cluster-errors
    
  3. create a function to process the pubsub messages(logs)

Here, we are simply printing back the filtered log but it's up to you to process the log as you wish and perhaps store it later in a storage service like cloud BigTable, BigQuery, GCS. you can write your function in any of the following: python, nodejs, GO.

    cat<< EOF > main.py
    def hello_pubsub(data, context):
        """Background Cloud Function to be triggered by Pub/Sub.
        Args:
            data (dict): The dictionary with data specific to this type of event.
            context (google.cloud.functions.Context): The Cloud Functions event
            metadata.
        """
        import base64

        if 'data' in data:
            payload = base64.b64decode(data['data']).decode('utf-8')
        else:
            payload = 'EMPTY'
        print('I am a log entry, do somehting with me: {}!'.format(payload))
    EOF
  1. deploy the function while specifying our previously created topic as the trigger

    gcloud functions deploy hello_pubsub --source `pwd` --runtime python37 --trigger-topic log-demo-cluster-errors
    
  2. create a pubsub log export sink while filtering logs specific to our gke cluster which has severity warning and above: NOTE: change PROJECT_ID to your project id

     gcloud beta logging sinks create pub-sub-sink \
         pubsub.googleapis.com/projects/[PROJECT_ID]/topics/log-demo-cluster-errors \
         --log-filter='resource.type="container" AND resource.labels.cluster_name="log-demo-cluster" AND severity>=WARNING'
    
  3. get the pubsub sink writer identity service account

     sink_sa=$(gcloud beta logging sinks describe pub-sub-sink --format="(writerIdentity)" | awk '{print $2}')
    
  4. Grant role of cloud pub sub publisher to the service account gcloud projects add-iam-policy-binding rad-tests --member=$sink_sa --role=roles/pubsub.publisher

  5. Create some errors withing the cluster (deploy many replicas)

     kubectl run nginx --image=nginx --replicas=20
    
  6. Get the logs from our cloud function execution:

     gcloud functions logs read hello_pubsub
    

stackdriver-logging_pubsub_functions's People

Contributors

raddaoui avatar

Watchers

 avatar  avatar

Forkers

sadasystems

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.