Categorygithub.com/orian/sdhook
modulepackage
1.1.5
Repository: https://github.com/orian/sdhook.git
Documentation: pkg.go.dev

# README

About sdhook

Package sdhook provides a logrus compatible hook for Google Stackdriver logging.

This is a hard fork of github.com/knq/sdhook. It adds semantic versioning and replaces deprecated google.golang.org/api/logging/v2 with cloud.google.com/go/logging

Installation

Install in the usual Go way:

go get -u github.com/orian/sdhook

Usage

Simply create the hook, and add it to a logrus logger:

// create hook using service account credentials
h, err := sdhook.New(
	sdhook.GoogleServiceAccountCredentialsFile("./credentials.json"),
)

// create logger with extra fields
//
// logrus fields will be converted to Stackdriver labels
logger := logrus.New().WithFields(logrus.Fields{
	"field1": 15,
	"field2": 20,
})

// add hook
logger.Hooks.Add(h)

// log something
logger.Printf("something %d", 15)

The example above sends log entries directly to the logging API. If you have the logging agent running, you can send log entries to it instead, with the added benefit of having extra instance metadata added to your log entries by the agent. In the example above, the initialization would simply be:

// create hook using the logging agent
h, err := sdhook.New(
	sdhook.GoogleLoggingAgent(),
)

Please also see example/example.go for a more complete example.

Error Reporting

If you'd like to enable sending errors to Google's Error Reporting (https://cloud.google.com/error-reporting/), you have to set the name of the service, app or system you're running. Following the example above, the initialization would then be:

// create hook using the logging agent
h, err := sdhook.New(
	sdhook.GoogleLoggingAgent(),
	sdhook.ErrorReportingService("your-great-app"),
)

The value of the ErrorReportingService function parameter above corresponds to the string value you'd like to see in the service field of the Error Reporting payload, as defined by https://cloud.google.com/error-reporting/docs/formatting-error-messages

Also note that, if you enable error reporting, errors and messages of more severe levels go into the error log and will not be displayed in the regular log. The error log name is either defined by the ErrorReportingLogName function or defaults to <regular-log-name>_errors. This fulfills Google's Error Reporting requirement that the log name should have the string err in its name. See more in: https://cloud.google.com/error-reporting/docs/setup/ec2

See GoDoc for a full API listing.

# Packages

No description provided by the author

# Functions

ErrorReportingLogName is an option that sets the log name to send with each error message for error reporting.
ErrorReportingService is an option that defines the name of the serviceClient being tracked for Stackdriver error reporting.
ErrorService is an option that sets the Google API error reporting serviceClient to use.
No description provided by the author
GoogleDefaultCredentials returns the token source for "Application Default Credentials".
No description provided by the author
Labels is an option that sets the labels to send with each log entry.
Levels is an option that sets the logrus levels that the StackdriverHook will create log entries for.
LoggingService is an option that sets the Google API logging serviceClient to use.
LogName is an option that sets the log name to send with each log entry.
MonitoredResource is an option that sets the monitored resource to send with each log entry.
New creates a StackdriverHook using the provided options that is suitible for using with logrus for logging to Google Stackdriver.
PartialSuccess is an option that toggles whether or not to write partial log entries.
ProjectID is an option that sets the project ID which is needed for the log name.
Resource is an option that sets the resource information to send with each log entry.
No description provided by the author

# Constants

DefaultName is the default name passed to LogName when using serviceClient account credentials.
ResTypeApi, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_api.
ResTypeAppScriptFunction, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_app_script_function.
ResTypeAssistantAction, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_assistant_action.
ResTypeAuditedResource, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_audited_resource.
ResTypeAwsAlbLoadBalancer, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_aws_alb_load_balancer.
ResTypeAwsCloudfrontDistribution, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_aws_cloudfront_distribution.
ResTypeAwsDynamodbTable, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_aws_dynamodb_table.
ResTypeAwsEbsVolume, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_aws_ebs_volume.
ResTypeAwsEc2_instance, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_aws_ec2_instance.
ResTypeAwsElasticacheCluster, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_aws_elasticache_cluster.
ResTypeAwsElbLoadBalancer, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_aws_elb_load_balancer.
ResTypeAwsEmrCluster, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_aws_emr_cluster.
ResTypeAwsKinesisStream, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_aws_kinesis_stream.
ResTypeAwsLambdaFunction, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_aws_lambda_function.
ResTypeAwsRdsDatabase, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_aws_rds_database.
ResTypeAwsRedshiftCluster, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_aws_redshift_cluster.
ResTypeAwsS3_bucket, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_aws_s3_bucket.
ResTypeAwsSes, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_aws_ses.
ResTypeAwsSnsTopic, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_aws_sns_topic.
ResTypeAwsSqsQueue, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_aws_sqs_queue.
ResTypeBigqueryDataset, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_bigquery_dataset.
ResTypeBigqueryProject, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_bigquery_project.
ResTypeBigtableCluster, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_bigtable_cluster.
ResTypeBigtableTable, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_bigtable_table.
ResTypeBuild, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_build.
ResTypeCloudComposerEnvironment, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_cloud_composer_environment.
ResTypeCloudDataprocCluster, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_cloud_dataproc_cluster.
ResTypeCloudDataprocJob, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_cloud_dataproc_job.
ResTypeCloudFunction, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_cloud_function.
ResTypeCloudiotDevice, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_cloudiot_device.
ResTypeCloudiotDeviceRegistry, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_cloudiot_device_registry.
ResTypeCloudmlJob, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_cloudml_job.
ResTypeCloudmlModelVersion, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_cloudml_model_version.
ResTypeCloudRunRevision, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_cloud_run_revision.
ResTypeCloudSchedulerJob, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_cloud_scheduler_job.
ResTypeCloudsqlDatabase, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_cloudsql_database.
ResTypeCloudTasksQueue, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_cloud_tasks_queue.
ResTypeConsumedApi, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_consumed_api.
ResTypeCsrRepository, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_csr_repository.
ResTypeDataflowJob, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_dataflow_job.
ResTypeDatastoreRequest, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_datastore_request.
ResTypeDnsManagedZone, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_dns_managed_zone.
ResTypeDnsPolicy, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_dns_policy.
ResTypeDnsQuery, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_dns_query.
ResTypeFilestoreInstance, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_filestore_instance.
ResTypeFirebaseDomain, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_firebase_domain.
ResTypeFirebaseNamespace, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_firebase_namespace.
ResTypeFirestoreInstance, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_firestore_instance.
ResTypeGaeApp, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_gae_app.
ResTypeGaeInstance, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_gae_instance.
ResTypeGceDisk, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_gce_disk.
ResTypeGceInstance, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_gce_instance.
ResTypeGceNodeGroup, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_gce_node_group.
ResTypeGceNodeTemplate, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_gce_node_template.
No description provided by the author
ResTypeGceResourcePolicy, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_gce_resource_policy.
ResTypeGceRouter, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_gce_router.
ResTypeGcsBucket, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_gcs_bucket.
ResTypeGenericNode, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_generic_node.
ResTypeGenericTask, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_generic_task.
ResTypeGkeContainer, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_gke_container.
ResTypeGlobal, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_global.
ResTypeHttpsLbRule, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_https_lb_rule.
ResTypeIdentitytoolkitProject, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_identitytoolkit_project.
ResTypeIdentitytoolkitTenant, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_identitytoolkit_tenant.
ResTypeInterconnect, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_interconnect.
ResTypeInterconnectAttachment, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_interconnect_attachment.
ResTypeInternalHttpLbRule, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_internal_http_lb_rule.
ResTypeInternalTcpLbRule, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_internal_tcp_lb_rule.
ResTypeInternalUdpLbRule, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_internal_udp_lb_rule.
ResTypeK8s_cluster, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_k8s_cluster.
ResTypeK8s_container, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_k8s_container.
ResTypeK8s_node, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_k8s_node.
ResTypeK8s_pod, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_k8s_pod.
ResTypeKnativeRevision, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_knative_revision.
ResTypeL7_lb_rule, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_l7_lb_rule.
ResTypeLoggingExclusion, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_logging_exclusion.
ResTypeLoggingSink, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_logging_sink.
ResTypeMetric, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_metric.
ResTypeMicrosoftAdDomain, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_microsoft_ad_domain.
ResTypeNatGateway, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_nat_gateway.
ResTypeNetappCloudVolume, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_netapp_cloud_volume.
ResTypeNetworkSecurityPolicy, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_network_security_policy.
ResTypePubsubSnapshot, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_pubsub_snapshot.
ResTypePubsubSubscription, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_pubsub_subscription.
ResTypePubsubTopic, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_pubsub_topic.
ResTypeRecommender, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_recommender.
ResTypeRedisInstance, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_redis_instance.
ResTypeSpannerInstance, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_spanner_instance.
ResTypeTcpLbRule, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_tcp_lb_rule.
ResTypeTcpSslProxyRule, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_tcp_ssl_proxy_rule.
ResTypeTpuWorker, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_tpu_worker.
ResTypeUdpLbRule, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_udp_lb_rule.
ResTypeUptimeUrl, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_uptime_url.
ResTypeVpnGateway, for description and list of labels https://cloud.google.com/monitoring/api/resources#tag_vpn_gateway.

# Variables

No description provided by the author

# Structs

StackdriverHook provides a logrus hook to Google Stackdriver logging.

# Type aliases

Option represents an option that modifies the Stackdriver hook settings.
ResType is a monitored resource descriptor type.